UNPKG

1.99 kBPlain TextView Raw
1declare global {
2 interface ObjectConstructor {
3 assign<T>(target:T, ...sources: any[]):T & any;
4 isEqual(o1:any,o2:any):boolean
5 }
6
7 interface Array<T> {
8 includes(element:T):boolean
9 size:number
10 }
11
12 interface ArrayConstructor {
13 isEqual(arr1:any[],arr2:any[],ignoreOrder?:boolean):boolean
14 }
15
16 var __filename:string
17}
18
19/**
20 * Add Object.isEqual to static type
21 */
22if (!Object.isEqual) {
23 Object.isEqual = (o1:any,o2:any):boolean => {
24 return o1 === o2 ||
25 (o1.isEqual && o2.isEqual && o1.isEqual(o2))
26 }
27}
28
29/**
30 * Add Array.isEqual
31 */
32if (!Array.isEqual) {
33
34 Array.isEqual = function(arr1,arr2,ignoreOrder = false) {
35
36 if (arr1 === arr2) return true;
37 if (!Array.isArray(arr1) || !Array.isArray(arr2)) return false;
38 if (arr1.length !== arr2.length) return false;
39
40
41 // If you don't care about the order of the elements inside
42 // the array, you should sort both arrays here.
43 if (!ignoreOrder) {
44 for (var i = 0; i < arr1.length; ++i) {
45 if (arr1[i] !== arr2[i]) return false;
46 }
47 } else {
48 for (let item1 of arr1) {
49 if (!arr2.find(item2 => Object.isEqual(item1,item2)))
50 return false
51 }
52 }
53 return true;
54
55 }
56}
57
58
59if (!Object.getOwnPropertyDescriptor(Array.prototype,'size')) {
60 Object.defineProperty(Array.prototype,'size',{
61 get: function() {
62 return this.length
63 }
64 })
65}
66
67/**
68 * Polyfill Array.prototype.includes
69 */
70if (!Array.prototype.includes) {
71 Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
72 'use strict';
73 let O = Object(this) as any;
74 let len = parseInt(O.length) || 0;
75 if (len === 0) {
76 return false;
77 }
78 let n = parseInt(arguments[1]) || 0;
79 let k;
80 if (n >= 0) {
81 k = n;
82 } else {
83 k = len + n;
84 if (k < 0) {k = 0;}
85 }
86 let currentElement;
87 while (k < len) {
88 currentElement = O[k];
89 if (searchElement === currentElement ||
90 (searchElement !== searchElement && currentElement !== currentElement)) { // NaN !== NaN
91 return true;
92 }
93 k++;
94 }
95 return false;
96 };
97}
98
99export {}
\No newline at end of file