Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

expander

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expander - npm Package Compare versions

Comparing version 0.3.1 to 0.3.2

79

index.js

@@ -10,2 +10,3 @@ const _ = require('lodash');

// but not these: <%= foo() %> or <%= foo.bar() %>
// todo -- support method calls here to remove the
var tmplRegExp = /\s*(?:\${\s*([a-z0-9_]+(?:\.[a-z0-9_]+)*)\s*}|<%=\s*([a-z0-9_]+(?:\.[a-z0-9_]+)*)\s*%>)\s*/i;

@@ -16,3 +17,3 @@

var matches = tmplRegExp.exec(lookup);
if(matches) {
if (matches) {
return {

@@ -27,4 +28,10 @@ src: matches && matches[0],

var hasTemplate = function (lookup) {
return (_.isString(lookup) &&
(lookup.indexOf('<%=') !== -1 ||
lookup.indexOf('${') !== -1));
};
// recursively expand a string until it doesn't contain any template strings
var stringExpand = function (data, lookup) {
var stringExpand = function (data, lookup, options) {
var property;

@@ -35,9 +42,13 @@ // as long as this contains a template string, keep traversing

// recursively process it as a template to handle interpolated strings (e.g. 'hi <%= key %>).
if(property.src !== lookup) {
lookup = _.template(lookup, data);
if (property.src !== lookup) {
lookup = _.template(lookup, data, options);
} else {
// expand to the literal value of this key
lookup = expander.process(data, getobject.get(data, property.prop));
lookup = expander.process(data, getobject.get(data, property.prop), options);
}
}
// do one final check for templates.
if (hasTemplate(lookup)) {
lookup = _.template(lookup, data, options);
}
return lookup;

@@ -47,5 +58,5 @@ };

// recursively expand an array until it doesn't contain any template strings
var arrayExpand = function (data, arr) {
var arrayExpand = function (data, arr, options) {
return arr.map(function(lookup) {
return expander.process(data, lookup);
return expander.process(data, lookup, options);
});

@@ -55,6 +66,6 @@ };

// recursively expand an object, resolving its template strings
var objectExpand = function (data, obj) {
var objectExpand = function (data, obj, options) {
var result = {};
Object.keys(obj).forEach(function(key) {
result[key] = expander.process(data, obj[key]);
result[key] = expander.process(data, obj[key], options);
});

@@ -71,11 +82,11 @@ return result;

// expand any type of legal lookup
expander.process = function (data, lookup) {
if(_.isFunction(lookup) && lookup.__expanderFn__ === true) {
expander.process = function (data, lookup, options) {
if (_.isFunction(lookup) && lookup.__expanderFn__ === true) {
return lookup.call(this, data);
} else if(_.isString(lookup)) {
return stringExpand(data, lookup);
} else if(_.isArray(lookup)) {
return arrayExpand(data, lookup);
} else if(_.isPlainObject(lookup)) {
return objectExpand(data, lookup);
} else if (_.isString(lookup)) {
return stringExpand(data, lookup, options);
} else if (_.isArray(lookup)) {
return arrayExpand(data, lookup, options);
} else if (_.isPlainObject(lookup)) {
return objectExpand(data, lookup, options);
} else {

@@ -88,3 +99,3 @@ return lookup;

expander.getRaw = function (data, lookup) {
if(!lookup) {
if (!lookup) {
return data;

@@ -97,7 +108,7 @@ } else {

// get the expanded value of a key as referenced by a dot-notated string
expander.get = function (data, lookup) {
expander.get = function (data, lookup, options) {
if (!lookup) {
return objectExpand(data, data);
return objectExpand(data, data, options);
} else {
return expander.process(data, getobject.get(data, lookup));
return expander.process(data, getobject.get(data, lookup), options);
}

@@ -111,17 +122,7 @@ };

expander.walk = function (config, lookup, key) {
var path = [];
var nodes = lookup.split('.');
var merges = [config.get(key)];
nodes.forEach(function (node) {
path.push(node);
merges.push(config.get(path.concat(key).join('.')));
});
return _.merge.apply(null, merges);
};
// provide a getter/setter interface for expander
// this is so ugly.
expander.interface = function (data) {
expander.interface = function (data, options) {
var emitter = new EventEmitter();
options = options||{};
var API = function (prop, value) {

@@ -132,3 +133,3 @@ if (arguments.length === 2) {

} else {
return expander.get(data, prop);
return expander.get(data, prop, options);
}

@@ -140,6 +141,10 @@ };

API.on = emitter.on.bind(emitter);
['get', 'getRaw', 'process'].forEach(function (method) {
API[method] = expander[method].bind(null, data);
});
API.get = function (prop) {
return expander.get(data, prop, options)
};
API.getRaw = expander.getRaw.bind(null, data);
API.process = function (lookup) {
return expander.process(data, lookup, options);
};
return API;
};
{
"name": "expander",
"description": "Expand template strings in declarative configurations.",
"version": "0.3.1",
"version": "0.3.2",
"homepage": "https://github.com/tkellen/expander",

@@ -6,0 +6,0 @@ "author": {

@@ -5,2 +5,66 @@ # expander [![Build Status](https://secure.travis-ci.org/tkellen/node-expander.png?branch=master)](http://travis-ci.org/tkellen/node-expander)

## API
### get(data, lookup, imports)
Retrieve a value from the data object with all template strings resolved.
`data` a configuration object
`lookup` a dot-notated key
`options` sent to [_.template](http://lodash.com/docs#template) when resolving values.
Example:
```js
var data = {
key: '<%= uppercase("foo") %>'
};
expander.get(data, 'key', {
imports: {
uppercase: function (str) {
return str.toUpperCase();
}
}
}); // FOO
```
### getRaw(data, lookup)
Retrieve a literal value from the data object.
`data` a configuration object
`lookup` a dot-notated string representing a key in the configuration
### set(data, lookup, value)
Set a value in the data object.
`data` a configuration object
`lookup` a dot-notated string representing a key in the data
`value` the value to set
### process(data, lookup, options)
Resolve any arbitrary template string.
`data` a configuration object
`lookup` any string value, typically a template string, e.g. "<%= key %>"
`options` sent to [_.template](http://lodash.com/docs#template) when resolving values.
### interface(data, options)
Bind the above API to a provided data object so you can access it more succinctly.
`data` a configuration object
`options` sent to [_.template](http://lodash.com/docs#template) when resolving values.
Example:
```js
var configRaw = {
key: 'value',
keyRef: '<%= key %>'
};
var config = expander.interface(config);
config('key'); // value
config.get('key'); // value
config('keyRef'); // value
config.get('keyRef'); // value
config('key', 'changed'); // changed
config('key'); // changed
```
## Examples

@@ -59,3 +123,3 @@

// getter setter api
var config = expander.interface(config);
var config = expander.interface(data);
config('keyRef'); // value

@@ -85,2 +149,3 @@ config('recursiveKeyRef'); // value

* 2014-02-20 - v0.3.2 - allow passing options to _.template
* 2014-02-11 - v0.3.1 - interface emits events on set

@@ -87,0 +152,0 @@ * 2014-02-10 - v0.3.0 - support a getter/setter api

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc