Coder Social home page Coder Social logo

terran-source / interpolate-json Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 2.0 582 KB

Interpolate a Javascript Object or string with json - Advanced

Home Page: https://www.npmjs.com/package/interpolate-json

License: MIT License

JavaScript 100.00%
interpolation json javascript string-interpolation json-interpolation nodejs

interpolate-json's People

Contributors

turn-a-round avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

zitoloco kirbo

interpolate-json's Issues

Interpolation does not seem to be working

I'm probably doing something wrong but here is my code below

`inputData[itemIndex] = {
json: {
timestamp: '2023-03-17T17:20:42.093+00:00',
'Readable date': 'March 17th 2023, 5:20:42 pm',
'Readable time': '5:20:42 pm',
'Day of week': 'Friday',
Year: '2023',
Month: 'March',
'Day of month': '17',
Hour: '17',
Minute: '20',
Second: '42',
Timezone: 'GMT +00:00'
}
}

const paramterValue = [ { value1: '{{json.Minute}}', operation: 'equal', value2: '20' } ]

const paramterNew = interpolation.expand(parameterValue, inputData[itemIndex], {prefix: "{{",suffix: "}}",});
`

This does not replace the {{json.Minute}} template with the value 20

Any help greatly apreciated

It seems that some strings do not get interpolated when there is only one value

Describe the bug
reading a json string values substitution with only one entry seems to fail, returning the ${variable} within the original string.
Using two or more works as expected, even if the second variable does not exist within the values json

To Reproduce
Steps to reproduce the behavior:

  1. Code snippet:

    `'const {interpolation} = require('interpolate-json');
    // interpolation.debug();

var json1 = {variable: "test ${one}, ${two}, ${three}"};
//this works as expected
console.log("result 1");
console.log(interpolation.expand(json1, {one : "test1", two: "test2", three: "test3"}));

//this one fails
console.log("result 2");
console.log(interpolation.expand("this is ${language}" , {language: "english"}));

//same as above, just adding a second value, it works
console.log("result 3");
console.log(interpolation.expand("this is ${language} ${nowitworks}" , {language: "english", nowitworks:"ofcourse"}));

//correctly supresses the ${a} value and correctly replaces ${thing}
console.log("result 4");
console.log(interpolation.expand("${thing} it works without value ${a}" , {thing: "there is only one"}));
'2. Run command:$ ....3. Error:Exception:...`

Expected behavior
console.log(interpolation.expand("this is ${language}" , {language: "english"}));
should output
"this is english"

Screenshots
image

Environment (please complete the following information):

  • OS:
    linux mint 19.3 64bit
  • Node.js version
    v13.9.0
  • Version
    "interpolate-json": "^2.0.1"

Additional context
nothing else, this library is exactly what i need but its not working for that specific case.

workaround: just add a ${x} at the end as a white space:
interpolation.expand( localeStrings.settinglanguage+"${a}", {language: lang_name} )

✍️Interpolation is not working with dashes.

Describe the bug
The interpolation is not working when using dashes.

To Reproduce
Steps to reproduce the behavior:
interpolation.expand('http://${my-domain}/whatever/', {'my-domain': 'example.com'})
The output is still "http://${my-domain}/whatever/"

Expected behavior
I would expect to have "${my-domain}" replaced by "example.com".

Environment (please complete the following information):

  • OS: MacOS 11.1
  • Node.js version 14
  • Version 2.1.3

✍️ Support for interpolation on arrays

The expand method is not able to replace templates located within strings nested in arrays. Support seems to be only for JSON objects that don't have any data nested in arrays.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.