项目作者: miraclx

项目描述 :
NodeJS String Variable Parser
高级语言: JavaScript
项目地址: git://github.com/miraclx/stringd.git
创建时间: 2019-02-19T04:46:45Z
项目社区:https://github.com/miraclx/stringd

开源协议:Apache License 2.0

下载


STRING-D

NodeJS String Variable Parser

NPM Version
NPM Downloads

Create parsable strings using template formats by argument mapping.

Similar to printf but supports nesting and exclusive recursions

NPM

Installing

Via NPM:

  1. npm install stringd

Usage

  1. import stringd from 'stringd';
  2. stringd('Hello :{name}', {name: 'World'});
  3. // Hello World
  4. stringd(':{last}, :{first} :{last}', {last: 'Doe', first: 'John'});
  5. // Doe, John Doe

API

stringd(template, object[, ignore])

Parse the tmp string, replacing variable sections with flexibly defined values within the props object
String tags within the ignore array are skipped in the process. (used specifically to avoid repetition/recursion)

Features

Multi parse methods

Whichever is more comfortable for you would work just fine

  1. stringd('Hello :{name}', {name: 'World'}); // Hello World
  2. stringd('Hello %{name}', {name: 'World'}); // Hello World
  3. stringd('Hello ${name}', {name: 'World'}); // Hello World
  4. stringd('Hello :{name%}', {name: 'World'}); // Hello World
  5. stringd('Hello %{name%}', {name: 'World'}); // Hello World
  6. stringd('Hello ${name%}', {name: 'World'}); // Hello World

Nesting

  1. assert.equal('John Davis', stringd(':{name}', {
  2. name: ':{first} :{last}',
  3. last: 'Davis',
  4. first: 'John',
  5. }));

Extended, Variable Passing

  1. assert.equal(
  2. 'Age Difference = [32 + 25] = [57]',
  3. stringd(
  4. stringd('Age Difference = [:{age1} + :{age2}] = [:{add(:{age1}, :{age2})}]', {
  5. age1: 32,
  6. age2: 25,
  7. }),
  8. {add: (_, data) => data.args.reduce((a, v) => a + +v.trim(), 0)},
  9. ),
  10. );
  1. assert.equal(
  2. 'Hello, Guys; Hello, World. How are you doing?',
  3. stringd(':{greet(Guys, post=How are you doing?, World)}', {
  4. greet: (_, names) =>
  5. names.args
  6. .map(name => `Hello, ${name.trim()}`)
  7. .join('; ')
  8. .concat('. ', names.matched.post),
  9. }),
  10. );

Functional Evaluation

  1. assert.equal(
  2. 'Hello John Davis, you have contributed $585 in the span of 13 months',
  3. stringd(':{name:parsed}', {
  4. name: ':{first} :{last}',
  5. last: 'Davis',
  6. first: 'John',
  7. greeting: 'Hello :{name}',
  8. months: 13,
  9. duration: ':{months} months',
  10. contribution: 45,
  11. // Functions get the entire template object as an argument
  12. // so you can do advanced processing
  13. cash: ({contribution, months}) => contribution * months,
  14. 'name:parsed': `:{greeting}, you have contributed $:{cash} in the span of :{duration}`,
  15. })
  16. );

Forward Padding, Space

  1. assert.equal(' 10', stringd(':5{val}', {val: 10}));

End Padding, Space

  1. assert.equal('10 ', stringd(':-5{val}', {val: 10}));

Forward Padding, Zero

  1. assert.equal('00010', stringd(':05{val}', {val: 10}));

End Padding, Zero

  1. assert.equal('10000', stringd(':-05{val}', {val: 10}));

Complex Nesting with exclusive recursion

Recursive nesting is unparsed at the second level, otherwise, it continues until its done parsing the entire string

  1. assert.equal(
  2. 'James:{name} :{name} :{data} :{data} :{all} :{name} :{data}Jack James:{data}Jack James:{data}Jack :{misc} :{data} :{all} James:{data}Jack :{misc}',
  3. stringd(':{name} :{misc}', {
  4. name: ':{first}:{data}:{last}',
  5. first: 'James',
  6. last: 'Jack',
  7. misc: ':{data}',
  8. data: ':{name} :{all}',
  9. all: ':{name} :{misc} :{data} :{all} :{name} :{misc}',
  10. })
  11. );

Iterative processing

  1. assert.equal(' 2364', stringd(stringd('::{pad}{price}', {pad: 10}), {price: 2364}))

Precedence

stringd replaces keys in order precedence. Keys that appear first are replaced first

  1. stringd( // str key appears first
  2. stringd(':{tro:{str}}', {str: 'y', 'tro:{str}': ':{tron}'}),
  3. { tron: 'Movie', troy: 'Dude' }
  4. ); // Dude
  5. stringd( // str key appears later
  6. stringd(':{tro:{str}}', {'tro:{str}': ':{tron}', str: 'y'}),
  7. { tron: 'Movie', troy: 'Dude' }
  8. ); // Movie

Development

Feel free to clone, use in adherance to the license. Pull requests are very much welcome.

  1. git clone https://github.com/miraclx/stringd.git
  2. cd stringd
  3. npm install
  4. # hack on code
  5. npm test

Testing

Tests are executed with Jest. To use it, simple run npm install, it will install
Jest and its dependencies in your project’s node_modules directory and finally npm test.

To run the tests:

  1. npm install
  2. npm test

License

Apache 2.0 © Miraculous Owonubi (@miraclx) omiraculous@gmail.com