-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonQuery.js
1185 lines (1131 loc) · 53.5 KB
/
jsonQuery.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(global) {
// 配置变量
var _$_conf_ = {
// 判断模式
MODE_TYPE: {
STRICT: 'strict',
CONTAIN: 'contain'
},
inValidValue: 'ignore', // 用来标记是否为无效的值
};
// 基本工具库
var _$_tool_ = {
/**
* 判断数据类型是否为json数据类型
* @Author dingyang
* @DateTime 2018-04-16
* @param {void} data 数据源
* @return {boolean} 如果是json,返回true,否则返回false
*/
isJson: function (data) {
return (typeof(data) == "object" && Object.prototype.toString.call(data).toLowerCase() == "[object object]") || false;
},
/**
* 判断数据类型是否为array数据类型
* @Author dingyang
* @DateTime 2018-04-16
* @param {void} data 数据源
* @return {boolean} 如果是array,返回true,否则返回false
*/
isArray: function (data) {
return (typeof(data) == "object" && Object.prototype.toString.call(data).toLowerCase() == "[object array]") || false;
},
isInArray: function (arr, data) {
for (var i = 0; i < arr.length; i++) {
if (this.compare(arr[i], data)) {
return true;
}
}
return false;
},
/**
* 返回array||json键长度(个数)
* @Author dingyang
* @DateTime 2018-05-21
* @param {object} data 数据源
* @return {number} 长度
*/
getJsonArrLength: function (data) {
var isJson = this.isJson(data),
isArray = this.isArray(data);
if (!isJson && !isArray) {
return 0;
}
return Object.keys(data).length;
},
/**
* 对象深度拷贝【原始数据不产生变化】
* @Author dingyang
* @DateTime 2018-05-21
* @param {object} obj 数据目标拷贝对象arrary||json
* @return {object} 拷贝后数据
*/
deepCopy: function (obj) {
if (!this.isJson(obj) && !this.isArray(obj)) {
return null;
}
var type = this.isJson(obj) ? {} : [];
for (var key in obj) {
if(typeof obj[key]=='object') {
type[key] = arguments.callee.call(this, obj[key]);
} else {
type[key] = obj[key];
}
}
return type;
},
/**
* 为数组中的json数据添加广度&深度索引值[{id:1,childs:[{id:2}]}] => [{id:1,xIndex: 0, yIndex:0, childs:[{id:2}]}]
* @Author dingyang
* @DateTime 2018-06-27
* @param {[type]} config [description]
* @param {[type]} yindex [description]
* @return {[type]} [description]
*/
formatIndex: function (config, yindex) {
var obj = config.data,
xKey = config.xKey || 'xIndex',
yKey = config.yKey || 'yIndex';
var isJson = this.isJson(obj);
var isArray = this.isArray(obj);
if (!isJson && !isArray) {
return obj;
}
var type = isJson ? {} : [];
var xIndex = 0;
for (var key in obj) {
var keyValue = obj[key];
if (typeof keyValue == 'object') {
if (isArray && this.isJson(keyValue)) {
keyValue[xKey] = xIndex;
keyValue[yKey] = yindex;
}
if (this.isArray(keyValue)) {
yindex++;
}
config.data = keyValue;
type[key] = arguments.callee.call(this, config, yindex);
} else {
type[key] = keyValue;
}
if (isArray) {
xIndex++;
}
}
return type;
},
// 根据指定的key进行
getValueWithKey: function (config) {
var json_arr_data = config.data,
nodeKey = config.key;
var isArrayData = this.isArray(json_arr_data),
isJsonData = this.isJson(json_arr_data),
results = [];
// 首先,进行异常逻辑处理(如果不是json也不是array)
if (!isJsonData && !isArrayData) {
return results;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
// 其次,如果数据是json格式数据{a:1, b:1} || [a, b]
for (var key in json_arr_data) {
var keyValue = json_arr_data[key];
if (this.isJson(keyValue) || this.isArray(keyValue)) {
config.data = keyValue;
results = results.concat(arguments.callee.call(this, config));
}
key = isArrayData ? key * 1 : key;
if (nodeKey === key) {
results.push(keyValue);
}
continue;
}
return results;
},
/**
* 数据比较是否相等【此处不进行引用类型地址比较】
* @Author dingyang
* @DateTime 2018-04-24
* @param {object} data1 数据源之一
* @param {object} data2 数据源之一
* @return {boolean} 如果相等,返回true,否则返回false
*/
compare: function (data1, data2) {
var isJson = (this.isJson(data1) && this.isJson(data2)),
isArray = (this.isArray(data1) && this.isArray(data2));
if (isJson || isArray) {
// 说明data1 || data2都是json_data 或者 array-data
if (this.getJsonArrLength(data1) !== this.getJsonArrLength(data2)) {
return false;
}
for (var key in data1) {
var compareResult = arguments.callee.call(this, data1[key], data2[key]);
if (!compareResult) {
return false;
}
else {
continue;
}
}
return true;
}
else {
return (data1 === data2);
}
},
/**
* 打印日志信息
* @Author dingyang
* @DateTime 2018-04-16
* @param {string} type error warn log三种日志信息类型
* @param {string} info 日志信息
* @return {void} 无
*/
logInfo: function (type, info) {
console.log('错误类型:' + (type || 'error'), '错误信息:'+ (info || 'error info'));
}
};
// 业务公共处理对象
var _$_busi_ = {
toolUtil: _$_tool_,
conf: _$_conf_, // 配置对象
/**
* 判断指定key或者value是否位于对象中]
* @Author dingyang
* @example 几种条件的示例设置
* var result = isContain({data: [1,2], key: null, value: 2}); 如果data数据源是数组,则key代表下标(int类型)返回true
* var result = isContain({data: [1,2], key: 1, value: 2}); 返回true
* var result = isContain({data: [1,2], key: 0, value: 2}); 返回false
* var result = isContain({data: {a:1, b:2}, key: b, value: 2}); 如果data数据源是json,则key代表键值(string类型)返回false
* var result = isContain({data: {a:1, b:2}, key: null, value: 2});返回true
* @DateTime 2018-05-17
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @return {boolean} 如果命中条件,返回true,否则返回false
*/
isContain: function (config) {
var json_arr_data = config.data,
nodeValue = config.value,
nodeKey = config.key;
var isArrayData = this.toolUtil.isArray(json_arr_data),
isJsonData = this.toolUtil.isJson(json_arr_data);
if (!isArrayData && !isJsonData) {
return false;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
for (var key in json_arr_data) {
var compare = false;
var keyValue = json_arr_data[key];
if (nodeKey !== this.conf.inValidValue && nodeValue !== this.conf.inValidValue) {
compare = (this.toolUtil.compare(isQueryArray ? key * 1 : key, nodeKey) && this.toolUtil.compare(keyValue, nodeValue));
}
else if (nodeKey !== this.conf.inValidValue) {
compare = this.toolUtil.compare(isQueryArray ? key * 1 : key, nodeKey);
}
else if (nodeValue !== this.conf.inValidValue) {
compare = this.toolUtil.compare(keyValue, nodeValue);
}
if (compare) return true;
}
return false;
},
_filterCompare: function (modeType, key, keyValue, nodeKey, nodeValue) {
var compare = false;
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
if(modeType === this.conf.MODE_TYPE.CONTAIN) {
if (nodeKey !== this.conf.inValidValue || nodeValue !== this.conf.inValidValue) {
compare = this.isContain({
key: nodeKey,
value: nodeValue,
data: keyValue
});
}
return compare;
}
if (modeType === this.conf.MODE_TYPE.STRICT) {
if (nodeKey !== this.conf.inValidValue && nodeValue !== this.conf.inValidValue) {
compare = (this.toolUtil.compare(isQueryArray ? key * 1 : key, nodeKey) && this.toolUtil.compare(keyValue, nodeValue));
}
else if (nodeKey !== this.conf.inValidValue) {
compare = this.toolUtil.compare(isQueryArray ? key * 1 : key, nodeKey);
}
else if (nodeValue !== this.conf.inValidValue) {
compare = this.toolUtil.compare(keyValue, nodeValue);
}
return compare;
}
return compare;
},
};
// 规则层
var _$_rule_ = {
conf: _$_conf_, // 配置对象
toolUtil: _$_tool_,
_getRules: function (rule) {
var subRules = rule.split(',');
var result = [];
for (var i = 0; i < subRules.length; i++) {
var tmpSubRule = subRules[i];
tmpSubRule = tmpSubRule.replace(/^\s*|\s*$/g,"");
// 验证该子规则是否符合条件(只有一个)
var isValidRule = /\=/g.test(tmpSubRule) && !/\=\s*\=/g.test(tmpSubRule) && /[0-9a-zA-Z\_\u4e00-\u9fa5]+/g.test(tmpSubRule);
if (isValidRule) {
var tmpSubRuleKeyValue = tmpSubRule.split('='),
tmpSubRuleValue = this._formatRule(tmpSubRuleKeyValue[1]),
tmpSubRuleKey = this._formatRule(tmpSubRuleKeyValue[0]);
if (tmpSubRuleValue.flag && tmpSubRuleKey.flag) {
result.push({key: tmpSubRuleKey.data, value: tmpSubRuleValue.data});
}
} else {
this.toolUtil.logInfo('warn', 'rule-analysis: ' + tmpSubRule + ' is invalid');
}
}
return result;
},
// 对规则中的数据进行解析
_formatRule: function (key) {
// 首先判断其是不是字符串
if (/^'\w*'$/g.test(key) || /^"\w*"$/g.test(key)) {
return {flag: true, data: key.substring(1, key.length - 1)};
} else {
try {
return {flag: true, data: JSON.parse(key)};
} catch(e) {
if (key === '') {
return {flag: true, data: this.conf.inValidValue};
} else if (key === 'undefined') {
return {flag: true, data: undefined};
} else {
this.toolUtil.logInfo('error', 'rule-analysis: ' + key + ' is invalid');
return {flag: false, data: ''};
}
}
}
}
};
// 核心方法
var _$_core_ = {
toolUtil: _$_tool_, // 工具库
busiUtil: _$_busi_, // 公共业务模块
conf: _$_conf_, // 配置对象
/**
* 格式化方法[{id:,parentId:}, {id:,parentId:}] => [{id:,childern:[{}]}]
* @Author dingyang
* @example 示例
* var result = formatChildren({data: [{id:1,parentId:null}, {id:2,parentId:1}], parentId: 'parentId', id: 'id'});
* 返回数据为[{id:1,parentId:null,children:[{id:2,parentId:1,children:[]}]}]
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string)} config.parentId 配置项-key
* @param {(number|string)} config.id 配置项-value
* @return {object} 格式化之后的数据格式
*/
_formatChildren: function (config, arr) {
var jsonArr_data = config.data,
nodeParentId = config.parentId,
nodeId = config.id;
var results = arr, tmpResult = [];
for (var i = 0; i < jsonArr_data.length; i++) {
var tmpData = jsonArr_data[i];
var tmpNodeParentId = tmpData[nodeParentId],
tmpNodeId = tmpData[nodeId];
if (!tmpNodeParentId) {
tmpData.children = [];
results.push(tmpData);
continue;
}
// 查找当前节点的父节点是否位于results结果中
var queryCurrentNodes = this._queryNodes({
key: nodeId,
value: tmpNodeParentId,
data: results
});
if (queryCurrentNodes.length > 1) {
this.toolUtil.logInfo('error', 'data-format: id repeat');
return;
}
if (queryCurrentNodes.length === 1) {
// 说明查找到指定父元素节点
var parentNode = queryCurrentNodes[0];
tmpData.children = [];
parentNode.children.push(tmpData);
} else {
// 说明没有查找到指定父元素节点
tmpResult.push(tmpData);
}
}
if (!tmpResult.length) {
return results;
}
config.data = tmpResult;
return arguments.callee.call(this, config, results);
},
/**
* 执行目标对象的前置插入或者后置插入
* @Author dingyang
* @example 严格模式【严格同一级匹配】
* var result = insertBefore({data: [1,2,{a:2}], key: null, value: 2, target: {key: 'b', value: '3'}});
* 解释:数组中第2项符合查找条件,则在其前面插入节点"3"(注:此时数组时,会忽略target中的key值),对象{a:2}也同样符合条件,此在其父对象添加target目标元素,结果为{b:3,a:2};综合以上,则返回[1,'3',2,{b:'3',a:2}]
* @example 包含模式【对象包含查找条件模式】
* var result = insertBefore2({data: [1,2,{a:2}], key: null, value: 2, target: {key: 'b', value: '3'}});
* 解释:数组中第2项符合查找条件,因为此时是兼容模式,则应查找其父节点对象的兄弟节点,因为其父对象为根节点,此处暂时做不操作处理(注意:);另外,对象{a:2}也符合条件,则在其前面插入“2”;综合以上,返回[1,2,‘3’,{a:2}]]
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @param {object} config.target 配置项-target
* @param {(number|string|null|undefined)} target.key 配置项target-key
* @param {void} target.value 配置项target-value
* @param {string} modeType 配置模式(提供两种模式'strict'|'contain')
* @return {object} 返回新的对象集合
*/
_insertBeforeOrAfter: function (config, type, modeType) {
var json_arr_data = config.data,
nodeValue = config.value,
nodeKey = config.key,
targetValue = config.target.value, // 插入目标元素value
targetKey = config.target.key; // 插入目标元素key
var isArrayData = this.toolUtil.isArray(json_arr_data),
isJsonData = this.toolUtil.isJson(json_arr_data);
// 首先,进行异常逻辑处理(如果不是json也不是array)
if (!isJsonData && !isArrayData) {
return json_arr_data;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
var results = isArrayData ? [] : {};
for (var key in json_arr_data) {
var keyValue = json_arr_data[key];
var compare = this.busiUtil._filterCompare(modeType, key, keyValue, nodeKey, nodeValue);
if (compare && type === 'before') {
// 如果命中比对规则
if (isArrayData) {
// 如果数据源是数组,此时需要对新数组进行插入target目标元素
results.push(targetValue);
} else {
results[targetKey] = targetValue;
}
}
if (this.toolUtil.isArray(keyValue) || this.toolUtil.isJson(keyValue)) {
config.data = keyValue;
if (isArrayData) {
results.push(arguments.callee.call(this, config, type, modeType));
} else {
results[key] = arguments.callee.call(this, config, type, modeType);
}
} else {
if (isArrayData) {
results.push(keyValue);
} else {
results[key] = keyValue;
}
}
if (compare && type === 'after') {
// 如果命中比对规则
if (isArrayData) {
// 如果数据源是数组,此时需要对新数组进行插入target目标元素
results.push(targetValue);
} else {
results[targetKey] = targetValue;
}
}
}
return results;
},
/**
* 节点对象删除
* @Author dingyang
* @example 严格模式【严格同一级匹配】
* var result = delete({data: [1,{a:2},[2]], key: null, value: 2}); 返回 [1,{},[]]
* @example 包含模式【对象包含查找条件模式】
* var result = delete2({data: [1,{a:2},[2]], key: null, value: 2}); 返回 [1]
* 解释:数组中第2项{a;2},第3项[2]符合查找条件,因为此时是兼容模式,所以返回[1]
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @param {string} modeType 配置模式(提供两种模式'strict'|'contain')
* @return {object} 返回新的对象集合
*/
_delete: function (config, modeType) {
var compare = this.busiUtil._filterCompare(modeType, null, config.data, config.key, config.value);
if (compare) return null;
return this.__delete(config, modeType);
},
__delete: function (config, modeType) {
var json_arr_data = config.data,
nodeValue = config.value,
nodeKey = config.key;
var isArrayData = this.toolUtil.isArray(json_arr_data),
isJsonData = this.toolUtil.isJson(json_arr_data),
results = [];
// 首先,进行异常逻辑处理(如果不是json也不是array)
if (!isJsonData && !isArrayData) {
return json_arr_data;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
var results = isArrayData ? [] : {};
for (var key in json_arr_data) {
var keyValue = json_arr_data[key];
var compare = this.busiUtil._filterCompare(modeType, key, keyValue, nodeKey, nodeValue);
if (compare) continue;
if (this.toolUtil.isArray(keyValue) || this.toolUtil.isJson(keyValue)) {
config.data = keyValue;
if (isArrayData) {
results.push(arguments.callee.call(this, config, modeType));
} else {
results[key] = arguments.callee.call(this, config, modeType);
}
} else {
if (isArrayData) {
results.push(keyValue);
} else {
results[key] = keyValue;
}
}
}
return results;
},
/**
* 删除兄弟节点
* @Author dingyang
* @example 严格模式【严格同一级匹配】
* var result = deleteAllSiblings({data: [1,2,3], key: null, value: 2}); 返回 [2]
* var result = deleteBeforeSiblings({data: [1,2,3], key: null, value: 2}); 返回 [2,3]
* var result = deleteAfterSiblings({data: [1,2,3], key: null, value: 2}); 返回 [1,2]
* 包含模式【对象包含查找条件模式】同之前匹配模式
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @param {string} modeType 配置模式(提供两种模式'strict'|'contain')
* @param {string} type 删除规则(提供三种模式'before'|'after'|'all')
* @return {object} 返回新的对象集合
*/
_deleteSiblings: function (config, type, modeType) {
var compare = this.busiUtil._filterCompare(modeType, null, config.data, config.key, config.value);
if (compare) return config.data;
return this.__deleteSiblings(config, type, modeType);
},
__deleteSiblings: function (config, type, modeType) {
var json_arr_data = config.data,
nodeValue = config.value,
nodeKey = config.key;
var isArrayData = this.toolUtil.isArray(json_arr_data),
isJsonData = this.toolUtil.isJson(json_arr_data),
results = [];
// 首先,进行异常逻辑处理(如果不是json也不是array)
if (!isJsonData && !isArrayData) {
return json_arr_data;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
var results = isArrayData ? [] : {};
for (var key in json_arr_data) {
var keyValue = json_arr_data[key];
var compare = this.busiUtil._filterCompare(modeType, key, keyValue, nodeKey, nodeValue);
if ((type === 'beforeAll' || type === 'all') && compare) {
results = isArrayData ? [] : {};
};
if (this.toolUtil.isArray(keyValue) || this.toolUtil.isJson(keyValue)) {
config.data = keyValue;
if (isArrayData) {
results.push(arguments.callee.call(this, config, type, modeType));
} else {
results[key] = arguments.callee.call(this, config, type, modeType);
}
} else {
if (isArrayData) {
results.push(keyValue);
} else {
results[key] = keyValue;
}
}
if ((type === 'afterAll' || type === 'all') && compare) break;
}
return results;
},
/**
* 指定位置节点替换
* @Author dingyang
* @example 严格模式【严格同一级匹配】
* var result = replace({data: [1,{a:2}], key: null, value: 2, target: {key: 'b', value: '3'}});
* 解释:数组中{a:2}符合条件,所以返回[1,{b:3}]
* @example 包含模式【对象包含查找条件模式】
* var result = replace2({data: [1,{a:2}], key: null, value: 2, target: {key: 'b', value: '3'}});
* 解释:包含模式下返回[1,3] 此时因为是在数组中执行replace,所以会忽略target中的key
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @param {object} config.target 配置项-target
* @param {(number|string|null|undefined)} target.key 配置项target-key
* @param {void} target.value 配置项target-value
* @param {string} modeType 配置模式(提供两种模式'strict'|'contain')
* @return {object} 返回新的对象集合
*/
_replace: function (config, modeType) {
var compare = this.busiUtil._filterCompare(modeType, null, config.data, config.key, config.value);
if (compare) return config.target.value;
return this.__replace(config, modeType);
},
__replace: function (config, modeType) {
var json_arr_data = config.data,
nodeValue = config.value,
nodeKey = config.key,
targetKey = config.target.key;
targetValue = config.target.value;
var isArrayData = this.toolUtil.isArray(json_arr_data),
isJsonData = this.toolUtil.isJson(json_arr_data),
results = [];
// 首先,进行异常逻辑处理(如果不是json也不是array)
if (!isJsonData && !isArrayData) {
return json_arr_data;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
var results = isArrayData ? [] : {};
for (var key in json_arr_data) {
var keyValue = json_arr_data[key];
var compare = this.busiUtil._filterCompare(modeType, key, keyValue, nodeKey, nodeValue);
if (compare && isArrayData && targetValue !== this.conf.inValidValue) {
results.push(targetValue);
continue;
} else if (compare && targetKey !== this.conf.inValidValue) {
if (targetValue !== this.conf.inValidValue) {
results[targetKey] = targetValue;
continue;
} else {
key = targetKey;
}
}
if (this.toolUtil.isArray(keyValue) || this.toolUtil.isJson(keyValue)) {
config.data = keyValue;
if (isArrayData) {
results.push(arguments.callee.call(this, config, modeType));
} else {
results[key] = arguments.callee.call(this, config, modeType);
}
} else {
if (isArrayData) {
results.push(keyValue);
} else {
results[key] = keyValue;
}
}
}
return results;
},
/**
* 从对象数据中查找符合条件的节点对象集合
* @Author dingyang
* @example 示例
* var result = queryNodes({data: [1,2,{a:2}], key: null, value: 2}); 此处有两处符合条件,返回[[1,2],{a:2}]
* var result = queryNodes({data: [1,2,2], key: null, value: 2}); 此处有两处符合条件,返回[[1,2,2],[1,2,2]]
* var result = queryNodes({data: [1,2,{a:2}], key: 1, value: 2}); 只有一处符合条件,返回[[1,2,{a:2}]]
* var result = queryNodes({data: {a:2,b:3}, key: null, value: 2}); 返回[{a:2,b:3}]
* var result = queryNodes({data: {a:2,b:3}, key: 'a', value: 2}); 返回[{a:2,b:3}]
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @return {array} 符合条件的节点对象集合
*/
_queryNodes: function (config) {
var json_arr_data = config.data,
nodeValue = config.value,
nodeKey = config.key;
var isArrayData = this.toolUtil.isArray(json_arr_data),
isJsonData = this.toolUtil.isJson(json_arr_data),
results = [];
// 首先,进行异常逻辑处理(如果不是json也不是array)
if (!isJsonData && !isArrayData) {
return results;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
// 其次,如果数据是json格式数据{a:1, b:1} || [a, b]
for (var key in json_arr_data) {
var keyValue = json_arr_data[key];
if (this.toolUtil.isJson(keyValue) || this.toolUtil.isArray(keyValue)) {
config.data = keyValue;
results = results.concat(arguments.callee.call(this, config));
}
var compare = this.busiUtil._filterCompare(this.conf.MODE_TYPE.STRICT, key, keyValue, nodeKey, nodeValue);
if (compare) {
results.push(isQueryArray ? keyValue : json_arr_data);
}
continue;
}
return results;
},
/**
* 查找父节点对象集合,返回二维数组
* @Author dingyang
* @example 示例
* var result = queryParents({data: {a:{c1:3},b:{c1:3}}, key: 'c1', value: 3});
* 返回[[{a:{c1:1},b:{c1:3}}, {c1:3}], [{a:{c1:1},b:{c1:3}}, {c1:3}]]
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @return {array} 符合条件的节点对象集合
*/
_queryParents: function (config) {
var results = [];
var originData = config.data;
var invalidNodes = this._queryNodes(config);
for (var i = 0; i < invalidNodes.length; i++) {
config.data = originData;
this.cIndex = 0;
results.push(this.__queryParents(config, i));
}
return results;
},
__queryParents: function (config, index) {
var json_arr_data = config.data,
nodeValue = config.value,
nodeKey = config.key;
var isArrayData = this.toolUtil.isArray(json_arr_data),
isJsonData = this.toolUtil.isJson(json_arr_data),
results = [];
// 首先,进行异常逻辑处理(如果不是json也不是array)
if (!isJsonData && !isArrayData) {
return results;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
// 其次,如果数据是json格式数据{a:1, b:1} || [a, b]
for (var key in json_arr_data) {
var keyValue = json_arr_data[key];
var compare = this.busiUtil._filterCompare(this.conf.MODE_TYPE.STRICT, key, keyValue, nodeKey, nodeValue);
if (compare) {
if (this.cIndex === index) {
// 说明找到当前指定索引位置的元素
results.push(json_arr_data);
break;
} else {
// 说明当前匹配的元素不是指定索引的元素,进行计数+1
this.cIndex = this.cIndex + 1;
}
}
else if (this.toolUtil.isArray(keyValue) || this.toolUtil.isJson(keyValue)) {
config.data = keyValue;
var tmpResults = arguments.callee.call(this, config, index);
if (tmpResults.length) {
results = [json_arr_data].concat(tmpResults);
break;
} else {
continue;
}
}
else {
continue;
}
}
return results;
},
/**
* 查找指定条件的父节点要素,类似queryParents,但不同于queryParents,返回结果要么0个要么1个
* @Author dingyang
* @example 示例
* var result = queryClosest({data: [1,2,{a:2,b:{a:5}}], key: 'a', value: 5, target: {key: 'a', value: 2}});
* 返回[[{a:2,b:{a:5}}]]
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @param {object} config.target 配置项-target
* @param {(number|string|null|undefined)} target.key 配置项target-key
* @param {void} target.value 配置项target-value
* @return {array} 符合条件的节点对象集合
*/
_queryClosest: function (config) {
var queryParents = this._queryParents(config);
if (!queryParents.length) return [];
var closestTarget = config.target;
var results = [];
for (var i = 0; i < queryParents.length; i++) {
var tmpResult = [];
for (var j = (queryParents[i].length - 1); j >=0 ; j--) {
var cNode = queryParents[i][j];
var compare = this.busiUtil.isContain({
data: cNode,
key: closestTarget.key,
value: closestTarget.value
})
if (compare) {
tmpResult.push(cNode);
break;
}
}
results.push(tmpResult);
}
return results;
},
/**
* 查找兄弟节点对象集合
* @Author dingyang
* @example 严格模式【严格同一级匹配】
* var result = querySiblings({data: [1,2,{a:2}], key: null, value: 2});
* 解释:数组中第2项符合查找条件,则其兄弟节点为[1,{a:2}]; 对象{a:2}也是符合查找条件,则其兄弟节点为空[](因为对象只有a:2这一项);综合上述,返回[[1,{a:2}], []]
* @example 包含模式【对象包含查找条件模式】
* var result = querySiblings2({data: [1,2,{a:2}], key: null, value: 2});
* 解释:数组中第2项符合查找条件,因为此时是兼容模式,则应查找其父节点对象的兄弟节点,因为其父对象为根节点,此处暂时做不操作处理(注意:);另外,对象{a:2}也符合条件,则对应兄弟节点为[1,2];综合以上,返回[[1,2]]
* @DateTime 2018-04-24
* @param {Object} config 配置项
* @param {string} config.data 配置项-数据源
* @param {(number|string|null|undefined)} config.key 配置项-key
* @param {void} config.value 配置项-value
* @param {string} modeType 配置模式(提供两种模式'strict'|'contain')
* @return {array} 符合条件的节点对象集合
*/
_querySiblings: function (config, type, modeType) {
this.tmpSiblings = [];
this.__querySiblings(config, type, modeType);
return this.tmpSiblings;
},
__querySiblings: function (config, type, modeType) {
var json_arr_data = config.data,
nodeValue = config.value,
nodeKey = config.key;
var isArrayData = this.toolUtil.isArray(json_arr_data),
isJsonData = this.toolUtil.isJson(json_arr_data),
results = [],
subData = []; // 用以存储下一级数据集合
// 首先,进行异常逻辑处理(如果不是json也不是array)
if (!isJsonData && !isArrayData) {
return results;
}
var isQueryArray = ((typeof nodeKey) === 'number') ? true : false;
var findFlag = false;
// 其次,如果数据是json格式数据{a:1, b:1} || [a, b]
for (var key in json_arr_data) {
var keyValue = json_arr_data[key];
results.push(keyValue);
var compare = this.busiUtil._filterCompare(modeType, key, keyValue, nodeKey, nodeValue);
if (compare) {
findFlag = true;
results.pop();
results.push('|'); // 此处插入区分标记,区分前兄弟节点/后兄弟节点
}
else if (this.toolUtil.isArray(keyValue) || this.toolUtil.isJson(keyValue)) {
subData.push(keyValue);
}
}
if (findFlag) {
var newResults = [];
if (type === 'all') {
newResults = results.filter(function (item) {
return item !== '|'
});
} else if (type === 'beforeAll') {
newResults = results.slice(0, results.indexOf('|'));
} else if (type === 'afterAll') {
newResults = results.slice(results.indexOf('|') + 1);
} else if (type === 'before') {
newResults = results.slice(results.indexOf('|') - 1, results.indexOf('|'));
} else if (type === 'after') {
newResults = results.slice(results.indexOf('|') + 1, results.indexOf('|') + 2);
}
this.tmpSiblings.push(newResults);
}
if (subData.length) {
for (var i = 0; i < subData.length; i++) {
config.data = subData[i];
arguments.callee.call(this, config, type, modeType);
}
}
}
};
// 统一暴露层
var _$_ = {
conf: _$_conf_, // 配置对象
ruleUtil: _$_rule_, // 规则解析
toolUtil: _$_tool_, // 基本工具库(暂时暴露)
busiUtil: _$_busi_, // 公共业务模块
core: _$_core_, // 核心对象
/**
* 用来进行规则拦截处理分发
* @Author dingyang
* @DateTime 2018-05-28
* @param {[type]} params [description]
* @param {[type]} funcName 执行函数名
* @param {[type]} type 'format' || 'query'
* @return {[type]} [description]
*/
_transferBelt: function (params, funcName, type) {
var rule = params.rule;
if (rule) {
var rules = this.ruleUtil._getRules(rule);
var result = type === 'format' ? params.data : [];
for (var i = 0; i < rules.length; i++) {
if (type === 'format') {
result = this[funcName]({
data: result,
key: rules[i]['key'],
value: rules[i]['value'],
target: params.target || {}
})
}
if (type === 'query') {
result.push(this[funcName]({
data: params.data,
key: rules[i]['key'],
value: rules[i]['value'],
target: params.target || {}
}));
}
}
return result;
} else {
return this[funcName](params);
}
},
formatChildren: function (config) {
return this.core._formatChildren(config, []);
},
insert: function (target, modeType, insertType) {
if (!this.rule) {
console.log('rule is not exits');
return;
}
this._insertType = this.toolUtil.isInArray(['after', 'before'], insertType) ? insertType : 'before';
// 模式参数设定
this._modeType = this.toolUtil.isInArray([this.conf.MODE_TYPE.STRICT, this.conf.MODE_TYPE.CONTAIN], modeType) ? modeType : this.conf.MODE_TYPE.CONTAIN;
// 执行查找
var datas = this.data;
var totalResults = [];
for (var i = 0; i < datas.length; i++) {
var tmpResults = this._transferBelt({
data: datas[i],
rule: this.rule,
target: target
}, '_insert', 'format');
totalResults.push(tmpResults);
}
this.setData(totalResults);
this.setResult(totalResults[0] || null);
return this;
},
_insert: function (config) {
return this.core._insertBeforeOrAfter(config, this._insertType, this._modeType);
},
// 元素替换
replace: function (target, modeType) {
if (!this.rule) {
console.log('rule is not exits');
return;
}
// 模式参数设定
this._modeType = this.toolUtil.isInArray([this.conf.MODE_TYPE.STRICT, this.conf.MODE_TYPE.CONTAIN], modeType) ? modeType : this.conf.MODE_TYPE.CONTAIN;
// 删除逻辑
var datas = this.data;
var totalResults = [];
for (var i = 0; i < datas.length; i++) {
var tmpResults = this._transferBelt({
data: datas[i],
rule: this.rule,
target: target
}, '_replace', 'format');
totalResults.push(tmpResults);
}
this.setData(totalResults);
this.setResult(totalResults[0] || null);
return this;
},
_replace: function (config) {
return this.core._replace(config, this._modeType);
},
// 元素删除
delete: function (modeType) {
if (!this.rule) {
console.log('rule is not exits');
return;
}
// 模式参数设定
this._modeType = this.toolUtil.isInArray([this.conf.MODE_TYPE.STRICT, this.conf.MODE_TYPE.CONTAIN], modeType) ? modeType : this.conf.MODE_TYPE.CONTAIN;
// 删除逻辑
var datas = this.data;
var totalResults = [];
for (var i = 0; i < datas.length; i++) {
var tmpResults = this._transferBelt({
data: datas[i],
rule: this.rule
}, '_delete', 'format');
totalResults.push(tmpResults);
}
this.setData(totalResults);
this.setResult(totalResults[0] || null);
return this;
},
_delete: function (config) {
return this.core._delete(config, this._modeType);
},
deleteSiblings: function (modeType, deleteType) {
if (!this.rule) {
console.log('rule is not exits');
return;