Socket
Socket
Sign inDemoInstall

adlib

Package Overview
Dependencies
0
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    adlib

Templating for deep JSON object graphs


Version published
Maintainers
2
Install size
429 kB
Created

Readme

Source

Adlib

A library for interpolating property values in JSON Objects.

The Hub team uses this to create customized Web Maps, Hub Sites, Hub Pages and other types of items.

General Pattern

template: {
  val: '{{thing.val}}'
};
settings: {
  thing: {
    val: 'red'
  }
};
result = adlib(template, settings);
// > {val: 'red'}

Note Adlib does not mutate the template, it returns a new object that contains copies of the template properties, with interpolations applied. This allows the template to be used multiple times in succession with different settings hashes.

Supported Interpolations

Strings

Within the template, the value of any property can be described using {{obj.prop}}.

If the obj.prop "path" in the settings object is a string, that string value is assigned to the value.

Multiple Strings

A property of a template can have a value like 'The {{thing.animal}} was {{thing.color}}'. When combined with a settings object that has the appropriate values, this will result in The fox was brown.

let template = {
  value: 'The {{thing.animal}} was {{thing.color}}'
};
let settings = {
  thing: {
    color: 'red',
    animal: 'fox'
  }
};
let result = adlib(template, settings);
//> {value: 'The fox was red'}

Objects

If the interpolated value is an object, it is returned. This allow us to graft trees of json together.

let template = {
  value: '{{s.obj}}'
};
let settings = {
  s: {
    obj: {
      val: 'red'
    }
  }
};
let result = adlib(template, settings);
//> { value: {val: 'red'}}

Arrays

If the interpolated value is an array, it is returned. Interpolation is also done within arrays.

let template = {
  values: ['{{s.animal}}', 'fuzzy','{{s.color}}'],
  names: '{{s.names}}'
};
let settings = {
  s: {
    animal: 'bear',
    color: 'brown',
    names: ['steve', 'dave']
  }
};
let result = adlib(template, settings);
//> result.values === ['bear', 'fuzzy', 'brown']
//> result.names === ['steve', 'dave']

Keywords

FAQs

Last updated on 01 May 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc