-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (96 loc) · 2.61 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
/**
* Created on 2016/5/17.
* @fileoverview 请填写简要的文件说明.
* @author joc (Firstname Lastname)
*/
"use strict";
/**
*
* 获取对象中指定路径对应的数据
* @param obj {?Object|string|Array=} 需要查找的对象,默认为__global__
* @param paths {?string|Array=} 需要查找的路径,格式为"path1.0.path2"
* @returns {*}
*/
let getDataByPath = function (obj, paths) {
var fn = function (path) {
var res = obj;
path.split('.').forEach(function (p) {
res = (res || {})[p];
});
return res;
};
if (!paths || !paths.length) {
throw new Error('getDataByPath', 'paths can not be empty.');
}
if (typeof paths === 'string') {
return fn(paths);
}
return paths.map(function (path) {
return fn(path);
});
};
/**
*
* 指定一个或多个路径,从传入的对象中依次查找数据,直到找到一项不是undefined类型的数据结果
* @param obj {?Object|string|Array=} 需要查找的对象,默认为__global__
* @param paths {?string|Array=} 需要查找的路径,格式为"path1.0.path2"
* @returns {*}
*/
let searchUntilDefined = function (obj, paths) {
var res;
if (typeof paths === 'string') {
paths = [paths];
}
for (var i = 0; i < paths.length; i++) {
res = getDataByPath(obj, paths[i]);
if (typeof res !== 'undefined') {
return res;
}
}
};
/**
* 从指定的对象中搜索满足条件的键值对,将其值替换
* @param obj {!object}
* @param search {!Function}
* @param replace {?Function|Object=} 如果没有传入,则删除指定的key
*/
let searchAndReplace = function (obj, search, replace) {
if (!obj) {
return;
}
check(search, Function);
let del = arguments.length === 2;
traverse(obj, function (val, key, ctx) {
if (search(val, key, ctx)) {
if (del) {
delete ctx[key];
return;
}
ctx[key] = _.isFunction(replace) ? replace(val, key, ctx) : replace;
}
});
};
/**
* 遍历指定的对象,并对每个键值对执行一些操作
* @param obj {!object}
* @param handle {!Function}
*/
let traverse = function (obj, handle) {
if (!obj) {
return;
}
check(handle, Function);
_.each(obj, function (val, key, ctx) {
if (val && _.isObject(val) && !_.isDate(val)) {
traverse(val, handle);
return;
}
handle(val, key, ctx);
});
};
module.exports = {
getDataByPath,
searchUntilDefined,
searchAndReplace,
traverse
};