vars-json

Parses variables in your Json files. Very useful for object oriented things

Usage no npm install needed!

<script type="module">
  import varsJson from 'https://cdn.skypack.dev/vars-json';
</script>

README

Vars Json

Heroes

Vars Json is a simple Library to use Variables in json files
For Example:

// Loading a json file in javascript
const VarsJson = require('vars-json');
const someValue = VarsJson.parse('yourPath', 'yourSearchString');

Your also can use your own json element and load it in the api

const json = JSON.parse(jsonText);
const someValue = VarsJson.parse(json, 'yourSearchString');

Search String

The search String works easily and efficiently

const configPath = VarsJson.parse('paths.json', "%Files~Config~Main%");

The ~ stands for a . in javascript json.

The two percent letters stands for the Expression that this is a Variable. You can write also other things in the search parameter, the api will ignore this.

For Example you point with the api on this json file:

{
  "Directories": {
    "Configs": "%Directories~Current%/configs",
    "Current": "."
  },
  "Files": {
    "Config": {
      "Main": "%Directories~Configs%/mainConfig.json"
    }
  }
}

Return Value: ./configs/mainConfig.json

const libraryPath = VarsJson.parse('paths.json', "%Directories~LibraryPath%/library.js");
{
  "Directories": {
    "LibraryPath": "%Directories~OthersDirectory%/libraries",
    "OthersDirectory": "%THIS%/others"
  },
  "THIS": "."
}

Return Value: ./others/libraries/library.js

const greetingsString = VarsJson.parse("greetings.json", "%Text%");
{
  "GREETINGS": {
    "1": "HEY",
    "2": "HO",
    "3": "HELLO"
  },
  "GOODBYES": {
    "1": "BYE",
    "2": "BYE BYE",
    "3": "HAVE A GOOD ONE",
    "4": "SEE YOU LATER"
  },
  "Text": "%GREETINGS~1% %GREETINGS~2% %GREETINGS~3% %GOODBYES~1% %GOODBYES~2% %GOODBYES~3% %GOODBYES~4%"
}

Return Value: HEY HO HELLO BYE BYE BYE HAVE A GOOD ONE SEE YOU LATER