Socket
Socket
Sign inDemoInstall

adlib

Package Overview
Dependencies
1
Maintainers
4
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
4
Install size
334 kB
Created

Changelog

Source

2.1.0

Added

  • ability to specify a hierarchy of possible values in order of preference in a template, e.g. this dataset was last modified {{metadata.some.nested.timestamp||item.modified}}

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: ['larry', 'sergey']
  }
};
let result = adlib(template, settings);
//> result.values === ['bear', 'fuzzy', 'brown']
//> result.names === ['larry', 'sergey']

Transforms

Adlib can apply transforms during the interpolation. The transform fn should have the following signature: fn(key, value, settings).

// Pattern
// {{key:transformFnName}}

let  tmpl = `{{s.animal.type:upcase}}`;
let settings = {
  s: {
    animal: {
      type: 'bear'
    }
  }
}
// will parse into
// key: s.animal.type
// value: 'bear'
// transformFnName: 'upcase'

Notes About Transforms

  • Transforms are ideally pure functions, and they must be sync functions! Promises are not supported.
  • Transform functions should be VERY resilient - we recommend unit testing them extensively
  • If your settings hash does not have an entry for the key, the value will be null.

Transforms

let template = {
  value:'{{s.animal.type:upcase}}'
};
let settings = {
  s: {
    animal: {
      type: 'bear'
    },
    color: 'brown'
  }
};
let transforms = {
  upcase (key, val, settings) {
    return val.toUpperCase();
  }
};
let result = adlib(template, settings, transforms);
//> result.value = 'BEAR'

Transforms using the Key

A typical use-case for this is for translation.

let template = {
  value:'{{s.animal.type:translate}}'
};
let settings = {};
let transforms = {
  translate (key, val, settings) {
    // the translator is passed in from the consuming application
    // note that the settings hash is empty
    return translator.translate(key);
  }
};
let result = adlib(template, settings, transforms);
//> result.value = 'string returned from translation system'

Built-in Transforms

adlib comes with some built-in transforms:

  • optional - declare a value to be optional

Optional Transform

{{key.path:optional:<levelToRemove>}}

By default, if the key is not found, adlib simply leaves the {{key.path}} in the output json. However, that can/will lead to problems when the json is consumed.

The optional transform helps out in these scenarios. By default when adlib encounters something like:

{
  someProp: 'red'
  val: '{{key.path:optional}}'
}

and key.path is null or undefined, the val property will simply be removed.

{
  someProp: 'red'
}

The same thing works in arrays

{
  someProp: 'red'
  vals: [
    'red',
    '{{key.path:optional}}'
  ]
}

// returns
{
  someProp: 'red'
  vals: [
    'red',
  ]
}

However, there are times when simply removing the property/entry is not enough. Sometimes you need to "reach up" the object graph and remove a parent. This is where the levelToRemove comes in...

let template = {
  someProp: 'red',
  operationalLayers: [
    {
      url: `{{layers.pipes.url}}`,
      fields: [
        {
          key: 'direction',
          fieldName: `{{layers.pipes.directionField:optional:3}}`
        }
      ]
    }
  ]
};
let settings = {
  layers: {
    pipes: {
      url: 'http://someserver.com/23'
    }
  }
};
// will returns
{
  someProp: 'red'
  operationalLayers: []
}

levelToRemove

valueremoves what
0 (default)the property or array entry
1the parent object/array
2the grand-parent object/array
...... up the hiearchy

Path Hierarchies

Sometimes you may want to adlib a value using one of several possible data sources. You can specify each data source in a hierarchy of preference in the template

let template = {
  dataset: {
    title: {{layer.name||item.title}},
    modified: {{metadata.some.super.nested.value.bc.im.a.weird.xml.doc:toISO||item.modified:toISO}},
    tags: {{metadata.categories||item.tags}}
  } 
}

let settings = {
  metadata: {
    categories: [
      'citations',
      'civil offense',
      'misdemeanor'
    ],
    some: {
      super: {
        nested: {
          value: {
            bc: {
              im: {
                a: {
                  weird: {
                    xml: {
                      doc: '1505836376836'  
                    }  
                  }  
                }  
              }  
            }  
          }  
        }  
      }  
    }
  },
  item: {
    title: '2014 Parking Violations',
    tags: [
      'Parking',
      'Washington',
      'Citations',
      'Crimes',
      'Law Enforcement',
      'Nuisance',
      'Cars'
    ]
  },
  layer: {}
}

let transforms = {
  toISO: function (key, val, settings) {
    if (isStringAndNotADateValue(val)) {
      return new Date(val).toISOString()
    }   
  }  
}

adlib(template, settings, transforms)
// => returns
{
  dataset: {
    title: '2014 Parking Violations',
    modified: '2017-09-19T15:52:56.836Z',
    tags: [
      'citations',
      'civil offense',
      'misdemeanor'
    ]
  }
}

Keywords

FAQs

Last updated on 23 Jan 2018

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