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

async-helpers

Package Overview
Dependencies
Maintainers
2
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-helpers - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

131

index.js

@@ -30,3 +30,3 @@ /*!

options = options || {};
this.prefix = options.prefix || '__async_';
this.prefix = options.prefix || '__async';
this.helpers = {};

@@ -51,4 +51,4 @@ this.stash = {};

* ```js
* asyncHelpers.set('upper', function (str, done) {
* done(null, str.toUpperCase());
* asyncHelpers.set('upper', function (str, cb) {
* cb(null, str.toUpperCase());
* });

@@ -64,2 +64,5 @@ * ```

AsyncHelpers.prototype.set = function(name, fn) {
if (typeof name !== 'string') {
throw new TypeError('AsyncHelpers#set expects `name` to be a string.');
}
this.helpers[name] = fn;

@@ -84,38 +87,81 @@ return this;

AsyncHelpers.prototype.get = function(name, options) {
AsyncHelpers.prototype.get = function(name, opts) {
if (name == null) {
throw new TypeError('AsyncHelpers#get expects a string or object.');
}
if (typeof name === 'object') {
options = name;
opts = name;
name = null;
}
options = options || {};
if (options.wrap) {
opts = opts || {};
if (opts.wrap) {
return this.wrap(name);
}
return name == null ? this.helpers : this.helpers[name];
return typeof name === 'string'
? this.helpers[name]
: this.helpers;
};
function wrapper(name) {
var self = this;
/**
* Wrap a helper or object of helpers with an async handler function.
*
* @param {String|Object} `name` Helper or object of helpers
* @return {Object} Wrapped helper(s)
*/
function wrap(name) {
if (name == null) {
throw new TypeError('async-helpers wrap expects a string or object.');
}
var helper = this.helpers[name];
if (typeof helper === 'object') {
for (var key in helper) {
helper[key] = wrapper(key, helper[key], this);
}
return helper;
} else {
return wrapper(name, helper, this);
}
}
return function () {
var args = [].slice.call(arguments);
/**
* Returns a wrapper function for a single helper.
*
* @param {String} `name` The name of the helper
* @param {Function} `fn` The actual helper function
* @param {Object} `thisArg` Context
* @return {String} Returns an async ID to use for resolving the value. ex: `__async18__`
*/
function wrapper(name, fn, thisArg) {
return function() {
var argRefs = [];
var len = arguments.length;
var args = new Array(len);
for (var i = len - 1; i >= 0; i--) {
var arg = args[i] = arguments[i];
// store references to other async helpers
if (typeof arg === 'string' && arg.indexOf(thisArg.prefix) === 0) {
argRefs.push({arg: arg, idx: i});
}
}
// generate a unique ID for the wrapped helper
var id = thisArg.prefix + thisArg.globalCounter + (thisArg.counter++) + '__';
var obj = {
id: id,
name: name,
id: self.prefix + self.globalCounter + '_' + (self.counter++) + '__',
fn: helper,
fn: fn,
args: args,
argRefs: []
argRefs: argRefs
};
// store references to other async helpers
args.forEach(function (arg, i) {
if (typeof arg === 'string' && arg.indexOf(self.prefix) === 0) {
obj.argRefs.push({arg: arg, idx: i});
}
});
self.stash[obj.id] = obj;
thisArg.stash[obj.id] = obj;
return obj.id;
};
}
}

@@ -137,11 +183,9 @@

AsyncHelpers.prototype.wrap = function(name) {
var self = this;
if (name) {
return wrapper.call(this, name);
if (name) return wrap.call(this, name);
var res = {};
for (var key in this.helpers) {
res[key] = this.wrap(key);
}
var keys = Object.keys(this.helpers);
return keys.reduce(function (res, key) {
res[key] = self.wrap(key);
return res;
}, {});
return res;
};

@@ -179,14 +223,22 @@

* @param {String} `key` ID generated when from executing a wrapped helper.
* @param {Function} `done` Callback function with the results of executing the async helper.
* @param {Function} `cb` Callback function with the results of executing the async helper.
* @api public
*/
AsyncHelpers.prototype.resolve = function(key, done) {
AsyncHelpers.prototype.resolve = function(key, cb) {
if (typeof cb !== 'function') {
throw new Error('AsyncHelpers#resolve() expects a callback function.');
}
if (typeof key !== 'string') {
cb(new Error('AsyncHelpers#resolve() expects `key` to be a string.'));
}
var stashed = this.stash[key];
if (!stashed) {
return done(new Error('Unable to resolve ' + key + '. Not Found'));
return cb(new Error('Unable to resolve ' + key + '. Not Found'));
}
if (typeof stashed.fn !== 'function') {
return done(null, stashed.fn);
return cb(null, stashed.fn);
}

@@ -211,5 +263,4 @@

next = once(next);
var res = null;
var args = [].concat.call([], stashed.args);
var args = stashed.args;
if (stashed.fn.async) {

@@ -224,5 +275,5 @@ args = args.concat(next);

], function (err, results) {
// update the fn so if it's called again it'll just return the true reults
// update the fn so if it's called again it'll just return the true results
stashed.fn = results[1];
done(err, stashed.fn);
cb(err, stashed.fn);
});

@@ -229,0 +280,0 @@

{
"name": "async-helpers",
"description": "Use async helpers in template engines like Handlebars and Lodash.",
"version": "0.1.3",
"description": "Use async helpers in templates with engines that typically only handle sync helpers. Handlebars and Lodash have been tested.",
"version": "0.2.0",
"homepage": "https://github.com/doowb/async-helpers",

@@ -38,3 +38,14 @@ "author": {

"mocha": "*"
}
},
"keywords": [
"async",
"engine",
"handlebars",
"helper",
"helpers",
"lodash",
"template",
"templates",
"underscore"
]
}

@@ -1,4 +0,4 @@

# async-helpers [![NPM version](https://badge.fury.io/js/async-helpers.svg)](http://badge.fury.io/js/async-helpers) [![Build Status](https://travis-ci.org/doowb/async-helpers.svg)](https://travis-ci.org/doowb/async-helpers)
# async-helpers [![NPM version](https://badge.fury.io/js/async-helpers.svg)](http://badge.fury.io/js/async-helpers) [![Build Status](https://travis-ci.org/doowb/async-helpers.svg)](https://travis-ci.org/doowb/async-helpers)
> Use async helpers in template engines like Handlebars and Lodash.
> Use async helpers in templates with engines that typically only handle sync helpers. Handlebars and Lodash have been tested.

@@ -37,8 +37,8 @@ ## Install with [npm](npmjs.org)

```js
asyncHelpers.set('upper', function (str, done) {
done(null, str.toUpperCase());
asyncHelpers.set('upper', function (str, cb) {
cb(null, str.toUpperCase());
});
```
### [.get](index.js#L81)
### [.get](index.js#L84)

@@ -56,3 +56,3 @@ Get all helpers or a helper with the given name.

### [.wrap](index.js#L132)
### [.wrap](index.js#L178)

@@ -69,3 +69,3 @@ Wrap a helper with async handling capibilities.

### [.reset](index.js#L155)
### [.reset](index.js#L199)

@@ -80,3 +80,3 @@ Reset all the stashed helpers.

### [.resolve](index.js#L178)
### [.resolve](index.js#L222)

@@ -86,3 +86,3 @@ Resolve a stashed helper by the generated id.

* `key` **{String}**: ID generated when from executing a wrapped helper.
* `done` **{Function}**: Callback function with the results of executing the async helper.
* `cb` **{Function}**: Callback function with the results of executing the async helper.

@@ -98,5 +98,3 @@ ```js

## Run tests
Install dev dependencies:

@@ -116,3 +114,3 @@

+ [github/doowb](https://github.com/doowb)
+ [twitter/doowb](http://twitter.com/doowb)
+ [twitter/doowb](http://twitter.com/doowb)

@@ -125,2 +123,2 @@ ## License

_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on April 17, 2015._
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on April 22, 2015._
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