npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@syook/json-placeholder-replacer

v1.0.37

Published

Javascript/Typescript library/cli to replace placeholders in an javascript object

Downloads

36

Readme

jsonPlaceholderReplacer

npm version build status Maintainability Greenkeeper badge

Lightweight yet really powerful typescript library/cli to replace placeholders in an javascript object. 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 variable.map

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}}"
})

// afterReplace = {
//    replaceable: 100,
//    otherReplaceableWithSameKey: 100,
//    otherReplaceable: 200
// }

It's possible to replace the default placeholders with some as cool as you want.

const placeHolderReplacer = new JsonPlaceholderReplacer({begin: '@{{-', end: '-}}@'});
placeHolderReplacer.addVariableMap({
    key: "nice"
});
const afterReplace = placeHolderReplacer.replace({
    replaceable: "@{{-key-}}@",
})

// afterReplace = {
//    replaceable: "nice",
// }

It's possible to add more than one variables map.

placeHolderReplacer.addVariableMap({
    firstMapKey: "1"
});
placeHolderReplacer.addVariableMap({
    secondMapKey: 2
});
const afterReplace = placeHolderReplacer.replace({
    replaceable: "{{firstMapKey}}",
    otherReplaceable: "<<secondMapKey>>"
})

// afterReplace = {
//    replaceable: "1",
//    otherReplaceable: 2
// }

And the last added maps have higher priority, so:

placeHolderReplacer.addVariableMap({
    id: "lowerPriority"
});
placeHolderReplacer.addVariableMap({
    id: "higherPriority"
});
const afterReplace = placeHolderReplacer.replace({
    replaceable: "{{id}}"
})

// afterReplace = {
//    replaceable: "higherPriority"
// }

It keeps original variable types. So, if, in the map, a variable is boolean/string/number/object when it's replaced, it still is 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}}"
})

// afterReplace = {
//    booleanReplaceable: true,
//    stringReplaceable: "string",
//    numberReplaceable: 10,
//    objectReplaceable: {
//      inner: "inner"
//    }
// }

Just to make it clear, it does not replace the placeholder Key:

placeHolderReplacer.addVariableMap({
    key: "someValue"
});
const afterReplace = placeHolderReplacer.replace({
    "{{key}}": "value"
})
// afterReplace = {
//    "{{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}}]
})

// afterReplace = {
//    array: ["string", {
//                        inner: "inner"
//                      }, 987]
// }

Want to get nested elements? Go for it!

placeHolderReplacer.addVariableMap({
    key: {
        nested: "value"
    }
});
const afterReplace: any = placeHolderReplacer.replace({
    replaceable: "<<key.nested>>"
});

// afterReplace = {
//    replaceable: "value"
// }