-
Notifications
You must be signed in to change notification settings - Fork 0
/
teocli.global.ts
69 lines (59 loc) · 1.77 KB
/
teocli.global.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//declare global {
interface Array<T> {
inArray(
comparer: (currentElement: T) => boolean,
exists_cb: (currentElement: T) => void
): boolean;
pushIfNotExist(
element: T,
comparer: (currentElement: T) => boolean,
done_cb: (currentElement: T) => void,
exists_cb: (currentElement: T) => void
): void;
doIfNotExist(
element: T,
comparer: (currentElement: T) => boolean,
do_cb: (currentElement: T) => void
): void;
}
//}
// check if an element exists in array using a comparer function
// comparer : function(currentElement)
if (!Array.prototype.inArray)
Array.prototype.inArray = function <T>(
comparer: (currentElement: T) => boolean,
exists_cb: (currentElement: T) => void) {
var retval = false;
for (var i = 0; i < this.length; i++) {
if (comparer(this[i])) {
if (typeof exists_cb === 'function') exists_cb(this[i]);
retval = true;
}
}
return retval;
};
// adds an element to the array if it does not already exist using a comparer
// function
if (!Array.prototype.pushIfNotExist)
Array.prototype.pushIfNotExist = function <T>(
element: T,
comparer: (currentElement: T) => boolean,
done_cb: (currentElement: T) => void,
exists_cb: (currentElement: T) => void) {
//console.log("pushIfNotExist", element);
if (!this.inArray(comparer, exists_cb)) {
this.push(element);
if (typeof done_cb === 'function')
done_cb(element);
}
};
if (!Array.prototype.doIfNotExist)
Array.prototype.doIfNotExist = function <T>(
element: T,
comparer: (currentElement: T) => boolean,
do_cb: (currentElement: T) => void) {
if (!this.inArray(comparer)) {
if (typeof do_cb === 'function')
do_cb(element);
}
};