Project

General

Profile

1
export class HelperFunctions {
2

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

    
12
  public static scrollToId(elementId:string) {
13
    if (typeof document !== 'undefined' && document.getElementById(elementId)) {
14
      document.getElementById(elementId).scrollIntoView();
15
    }
16
  }
17
  public static isTiny(url: string) {
18
    return (url.indexOf('tinyurl.com') !== -1);
19
  }
20

    
21
  public static copy(obj: any): any {
22
    let copy;
23

    
24
    // Handle the 3 simple types, and null or undefined
25
    if (null == obj || "object" != typeof obj) return obj;
26

    
27
    // Handle Date
28
    if (obj instanceof Date) {
29
      copy = new Date();
30
      copy.setTime(obj.getTime());
31
      return copy;
32
    }
33

    
34
    // Handle Array
35
    if (obj instanceof Array) {
36
      copy = [];
37
      for (let i = 0, len = obj.length; i < len; i++) {
38
        copy[i] = HelperFunctions.copy(obj[i]);
39
      }
40
      return copy;
41
    }
42

    
43
    // Handle Map
44
    if (obj instanceof Map) {
45
      return new Map(obj.entries());
46
    }
47

    
48
    // Handle Object
49
    if (obj instanceof Object) {
50
      copy = {};
51
      for (let attr in obj) {
52
        if (obj.hasOwnProperty(attr)) {
53
          copy[attr] = HelperFunctions.copy(obj[attr]);
54
        }
55
      }
56
      return copy;
57
    }
58
    throw new Error("Unable to copy obj! Its type isn't supported.");
59
  }
60

    
61
  public static encodeArray(elements: string[]): string[] {
62
    let encoded: string[] = [];
63
    elements.forEach(element => {
64
      encoded.push(encodeURIComponent(element));
65
    });
66
    return encoded;
67
  }
68
}
(1-1/21)