unwrapper
unwrap an array from a function that receives `err, result`
Last updated 9 years ago by jcrugzz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install unwrapper 
SYNC missed versions from official npm registry.

unwrapper

buildstatus

A simple module that unwraps an array when deemed necessary for the result passed to a given function. The threshold in which it is deemed necessary is determined by the length of the array returned. A length of 1 is assumed to be something you want unwrapped.

install

npm i unwrapper --save

Example

var unwrap = require('unwrapper');
var api = require('./api');

//
// Pretend this API returns an [] even when you expect it to be a single entity
//
api.get('/books-by', 'barney rubble', unwrap(function (err, result) {
  // expect `result` to be a single object.
}));

API

unwrap(fn, def, pred)
unwrap(fn, pred)
unwrap(fn);
  • fn: Function to execute
  • def: Optional Default value to use if no results returned
  • pred: Predicate function for more advanced decisions than an Array of length 1

Advanced usage

In some scenarios you may want to only unwrap certain Arrays of length one (e.g. listing directories of a length 1). In this case you can pass a pred argument to unwrapper:

unwrap(fn, function (err, result) {
  //
  // Result will always be the first element in the Array
  // - return true to unwrap
  // - return false to NOT unwrap
  //
  return result.expected === 'some-expected-value';
});
Author: Jarrett Cruger

Current Tags

  • 1.1.0                                ...           latest (9 years ago)

2 Versions

  • 1.1.0                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE