A tool to parse object properties with dynamic replacer.
var replacer = {
name: 'James',
age: 12
};
var source = {
foo: '{{name}} / age {{age}}',
bar: 'staic text',
baz: [
'{{name}}', {{age}}
]
};
var result = require('obj-parser').parse(source, replacer);
expect(result).to.deep.equal({
foo: 'James / age 12',
bar: 'staic text',
baz: [
'James', '12'
]
});
There are some other useful APIs as below:
desc: same as jQuery.isPlainObject
desc: same as jQuery.extend
desc: parse string, replace {{xxx}} with map object or callback function
parseString('name: {{foo}}', {
foo: 'James'
});
parseString('name: {{foo}}', function(match, key) {
return key.toUpperCase(); // key will be 'foo'
});
desc: get prop value from object with prop path
var source = {
foo: {
bar: 'hello'
}
};
getPropByPath(source, 'foo.bar'); //return 'hello'
desc: set prop value from object with prop path
var source = {
foo: {
bar: 'hello'
}
};
setPropByPath(source, 'foo.bar', 'world'); // update 'hello' to 'world'
desc: go throgh all props, and replace the value if it is string.
refer "test/replaceAllStrProp.test.js" for" detail usage.
desc: go throgh all props, and replace the value if it is pase the matchFn.
refer "test/replaceAllMatchProps.test.js" for detail usage.
If you have any questions, feel free to create a new issue.