-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenrich.js
95 lines (83 loc) · 2.05 KB
/
enrich.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function findOne(property){
for(var name in property) {}
for(var i in this){
if(this[i][name] == property[name]) return this[i];
}
}
function removeOnee(property){
for(var name in property) {}
for(var i in this){
if(this[i][name] == property[name]){
this.splice(i, 1);
break;
}
}
}
function removeOne(property){
for(var i in this){
var matching = true;
for(var name in property) {
matching = this[i][name] == property[name];
if(!matching) break;
}
if(matching){
this.splice(i, 1);
return true;
}
}
}
function merge(/* variable number of arrays */){
for(var i = 0; i < arguments.length; i++){
var array = arguments[i];
for(var j = 0; j < array.length; j++){
if(this.indexOf(array[j]) === -1) {
this.push(array[j]);
}
}
}
return this;
}
function deepclone(){
var nA = [];
for(var i in this){
if(typeof this[i] == 'object'){
var obj = this[i],
nObj = {};
for(var j in obj){
nObj[j] = obj[j];
}
nA.push(nObj);
}
else nA.push(this[i]);
}
return nA;
}
function countChildren(){
var t = 0;
for(var i in this) t++;
return t;
}
function pickProperties(names){
var object = {},
name;
for(var i in names){
name = names[i];
if(name in this) object[name] = this[name];
}
return object;
}
function toCSV( delimiter, seperator ) {
var arr = [];
seperator = seperator || '=';
for(var i in this){
arr.push( i + seperator + this[i] );
}
return arr.join(delimiter || ',');
}
Object.defineProperty(Array.prototype, 'findOne', {value: findOne});
Object.defineProperty(Array.prototype, 'removeOne', {value: removeOne});
Object.defineProperty(Array.prototype, 'merge', {value: merge});
Object.defineProperty(Array.prototype, 'deepclone', {value: deepclone});
Object.defineProperty(Object.prototype, 'countChildren', {value: countChildren});
Object.defineProperty(Object.prototype, 'pickProperties', {value: pickProperties});
Object.defineProperty(Object.prototype, 'toCSV', {value: toCSV});