Project

General

Profile

1
export class HelperFunctions {
2
  //Use this class function to create queryParams Objects in format {key1:value1} or {key1:value1,key2:value2,key3:value3,...} for  multiple parameters
3
  constructor() {
4
  }
5

    
6
  public static scroll() {
7
    if (typeof document !== 'undefined') {
8
      //this.element.nativeElement.scrollIntoView();
9
      window.scrollTo(0, 0);
10
    }
11
  }
12

    
13
  public static isTiny(url: string) {
14
    return (url.indexOf('tinyurl.com') !== -1);
15
  }
16

    
17
  public static copy(obj: any): any {
18
    let copy;
19

    
20
    // Handle the 3 simple types, and null or undefined
21
    if (null == obj || "object" != typeof obj) return obj;
22

    
23
    // Handle Date
24
    if (obj instanceof Date) {
25
      copy = new Date();
26
      copy.setTime(obj.getTime());
27
      return copy;
28
    }
29

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

    
39
    // Handle Map
40
    if (obj instanceof Map) {
41
      copy = new Map(obj.entries());
42
      return copy;
43
    }
44

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

    
58
  public static encodeArray(elements: string[]): string[] {
59
    let encoded: string[] = [];
60
    elements.forEach(element => {
61
      encoded.push(encodeURIComponent(element));
62
    });
63
    return encoded;
64
  }
65
}
(1-1/18)