jsonPlaceholderReplacer
Lightweight yet really powerful typescript library/cli to replace placeholders in an javascript object/JSON.
By default, all you have to do is to use double curly brackets {{placeholderKey}} or angle brackets <<placeholderKey>>, interchangeably, to identify the placeholder.
Don't worry, if you don't like these default placeholders you can create your own.
CLI usage
json-placeholder-replacer replaceableFilename [...variableMaps]
Example
json-placeholder-replacer [replaceable.json](/rep) [variable.map](/map)
Would result
cat replaceable.json
# {
# "curly": "{{key}}",
# "angle": "<<key>>"
# }
cat variable.map:
# {
# "key": 10,
# "not-mapped": 20
# }
json-placeholder-replacer replaceable.json variable.map
# {
# "curly": 10,
# "angle": 10,
# "not-mapped": 20
# }
Library usage
As simples as:
import { JsonPlaceholderReplacer } from "json-placeholder-replacer";
const placeHolderReplacer = new JsonPlaceholderReplacer();
placeHolderReplacer.addVariableMap({
key: 100,
otherKey: 200,
});
const afterReplace = placeHolderReplacer.replace({
replaceable: "{{key}}",
otherReplaceableWithSameKey: "<<key>>",
otherReplaceable: "{{otherKey}}",
});
You can replace the default placeholders with some as cool as you want
const placeHolderReplacer = new JsonPlaceholderReplacer({
delimiterTags: [{ begin: "@{{-", end: "-}}@" }],
});
placeHolderReplacer.addVariableMap({
key: "nice",
});
const afterReplace = placeHolderReplacer.replace({
replaceable: "@{{-key-}}@",
});
It's also possible to add more than one variables map
placeHolderReplacer.addVariableMap({
firstMapKey: "1",
});
placeHolderReplacer.addVariableMap({
secondMapKey: 2,
});
const afterReplace = placeHolderReplacer.replace({
replaceable: "{{firstMapKey}}",
otherReplaceable: "<<secondMapKey>>",
});
And the last added maps have higher priority, so
placeHolderReplacer.addVariableMap({
id: "lowerPriority",
});
placeHolderReplacer.addVariableMap({
id: "higherPriority",
});
const afterReplace = placeHolderReplacer.replace({
replaceable: "{{id}}",
});
It keeps original variable types. So, if, in the map, a variable is boolean/string/number/object when it's replaced, it remains as boolean/string/number/object
placeHolderReplacer.addVariableMap({
booleanKey: true,
stringKey: "string",
numberKey: 10,
objectKey: {
inner: "inner",
},
});
const afterReplace = placeHolderReplacer.replace({
booleanReplaceable: "{{booleanKey}}",
stringReplaceable: "{{stringKey}}",
numberReplaceable: "{{numberKey}}",
objectReplaceable: "{{objectKey}}",
});
Just to make it clearer, it does not replace the placeholder Key
placeHolderReplacer.addVariableMap({
key: "someValue",
});
const afterReplace = placeHolderReplacer.replace({
"{{key}}": "value",
});
And, of course, it handles array substitution as well
placeHolderReplacer.addVariableMap({
key: 987,
objectReplaceable: {
inner: "inner"
}
});
const afterReplace = placeHolderReplacer.replace({
array: ["string", "{{objectReplaceable}}", {{key}}]
})
Want to get nested elements? Go for it
placeHolderReplacer.addVariableMap({
key: {
nested: "value",
},
});
const afterReplace: any = placeHolderReplacer.replace({
replaceable: "<<key.nested>>",
});
This feature allows you to have default values in case you don't have them mapped
placeHolderReplacer.addVariableMap({
key: "value",
});
const afterReplace: any = placeHolderReplacer.replace({
replaceable: "<<not-found-key:default-value>>",
});
Of course, you can also change what is the default value separator (defaults to ':')
const placeHolderReplacer = new JsonPlaceholderReplacer({
defaultValueSeparator: "=",
});
placeHolderReplacer.addVariableMap({
key: "value",
});
const afterReplace: any = placeHolderReplacer.replace({
replaceable: "<<not-found-key=default-value>>",
});