Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

json-to-constant

alvarin3MIT2.0.0

Take a *.json file and generate a *.ts file containing a constant

json, constant, typescript, fixtures, fixture

readme

json-to-constant

Create constants out of json files to be use as fixtures for unit tests, or however you see fit.

The available command line options are:

  • --source (alias: -s, required) {string} - source folder path
  • --output (alias: -o, optional) {string} - output folder path
  • --uppercase (alias: -u, optional) {boolean} - flag to indicate if constant name should be all uppercase (ex. MY_CONSTANT vs my_constant)

    • If no output is provided source is used as the output folder.

This is a simple node program that takes Json files and create TS files exporting a constant with the json object as the value. It uses the name of the file as the name of the constant, for example:

  • JSON file (people.json)
{
    "name": "John Smith",
    "age": 45,
    "gender": "male"
}
  • Created ts file
    export constant people = {
        'name': 'John Smith',
        'age': 45,
        'gender': 'male'
    };

Using it via npm scripts

{
    "scripts": {
        "fixtures": "json-to-constant -s fixtures -o converted-fixtures -u false"
    }
}