npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

2,633

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:

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]' }
  ]
}
*/

All examples.

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