Created
March 4, 2016 08:07
-
-
Save countless-integers/7ff41dc98409e2ee1e0e to your computer and use it in GitHub Desktop.
object values elipsis using lodash
This file contains 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
if (typeof _ !== 'function') { | |
throw new Error('This is only intended for lodash use'); | |
} | |
// "test" object | |
var obj = { | |
name: { | |
last: "Hello", | |
nofd: ['nope', 'hoope'], | |
}, | |
nope: { | |
kaka: "sraka", | |
123: 'asf', | |
ma: "laka", | |
jojo: { | |
dojo: { | |
japa: { | |
lolo: "nanna" | |
} | |
} | |
} | |
} | |
}; | |
function objectElipsis(object, blacklist, elipsisText) { | |
elipsisText = elipsisText || '<...>'; | |
if (_.isString(blacklist)) { | |
blacklist = [blacklist]; | |
} | |
if (!_.isArray(blacklist)) { | |
throw new Error('`blacklist` should be an array of indexes to ellipsise or string with index name.'); | |
} | |
_.transform(object, function (obj, value, index) { | |
if (_.contains(blacklist, index)) { | |
obj[index] = elipsisText; | |
} else if (_.isObject(value)) { | |
objectElipsis(value, blacklist, elipsisText); | |
} | |
}, object); | |
return object; | |
} | |
console.info("===>", objectElipsis(obj, ['lolo', 'kaka'])); | |
console.info("===>", objectElipsis(obj, 'last')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment