refactor: re-implement idx (safe getter) & add test

This commit is contained in:
endiliey 2018-09-06 00:24:26 +08:00
parent 4bc010bfcc
commit 391a8b5500
2 changed files with 48 additions and 2 deletions

View file

@ -29,8 +29,13 @@ function getPath(pathStr, cleanUrl = false) {
: removeExtension(pathStr);
}
function idx(target, path) {
return path.reduce((obj, key) => obj && obj[key], target);
function idx(target, keyPaths) {
return (
target &&
(Array.isArray(keyPaths)
? keyPaths.reduce((obj, key) => obj && obj[key], target)
: target[keyPaths])
);
}
function getGitLastUpdated(filepath) {