object-placeholder
v0.2.3
Published
It's a zero-dependency package that exports default function: ```text placeholder(<template>, <data>, <options>) ``` and function with named params: ```text placeholder.replace({ template, data, options }) ``` where: - `template` - some template ( [string
Downloads
6,254
Maintainers
Readme
object-placeholder
It's a zero-dependency package that exports default function:
placeholder(<template>, <data>, <options>)
and function with named params:
placeholder.replace({ template, data, options })
where:
template
- some template ( string, object, array )data
- object with values to replaceoptions
- { error, clone, stringify }
This function allows you to substitute 'mustache' like {{<template>}}
by values in <data>
param including all nested properties of object or array template.
Usage
const placeholder = require('object-placeholder')
// or
const { replace } = require('object-placeholder')
String
template:
const template = '{{user.name}}, {{user.email}}, {{user.id}}'
const data = {
user: {
id: 1985,
name: 'John Connor',
email: '[email protected]'
}
}
const result = placeholder(template, data)
// or
const result = replace({ template, data })
// result = 'John Connor, [email protected], 1985'
Object
template:
const template = {
target: {
uuid: '&{{user.id}}',
user: '{{user.name}}',
},
mailto: 'mailto:{{user.email}}',
}
const data = {
user: {
id: 1985,
name: 'John Connor',
email: '[email protected]'
}
}
const result = placeholder(template, data)
/*
result = {
target: { uuid: 1985, user: 'John Connor' },
mailto: 'mailto:[email protected]'
}
*/
Array
template:
const template = {
title: '{{ service.id }}',
admin: '{{ service.members[0].id }}', // get first element of 'service.members'
mailto: '{{service.members.0.email}}',
emails: [
'@{{ service.members | member }}', // for each item of 'service.members'
'{{ @.member.email }}', // '@.member' - current item
],
users: '&{{ service.members }}',
}
const data = {
service: {
id: 'SOME_IT_SERVICE',
members: [
{ id: 'user1', email: '[email protected]' },
{ id: 'user2', email: '[email protected]' },
{ id: 'user3', email: '[email protected]' },
],
},
}
const result = placeholder(template, data)
/*
result = {
title: 'SOME_IT_SERVICE',
admin: 'user1',
mailto: '[email protected]',
emails: [ '[email protected]', '[email protected]', '[email protected]' ],
users: [
{ id: 'user1', email: '[email protected]' },
{ id: 'user2', email: '[email protected]' },
{ id: 'user3', email: '[email protected]' }
]
}
*/
Syntax
1. String value syntax
Returns the value converted to 'string' type
{{property}}
Path can also be dot-separated:
{{user.name}} {{user.email}}
In this case data
parameter should be the object:
{
property: 'blablabla',
user: {
name: 'John Connor',
email: '[email protected]'
}
}
2. Reference value syntax
Returns the value of original type
&{{property}}
3. Loop syntax
Starts new loop for property of array type
@{{ array | item }}
4. Item syntax
Returns the value of current item in a loop
{{@.item.property}}
Options
By default
options: {
error: true,
clone: true,
stringify: true,
}
error
Define how to manage the case when template was not resolved.
If true
then throw the Error immediately in place where value by specified path was not found.
If false
then just pass through this case and leave template string as is.
If custom function
passed then it will be used as error handler function.
For more details see test examples.
clone
Clone the output value or not.
If true
then all properties of output object will be cloned.
If false
then 'object' type properties will refer to input data object properties.
If custom function
passed then it will be used as clone function.
For more details see test examples.
stringify
Stringify the value of non 'string' type.
If true
then JSON.stringify()
will be used.
If false
then value.toString()
will be used.
If custom function
passed then it will be used as stringify function.
For more details see test examples.
Partial application
You can use partial application for replace
function parameters in order to produce another function of smaller arguments i.e binding values to one or more of those arguments. For example:
const { replace } = require('object-placeholder')
const template = '{{user.name}}, {{user.email}}, {{user.id}}'
const options = { clone: true }
const configured = replace({ template, options })
const result1 = configured({
data: {
user: {
id: 1985,
name: 'John Connor',
email: '[email protected]'
}
}
})
// result1 = 'John Connor, [email protected], 1985'
const result2 = configured({
data: {
user: {
id: 1965,
name: 'Sarah Connor',
email: '[email protected]'
}
}
})
// result2 = 'Sarah Connor, [email protected], 1965'
Install
Install on Node.JS with npm
$ npm install --save object-placeholder
License
MIT © Taras Panasyuk