Project

General

Profile

1
export class HelperFunctions {
2

    
3
  public static scroll() {
4
    if (typeof document !== 'undefined') {
5
      window.scrollTo(0, 0);
6
    }
7
  }
8

    
9
  public static isTiny(url: string) {
10
    return (url.indexOf('tinyurl.com') !== -1);
11
  }
12

    
13
  public static copy(obj: any): any {
14
    let copy;
15

    
16
    // Handle the 3 simple types, and null or undefined
17
    if (null == obj || "object" != typeof obj) return obj;
18

    
19
    // Handle Date
20
    if (obj instanceof Date) {
21
      copy = new Date();
22
      copy.setTime(obj.getTime());
23
      return copy;
24
    }
25

    
26
    // Handle Array
27
    if (obj instanceof Array) {
28
      copy = [];
29
      for (let i = 0, len = obj.length; i < len; i++) {
30
        copy[i] = HelperFunctions.copy(obj[i]);
31
      }
32
      return copy;
33
    }
34

    
35
    // Handle Map
36
    if (obj instanceof Map) {
37
      return new Map(obj.entries());
38
    }
39

    
40
    // Handle Object
41
    if (obj instanceof Object) {
42
      copy = {};
43
      for (let attr in obj) {
44
        if (obj.hasOwnProperty(attr)) {
45
          copy[attr] = HelperFunctions.copy(obj[attr]);
46
        }
47
      }
48
      return copy;
49
    }
50
    throw new Error("Unable to copy obj! Its type isn't supported.");
51
  }
52

    
53
  public static encodeArray(elements: string[]): string[] {
54
    let encoded: string[] = [];
55
    elements.forEach(element => {
56
      encoded.push(encodeURIComponent(element));
57
    });
58
    return encoded;
59
  }
60
}
(1-1/21)