Last active
June 9, 2018 08:15
-
-
Save zenHeart/6ccb87202c1b961ab63e54212334c056 to your computer and use it in GitHub Desktop.
字符串模式读取对象
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.parseKey = parseKey; | |
exports.parse = parse; | |
/** | |
* 采用字符串模式读取对象键值. | |
例如: | |
let object = { | |
b: { c: 4 }, d: [{ e: 5 }, { e: 6 }], | |
}; | |
function parse(obj, parseStr) { | |
// code | |
} | |
console.log(parse(object, 'b.c') == 4); //true | |
console.log(parse(object, 'd[0].e') == 5); //true | |
console.log(parse(object, 'd.0.e') == 5); //true | |
console.log(parse(object, 'd[1].e') == 6); //true | |
console.log(parse(object, 'd.1.e') == 6); //true | |
console.log(parse(object, 'f') == 'undefined'); //true | |
* @param {Object} obj 对象 | |
* @param {String} keyStr 对象键的索引字符串 | |
*/ | |
function parse(obj,keyStr) { | |
if(obj && typeof obj === 'object' && typeof keyStr === 'string') { | |
let keyArr = parseKey(keyStr),res = obj ; | |
while(keyArr.length && res) { | |
res = res[keyArr.shift()]; | |
} | |
return res===undefined?'undefined':res; | |
}else { | |
throw new Error('input error!'); | |
} | |
} | |
/** | |
* 将索引字符串拆分为键的数组 | |
* @param {String} keyStr 索引字符串 | |
*/ | |
function parseKey(keyStr) { | |
let keyDelimiter = /\w+\b/g; | |
let keyArr = [],res; | |
while ((res = keyDelimiter.exec(keyStr)) !== null) { | |
keyArr.push(res[0]); | |
} | |
return keyArr; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {parseKey,parse} = require('./parse'); | |
describe("parse test",function() { | |
it("parseKey",function() { | |
let testData = [ | |
{ | |
input:'b.c', | |
expect:['b','c'], | |
}, | |
{ | |
input:'12.df[12].df.we', | |
expect:['12','df','12','df','we'], | |
}, | |
{ | |
input:'a_sd.c', | |
expect:['a_sd','c'], | |
}, | |
{ | |
input:'f', | |
expect:['f'], | |
}, | |
{ | |
input:'fd[1][2][3].g[3][12][6]', | |
expect:['fd','1','2','3','g','3','12','6'], | |
} | |
] | |
testData.forEach((ele) => { | |
expect(parseKey(ele.input)).toEqual(ele.expect); | |
}) | |
}); | |
it("parse",function() { | |
let object = { | |
b: { c: 4 }, d: [{ e: 5 }, { e: 6 }], | |
}; | |
let testData = [ | |
{ | |
input:'b.c', | |
expect:4, | |
}, | |
{ | |
input:'d[0].e', | |
expect:5, | |
}, | |
{ | |
input:'d.0.e', | |
expect:5, | |
}, | |
{ | |
input:'d[1].e', | |
expect:6 | |
}, | |
{ | |
input:'d.1.e', | |
expect:6 | |
}, | |
{ | |
input:'f', | |
expect:'undefined' | |
}, | |
{ | |
input:'f.d.f', | |
expect:'undefined' | |
} | |
]; | |
testData.forEach((ele) => { | |
expect(parse(object,ele.input)).toEqual(ele.expect); | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment