/node-mkenv

Parser/template engine for simple enviroment variable-like text replacements

Primary LanguageJavaScript

mkenv

Install: npm install --save mkenv

Use: var mkenv = require('mkenv');

constructor: mkenv(envs...)

constructor: mkenv(envs...) accepts hashes

mkenv({ x: 'y' })('x').should.equal('y')

constructor: mkenv(envs...) accepts functions

mkenv(function (name) {
	return name == 'x' ? 'y' : null;
})('x').should.equal('y')

composition

composition allows values to be retrieved from the root

mkenv(
	{ z: 'y' },
	mkenv({ 'x': '$z' })
)('x').should.equal('y')

value retrieval

value retrieval works by calling the env with a key

mkenv({ x: 'y' })('x').should.equal('y')

value retrieval gives the rightmost value (overriding)

mkenv({ x: 'y' }, { x: 'z'})('x').should.equal('z')

value retrieval returns null on an unknown key

should.equal(
	mkenv({ x: 'y' })('z'), null
)

simple replacements

simple replacements can be made

mkenv({
	x: 'y',
	y: '$x'
})('y').should.equal('y')

simple replacements can be combined

mkenv({
	x: 'y',
	y: '$x$x$x'
})('y').should.equal('yyy')

simple replacements can be chained

mkenv({
	x: 'y',
	y: '$x',
	z: '$y',
	w: '$z'
})('w').should.equal('y')

bracketed replacements

bracketed replacements can be made

mkenv({
	x: 'y',
	y: '${x}'
})('y').should.equal('y')

bracketed replacements can be nested

mkenv({
	x: 'y',
	n: 'x',
	y: '${$n}'
})('y').should.equal('y')

bracketed replacements can be nested, combined, and chained

mkenv({
	x: 'y',
	n: 'x',
	xx: '$x$x',
	y: '${$n$n}'
})('y').should.equal('yy')

mkenv.keys(env)

mkenv.keys(env) returns the keys stored in an env

mkenv.keys(
	mkenv({ x: 'y' }, { y: 'z'})
).should.eql(['x', 'y'])

mkenv.keys(env) returns the keys stored in a nested env

mkenv.keys(
	mkenv({ x: 'y' }, mkenv({ y: 'z'}))
).should.eql(['x', 'y'])

mkenv.keys(env) returns additional keys

var env = mkenv({ x: 'y' });
env.y = 'z'
mkenv.keys(env).should.eql(['x', 'y'])

mkenv.vars(env)

mkenv.vars(env) returns the vars stored in an env

mkenv.vars(
	mkenv({ x: 'y' }, { y: 'z'})
).should.eql({ x: 'y', y: 'z'})

mkenv.vars(env) returns the vars stored in a nested env

mkenv.vars(
	mkenv({ x: 'y' }, mkenv({ y: 'z'}))
).should.eql({ x: 'y', y: 'z'})

mkenv.vars(env) returns additional keys

var env = mkenv({ x: 'y' });
env.y = 'z'
mkenv.vars(env).should.eql({ x: 'y', y: 'z'})

mkenv.hidden(env)

mkenv.hidden(env) allows values to be retrieved from the root

mkenv(
	mkenv.hidden({ z: 'y' }),
	mkenv({ 'x': '$z' })
)('x').should.equal('y')

mkenv.hidden(env) doesn't allow direct retrieval from the root

should.equal(
	mkenv(
		mkenv.hidden({ z: 'y' }),
		mkenv({ 'x': '${z}' })
	)('z'),
	null
)

Documentation generated by Docha