-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor.js
142 lines (130 loc) · 4.45 KB
/
xor.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var xor = (function() {
"use strict";
var filter = Array.prototype.filter,
map = Array.prototype.map,
flatMap = Array.prototype.flatMap,
includes = Array.prototype.includes,
each = Array.prototype.forEach,
slice = Array.prototype.slice,
toArray = Array.from,
isArray = Array.isArray;
var detect = [
"function",
"boolean",
"number",
"bigint",
"string",
"symbol"
];
var regex = {
"'": /\\\'/g,
'"': /\\\"/g,
};
var path = {
parse: function(str) {
var i = 0;
var parts = [];
var dot, bracket, quote, closing;
while (i < str.length) {
dot = str.indexOf('.', i);
bracket = str.indexOf('[', i);
if (dot === -1 && bracket === -1) {
parts.push(str.slice(i, str.length));
i = str.length;
}
else if (bracket === -1 || (dot !== -1 && dot < bracket)) {
parts.push(str.slice(i, dot));
i = dot + 1;
}
else {
if (bracket > i) {
parts.push(str.slice(i, bracket));
i = bracket;
}
quote = str.slice(bracket + 1, bracket + 2);
if (quote !== '"' && quote !== "'") {
closing = str.indexOf(']', bracket);
if (closing === -1) {
closing = str.length;
}
parts.push(str.slice(i + 1, closing));
i = (str.slice(closing + 1, closing + 2) === '.') ? closing + 2 : closing + 1;
} else {
closing = str.indexOf(quote + ']', bracket);
if (closing === -1) {
closing = str.length;
}
while (str.slice(closing - 1, closing) === '\\' && bracket < str.length) {
bracket++;
closing = str.indexOf(quote + ']', bracket);
}
parts.push(str.slice(i + 2, closing).replace(regex[quote], quote).replace(/\\+/g, function (backslash) {
return new Array(Math.ceil(backslash.length / 2) + 1).join('\\');
}));
i = (str.slice(closing + 2, closing + 3) === '.') ? closing + 3 : closing + 2;
}
}
}
return parts;
},
getValue: function(o, path) {
return path.reduce(function(o, next) {
return o == null ? undefined : o[next];
}, o);
}
};
var property = function(key) {
return function(o) {
return o == null ? undefined : path.getValue(o, path.parse(String(key)));
};
};
var identity = function(v) {
return v;
};
var last = function(o) {
return o[o.length - 1];
};
var drop = function(o, n) {
if (n == null) {
n = 0;
}
return slice.call(o, 0, o.length - n);
};
var uniq = function(array) {
return toArray(new Set(array));
};
var xor = function(arrays, diff) {
return flatMap.call(arrays, function(array, current) {
each.call(arrays, function(each, index) {
if (index === current) {
return;
}
array = diff(array, each);
});
return array;
});
};
return function(arrays, iteratee) {
iteratee = last(arguments);
if (includes.call(detect, typeof iteratee)) {
arrays = drop(arguments, 1);
if (typeof iteratee !== "function") {
iteratee = property(iteratee);
}
} else {
arrays = drop(arguments);
iteratee = identity;
}
arrays = filter.call(arrays, function(array) {
return isArray(array);
});
if (!arrays.length) {
return [];
}
return uniq(xor(arrays, function(array, values) {
return filter.call(array, function(v) {
return !includes.call(map.call(values, iteratee), iteratee(v));
});
}));
};
})();