-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (167 loc) · 6.48 KB
/
index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"use strict";
(function (root, factory) {
// Configuration
var exportName = 'oQuery';
if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory.call(root);
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(exportName, [], factory);
} else {
// Browser globals (root is window)
root[exportName] = factory.call(root);
}
// Dependencies passed as arguments
}(this,function(){
var objectQuery = {};
function pointerToObjPath(pointer){
return pointer.split('/').filter(function(objPath) {
return objPath !== '';
});
}
function objPathToPointer(objPath){
return objPath.join('/');
}
objectQuery.deepMerge = function(target, src) {
var array = Array.isArray(src)
var dst = array && [] || {}
if (array) {
target = target || []
dst = dst.concat(target)
src.forEach(function(e, i) {
if (typeof target[i] === 'undefined') {
dst[i] = e
} else if (typeof e === 'object') {
dst[i] = objectQuery.deepMerge(target[i], e)
} else {
if (target.indexOf(e) === -1) {
dst.push(e)
}
}
})
} else {
if (target && typeof target === 'object') {
Object.keys(target).forEach(function (key) {
dst[key] = target[key]
})
}
Object.keys(src).forEach(function (key) {
if (typeof src[key] !== 'object' || !src[key]) {
dst[key] = src[key]
}
else {
if (!target[key]) {
dst[key] = src[key]
} else {
dst[key] = objectQuery.deepMerge(target[key], src[key])
}
}
});
}
return dst
}
objectQuery.set = function(pointer, patch, obj){
if(pointer !== '/' && pointer !== ''){
var link = obj;
var objPath = pointerToObjPath(pointer);
for(var i = 0, len = objPath.length - 1; i < len; i++){
var tmp = link[objPath[i]];
if(tmp !== undefined){
link = tmp;
}else{
link[objPath[i]] = {};
link = link[objPath[i]];
}
}
link[objPath[objPath.length - 1]] = patch;
}else{
obj = patch;
}
return obj;
};
objectQuery.get = function(pointer, obj){
if(!obj) return undefined;
for(var i = 0, points = pointerToObjPath(pointer), _length = points.length; i < _length; i++){
obj = obj[points[i]];
if(obj === undefined){
return undefined;
}else if (obj === null){
if(i === points.length - 1){
return obj;
}else{
return undefined;
}
}
}
return obj;
};
objectQuery.remove = function(pointer, obj){
var pointerInArray = pointerToObjPath(pointer);
var removedItem = pointerInArray[pointerInArray.length-1];
pointerInArray.splice(pointerInArray.length-1,1);
pointer = objPathToPointer(pointerInArray);
var patch = objectQuery.get(pointer,obj);
if(Array.isArray(patch)){
patch.splice(removedItem,1);
}else{
delete patch[removedItem];
}
return objectQuery.set(pointer, patch, obj);
}
objectQuery.wildcard = function(pattern, findOptions, obj, filter){
var points = pattern.split('/');
var pointsLength = points.length;
var vStack = [{obj: obj, level:1, path:''}];
var res = [];
while(vStack.length !== 0){
var node = vStack.pop();
if(node['obj'] instanceof Object && node['obj'] !== null){
var point = points[node.level];
if(point === '*'){
for(var i = 0, keys = Object.keys(node['obj']), _len = keys.length; i < _len; i++){
vStack.push({obj: node['obj'][keys[i]], level: (node.level + 1), path: node['path'] + '/' + keys[i]});
}
}else if(point === '**'){
for(var i = 0, keys = Object.keys(node['obj']), _len = keys.length; i < _len; i++){
vStack.push({obj: node['obj'][keys[i]], level: (node.level), path: node['path'] + '/' + keys[i]});
}
}else{
vStack.push({obj: node['obj'][point], level: (node.level + 1), path: node['path'] + '/' + point});
}
if(point === '**' || node.level === pointsLength){
if(!!findOptions === false || checkSearchOptions(findOptions, node['obj'])){
if( ((filter instanceof Function) && filter(node['obj'], node['path'])) || !(filter instanceof Function) ){
res.push({
path: node['path'],
res: node['obj']
});
}
}
}
}
}
return res;
}
function checkSearchOptions(findOptions, obj){
return findOptions.every(function(findOption){
if(findOption instanceof Object){
var findKey = Object.keys(findOption)[0];
if(findKey === 'url' && findKey in obj){
findOption[findKey] = findOption[findKey]
.charAt(findOption[findKey].length-1) === '/' ?
findOption[findKey].substr(0, findOption[findKey].length-1) : findOption[findKey];
obj[findKey] = obj[findKey]
.charAt(obj[findKey].length-1) === '/' ?
obj[findKey].substr(0, obj[findKey].length-1) : obj[findKey];
}
return findKey in obj && findOption[findKey] === obj[findKey];
}else{
return findOption in obj;
}
});
}
return objectQuery
}));