expands

Expands variable placeholders in objects.

Usage no npm install needed!

<script type="module">
  import expands from 'https://cdn.skypack.dev/expands';
</script>

README

expands

NPM version Build status Coveralls status Support us

Expands variable placeholders in objects.

Installation

This module can be installed easily with npm:

$ npm install expands

Usage

These examples show you how expands works:

var assert = require('assert');
var expand = require('expands');

// Expands string
assert.deepEqual(expand({
  message: 'Welcome {name}!',
  name: 'intruder'
}), {
  message: 'Welcome intruder!',
  name: 'intruder'
});

// Expands array
assert.deepEqual(expand({
  messages: [
    'Welcome {name}!',
    'You are {age} years old.',
    12345
  ],
  name: 'intruder',
  age: 1000
}), {
  messages: [
    'Welcome intruder!',
    'You are 1000 years old.',
    12345
  ],
  name: 'intruder',
  age: 1000
});

// Expands deep path
assert.deepEqual(expand({
  foo: {
    bar: {
      one: '1',
      two: '2'
    },
    message: 'One is {foo.bar.one} and two is {foo.bar.two}.'
  }
}), {
  foo: {
    bar: {
      one: '1',
      two: '2'
    },
    message: 'One is 1 and two is 2.'
  }
});

// Expands circle dependency
assert.deepEqual(expand({
  a: 'from {b}', // expanding is top-down so `a` will be expanded first
  b: 'from {c}',
  c: 'from {a}', // `a` is not expanded completely, resolved to 'from ',
}), {
  a: 'from from from from ',
  b: 'from from from ',
  c: 'from from '
});

Contributing

Before create a pull request, make sure that you:

To execute all tests, simply run:

$ npm test

Contributors

License

This module is released under MIT license.