aboutsummaryrefslogtreecommitdiff
path: root/src/Utils.ts
diff options
context:
space:
mode:
authorMohammad Amoush <mohammad_amoush@brown.edu>2019-07-22 13:57:10 -0400
committerMohammad Amoush <mohammad_amoush@brown.edu>2019-07-22 13:57:10 -0400
commit658ded478c273654174cd2706f8b3e021b8ceb95 (patch)
tree7526f7850dd3702b9746125d2f4349a0d6aea8ee /src/Utils.ts
parent157060f7e6029c76765aa20d8fdbe325401a3880 (diff)
parent8db50c6ba0be83b85c896043da53e40c17523e90 (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into youtube-api-muhammed
Diffstat (limited to 'src/Utils.ts')
-rw-r--r--src/Utils.ts29
1 files changed, 28 insertions, 1 deletions
diff --git a/src/Utils.ts b/src/Utils.ts
index e8a80bdc3..8df67df5d 100644
--- a/src/Utils.ts
+++ b/src/Utils.ts
@@ -2,6 +2,7 @@ import v4 = require('uuid/v4');
import v5 = require("uuid/v5");
import { Socket } from 'socket.io';
import { Message } from './server/Message';
+import { RouteStore } from './server/RouteStore';
export class Utils {
@@ -27,6 +28,18 @@ export class Utils {
return { scale, translateX, translateY };
}
+ /**
+ * A convenience method. Prepends the full path (i.e. http://localhost:1050) to the
+ * requested extension
+ * @param extension the specified sub-path to append to the window origin
+ */
+ public static prepend(extension: string): string {
+ return window.location.origin + extension;
+ }
+ public static CorsProxy(url: string): string {
+ return this.prepend(RouteStore.corsProxy + "/") + encodeURIComponent(url);
+ }
+
public static CopyText(text: string) {
var textArea = document.createElement("textarea");
textArea.value = text;
@@ -133,7 +146,7 @@ export type Without<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export type Predicate<K, V> = (entry: [K, V]) => boolean;
-export function deepCopy<K, V>(source: Map<K, V>, predicate?: Predicate<K, V>) {
+export function DeepCopy<K, V>(source: Map<K, V>, predicate?: Predicate<K, V>) {
let deepCopy = new Map<K, V>();
let entries = source.entries(), next = entries.next();
while (!next.done) {
@@ -144,4 +157,18 @@ export function deepCopy<K, V>(source: Map<K, V>, predicate?: Predicate<K, V>) {
next = entries.next();
}
return deepCopy;
+}
+
+export namespace JSONUtils {
+
+ export function tryParse(source: string) {
+ let results: any;
+ try {
+ results = JSON.parse(source);
+ } catch (e) {
+ results = source;
+ }
+ return results;
+ }
+
} \ No newline at end of file