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

Comparing version 2.1.1 to 2.2.0

.esm-cache/025ecdb62228c28c.js

4

CHANGELOG.md

@@ -5,2 +5,6 @@ # Change Log

# 2.2.0
### Added
- support for `default` values like `Hello my name is {{obj.name||Slim Shady}}`. If `obj.name` is undefined, the default value is used.
# 2.1.1

@@ -7,0 +11,0 @@ ### Fixed

29

dist/adlib.js
/**
* adlib - v2.1.0 - Tue Jan 23 2018 17:04:32 GMT-0500 (EST)
* adlib - v2.0.0 - Thu Jan 11 2018 07:31:07 GMT-0700 (MST)
* Copyright (c) 2018 Dave Bouwman / Esri

@@ -323,3 +323,3 @@ * Apache-2.0

var replaceValue = false;
var handlebars = /{{([\w\.\:(||)?]+)}}/g;
var handlebars = /{{([\w\.\:]+)}}/g;
var match = templateValue.match(handlebars);

@@ -330,23 +330,5 @@

var path = entry.replace(/{|}/g, '');
// checking if there were multiple paths specified
// If so, take the value of the path that matches first
// in order from left to right.
//
// e.g.
//
// {{special.value||backup.value}}
//
// we will prefer the value @ 'special.value' but if it is not
// there we will take the value @ 'backup.value'
if (path.indexOf('||') !== -1) {
var paths = path.split('||');
path = paths.find(function (pathOption) {
var v = getWithDefault(settings, pathOption.split(':')[0], null);
return v !== null && v !== undefined
});
}
return {
key: entry,
value: _swap(path, settings, transforms)|| entry
value: _swap(path, settings, transforms) || entry
};

@@ -358,6 +340,3 @@ });

} else {
// a little extra regex dance to match the '||' because '|'
// is a Very Special Regex Character and we need to super
// escape them for the regex to work
var re = new RegExp(v.key.replace(/\|\|/gi, '\\|\\|'));
var re = new RegExp(v.key, 'g');
templateValue = templateValue.replace(re, v.value);

@@ -364,0 +343,0 @@ }

@@ -1,2 +0,2 @@

!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.adlib=e()}(this,function(){"use strict";function n(n,e,t){return void 0===t&&(t=void 0),e.split(".").reduce(function(n,e){return n?n[e]:t},n)}function e(n,e){return Object.keys(n).reduce(function(t,r){return t[r]=e(n[r],r,n),t},{})}function t(n){return n instanceof Date}function r(n){return"function"==typeof n}function i(n){return"object"==typeof n}function u(n){return n instanceof RegExp}function o(n,f,a,c){function l(n,e){return o(n,f,a?a+"."+e:e)}if(a=a||"",Array.isArray(n))return n.map(l);if(!n||!i(n)||t(n)||u(n)||r(n)){return f(n,a)}return Object.assign({},n,e(n,l))}function f(n){return"object"==typeof n}function a(n,t){function r(n,e){return a(n,t?t+"."+e:e)}if(t=t||"",Array.isArray(n)){return c(n.map(r).filter(function(n){return null!==n&&void 0!==n}))}return n&&f(n)?l(e(n,r)):p(n)}function c(n){var e=n,t=n.reduce(function(n,e){if(y(e)&&v(e)){var t=d(e);t>n&&(n=t)}return n},-1);return t>-1&&(e=0===t?[]:"{{delete:"+(t-1)+"}}"),e}function l(n){var e={obj:{},maxLevel:-1},t=Object.keys(n).reduce(function(e,t){var r=n[t];if(y(r)&&v(r)){var i=d(r);i>e.maxLevel&&(e.maxLevel=i)}else e.obj[t]=r;return e},e);return t.maxLevel>0?1===t.maxLevel?void 0:"{{delete:"+(t.maxLevel-1)+"}}":t.obj}function p(n){var e=n;return"string"==typeof n&&v(n)&&(e=s(n)),e}function s(n){var e=d(n);return 0===e?void 0:"{{delete:"+e+"}}"}function d(n){return parseInt(n.replace(/{|}/g,"").split(":")[1])}function v(n){return!(!n||"string"!=typeof n)&&n.indexOf("{{delete")>-1}function y(n){return"string"==typeof n}function m(n,e,t,r){void 0===r&&(r=0);var i=e;return e||(i="{{delete:"+r+"}}"),i}function g(n){return"string"==typeof n}function x(e,t,r){var i,u=e.split(":");if(u.length>1){var o=u[0],f=u[1],a=null;if(u[2]&&(a=u[2]),!r||!r[f]||"function"!=typeof r[f])throw new Error("Attempted to apply non-existant transform "+f+" on "+o+" with params "+a);i=n(t,o),i=r[f](o,i,t,a)}else i=n(t,e);return i}function b(e,t,r){if(void 0===r&&(r=null),r=r||{},r.optional)throw new Error("Please do not pass in an `optional` transform, adlib provides that interally.");return r.optional=m,a(o(e,function(e,i){if(!g(e))return e;var u,o=/{{([\w\.\:(||)?]+)}}/g,f=e.match(o);if(f&&f.length){return f.map(function(e){var i=e.replace(/{|}/g,"");if(-1!==i.indexOf("||")){i=i.split("||").find(function(e){var r=n(t,e.split(":")[0],null);return null!==r&&void 0!==r})}return{key:e,value:x(i,t,r)||e}}).forEach(function(n){if(e==n.key)u=n.value;else{var t=new RegExp(n.key.replace(/\|\|/gi,"\\|\\|"));e=e.replace(t,n.value)}}),u||e}return e}))}return b});
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.adlib=e()}(this,function(){"use strict";function n(n,e,t){return void 0===t&&(t=void 0),e.split(".").reduce(function(n,e){return n?n[e]:t},n)}function e(n,e){return Object.keys(n).reduce(function(t,r){return t[r]=e(n[r],r,n),t},{})}function t(n){return n instanceof Date}function r(n){return"function"==typeof n}function o(n){return"object"==typeof n}function u(n){return n instanceof RegExp}function i(n,f,a,c){function l(n,e){return i(n,f,a?a+"."+e:e)}if(a=a||"",Array.isArray(n))return n.map(l);if(!n||!o(n)||t(n)||u(n)||r(n)){return f(n,a)}return Object.assign({},n,e(n,l))}function f(n){return"object"==typeof n}function a(n,t){function r(n,e){return a(n,t?t+"."+e:e)}if(t=t||"",Array.isArray(n)){return c(n.map(r).filter(function(n){return null!==n&&void 0!==n}))}return n&&f(n)?l(e(n,r)):p(n)}function c(n){var e=n,t=n.reduce(function(n,e){if(y(e)&&v(e)){var t=d(e);t>n&&(n=t)}return n},-1);return t>-1&&(e=0===t?[]:"{{delete:"+(t-1)+"}}"),e}function l(n){var e={obj:{},maxLevel:-1},t=Object.keys(n).reduce(function(e,t){var r=n[t];if(y(r)&&v(r)){var o=d(r);o>e.maxLevel&&(e.maxLevel=o)}else e.obj[t]=r;return e},e);return t.maxLevel>0?1===t.maxLevel?void 0:"{{delete:"+(t.maxLevel-1)+"}}":t.obj}function p(n){var e=n;return"string"==typeof n&&v(n)&&(e=s(n)),e}function s(n){var e=d(n);return 0===e?void 0:"{{delete:"+e+"}}"}function d(n){return parseInt(n.replace(/{|}/g,"").split(":")[1])}function v(n){return!(!n||"string"!=typeof n)&&n.indexOf("{{delete")>-1}function y(n){return"string"==typeof n}function m(n,e,t,r){void 0===r&&(r=0);var o=e;return e||(o="{{delete:"+r+"}}"),o}function g(n){return"string"==typeof n}function x(e,t,r){var o,u=e.split(":");if(u.length>1){var i=u[0],f=u[1],a=null;if(u[2]&&(a=u[2]),!r||!r[f]||"function"!=typeof r[f])throw new Error("Attempted to apply non-existant transform "+f+" on "+i+" with params "+a);o=n(t,i),o=r[f](i,o,t,a)}else o=n(t,e);return o}function b(n,e,t){if(void 0===t&&(t=null),t=t||{},t.optional)throw new Error("Please do not pass in an `optional` transform, adlib provides that interally.");return t.optional=m,a(i(n,function(n,r){if(!g(n))return n;var o,u=/{{([\w\.\:]+)}}/g,i=n.match(u);if(i&&i.length){return i.map(function(n){return{key:n,value:x(n.replace(/{|}/g,""),e,t)||n}}).forEach(function(e){if(n==e.key)o=e.value;else{var t=new RegExp(e.key,"g");n=n.replace(t,e.value)}}),o||n}return n}))}return b});
//# sourceMappingURL=adlib.min.js.map
/**
* adlib - v2.1.0 - Tue Jan 23 2018 17:04:30 GMT-0500 (EST)
* adlib - v2.0.0 - Thu Jan 11 2018 07:31:06 GMT-0700 (MST)
* Copyright (c) 2018 Dave Bouwman / Esri

@@ -323,3 +323,3 @@ * Apache-2.0

var replaceValue = false;
var handlebars = /{{([\w\.\:(||)?]+)}}/g;
var handlebars = /{{([\w\.\:]+)}}/g;
var match = templateValue.match(handlebars);

@@ -330,23 +330,5 @@

var path = entry.replace(/{|}/g, '');
// checking if there were multiple paths specified
// If so, take the value of the path that matches first
// in order from left to right.
//
// e.g.
//
// {{special.value||backup.value}}
//
// we will prefer the value @ 'special.value' but if it is not
// there we will take the value @ 'backup.value'
if (path.indexOf('||') !== -1) {
var paths = path.split('||');
path = paths.find(function (pathOption) {
var v = getWithDefault(settings, pathOption.split(':')[0], null);
return v !== null && v !== undefined
});
}
return {
key: entry,
value: _swap(path, settings, transforms)|| entry
value: _swap(path, settings, transforms) || entry
};

@@ -358,6 +340,3 @@ });

} else {
// a little extra regex dance to match the '||' because '|'
// is a Very Special Regex Character and we need to super
// escape them for the regex to work
var re = new RegExp(v.key.replace(/\|\|/gi, '\\|\\|'));
var re = new RegExp(v.key, 'g');
templateValue = templateValue.replace(re, v.value);

@@ -364,0 +343,0 @@ }

@@ -15,2 +15,3 @@ /**

let value;
// console.info(`_swap: param: ${parameter}`);
// Parameters can optionally call transform functions

@@ -35,3 +36,3 @@ // e.g. "{{ipsum:translateLatin}}"

} else {
throw new Error(`Attempted to apply non-existant transform ${fn} on ${key} with params ${param}`);
throw new Error(`Attempted to apply non-existant transform ${fn} on ${key} with params ${parameter}`);
}

@@ -45,2 +46,16 @@ } else {

/**
* Does a propertyPath exist on a target
*/
function _propertyPathExists (propertyPath, target) {
// remove any transforms
let cleanPath = propertyPath.split(':')[0];
let value = getWithDefault(target, cleanPath, null);
if (value !== null && value !== undefined) {
return true;
} else {
return false;
}
}
// Combine a Template with Settings

@@ -64,33 +79,70 @@ export default function adlib(template, settings, transforms = null) {

var replaceValue = false;
var handlebars = /{{([\w\.\:(||)?]+)}}/g;
let match = templateValue.match(handlebars);
if (match && match.length) {
let values = match.map((entry) => {
var handlebars = /{{[\S\s]*?}}/g;
let hbsEntries = templateValue.match(handlebars);
if (hbsEntries && hbsEntries.length) {
// console.log(`Got a ${hbsEntries.length} handlebar entries...`);
let isStaticValue = false;
// iterate over the entries...
let values = hbsEntries.map((entry) => {
// console.info(`Matched ${entry}...`);
// strip off the curlies...
let path = entry.replace(/{|}/g, '');
// checking if there were multiple paths specified
// If so, take the value of the path that matches first
// in order from left to right.
//
// e.g.
//
// {{special.value||backup.value}}
//
// we will prefer the value @ 'special.value' but if it is not
// there we will take the value @ 'backup.value'
if (path.indexOf('||') !== -1) {
// check for || which indicate a hiearchy
if (path.indexOf('||') > -1) {
var paths = path.split('||');
path = paths.find((pathOption) => {
let v = getWithDefault(settings, pathOption.split(':')[0], null)
return v !== null && v !== undefined
})
let numberOfPaths = paths.length;
// here we check each option, in order, and return the first with a value in the hash, OR the last
path = paths.find((pathOption, idx) => {
// console.info(`Checking to see if ${pathOption} is in settings hash...`);
let exists = _propertyPathExists(pathOption, settings);
if (!exists) {
if ((idx + 1) === numberOfPaths) {
// console.info(`Got to last entry, and still did not find anything... assuming ${pathOption} is a static value...`);
isStaticValue = true;
// check if we can convert this into a number...
if (!isNaN(pathOption)) {
pathOption = parseInt(pathOption);
}
return pathOption;
} else {
return false;
}
} else {
return pathOption;
}
});
}
return {
// setup the return value...
let result = {
key: entry,
value: _swap(path, settings, transforms)|| entry
};
value: path
}
// if we have a valid object path, value comes from _swap
if (!isStaticValue) {
result.value = _swap(path, settings, transforms) || entry;
}
// console.info(`Value: ${JSON.stringify(result)}`);
return result;
});
values.forEach((v) => {
if (templateValue == v.key) {
// console.log(`Comparing ${templateValue} with ${v.key}`)
if (templateValue === v.key) {
// console.log(`template matches key, returning ${v.value}`);
// if the value is a string...
if (typeof v.value === 'string') {
// and it's numeric-ish
if(!isNaN(v.value)) {
// and has a . in it...
if (v.value.indexOf('.') > -1) {
// parse as a float...
v.value = parseFloat(v.value);
} else {
// parse as an int
v.value = parseInt(v.value);
}
}
}
settingsValue = v.value;

@@ -101,4 +153,6 @@ } else {

// escape them for the regex to work
let re = new RegExp(v.key.replace(/\|\|/gi, '\\|\\|'));
templateValue = templateValue.replace(re, v.value);
// console.log(`KEY ${v.key}`);
// console.log(`TEMPLATE ${templateValue}`);
templateValue = templateValue.replace(v.key, v.value);
// console.log(`template did not match key, interpolating value ${v.value} into template to produce ${templateValue}`);
}

@@ -109,4 +163,6 @@ });

if (settingsValue) {
// console.log(`We found a value so we return it ${settingsValue}`);
return settingsValue;
} else {
// console.log(`We did not find a value so we return the template ${templateValue}`);
// but if we don't, lets return the template itself

@@ -116,2 +172,3 @@ return templateValue;

} else {
// console.log(`We did not find a hbs match, so we return the template ${templateValue}`);
// no match, return the templateValue...

@@ -118,0 +175,0 @@ return templateValue;

{
"name": "adlib",
"version": "2.1.1",
"version": "2.2.0",
"description": "Templating for deep JSON object graphs",

@@ -5,0 +5,0 @@ "main": "dist/adlib.umd.js",

@@ -252,3 +252,3 @@ # Adlib

tags: {{metadata.categories||item.tags}}
}
}
}

@@ -320,1 +320,21 @@

```
### Path Hierarchies with Defaults
If none of the paths are available, the last entry can be a static value and that will be returned.
We support returning strings ('RED', 'the red fox'), ints (23, 15), and floats (12.3, 0.234)
**Note** Transforms can not be applied to the default value!
Please see TODO.md for notes about changes required for this.
```
let template = {
msg: 'Site is at {{obj.mainUrl||obj.otherUrl||https://foo.bar?o=p&e=n}}'
}
var settings = {}
let result = adlib(template, settings);
// => returns
// 'Site is at https://foo.bar?o=p&e=n'
```

@@ -624,1 +624,85 @@

})
test('Adlib::Hierarchies:: last choice can be a static string', (t) => {
let template = {
msg: 'Luke is {{obj.mood||happy}} with {{obj.friend||Jabba}}'
}
var settings = {}
let result = adlib(template, settings)
t.plan(1);
t.equal(result.msg, 'Luke is happy with Jabba');
t.end();
})
test('Adlib::Hierarchies:: last choice can be a static url', (t) => {
let template = {
msg: 'Site is at {{obj.mainUrl||obj.otherUrl||https://foo.bar?o=p&e=n}}'
}
var settings = {}
let result = adlib(template, settings)
t.plan(1);
t.equal(result.msg, 'Site is at https://foo.bar?o=p&e=n');
t.end();
})
test('Adlib::Hierarchies:: last choice can be a int', (t) => {
let template = {
msg: 'Luke is {{obj.happy||obj.sad||68}}',
val: '{{obj.age||68}}'
}
var settings = {}
let result = adlib(template, settings)
t.plan(2);
t.equal(result.msg, 'Luke is 68');
t.equal(result.val, 68, 'should return as a int');
t.end();
})
test('Adlib::Hierarchies:: last choice can be a float', (t) => {
let template = {
msg: 'Luke is {{obj.happy||obj.sad||68.345}}',
val: '{{obj.age||68.345}}'
}
var settings = {}
let result = adlib(template, settings)
t.plan(2);
t.equal(result.msg, 'Luke is 68.345');
t.equal(result.val, 68.345, 'should return as a float');
t.end();
})
test('Adlib::Hierarchies:: last choice can be timestamp', (t) => {
let template = {
msg: 'Created on {{obj.created:toISO||obj.modified:toISO||1469803210000}}',
val: '{{obj.age||1469803210000}}'
}
var settings = {}
var transforms = {
toISO: function (key, val, settings) {
if (val) {
return new Date(parseInt(val)).toISOString()
}
}
}
let result = adlib(template, settings, transforms)
t.plan(2);
t.equal(result.msg, 'Created on 1469803210000');
t.equal(result.val, 1469803210000, 'should return as a number');
t.end();
})

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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