Skip to content

Gozala/extract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

extract

Build Status

Library provides a decorator function that can be used to for extracting arrays parameters into arguments. This just a small taste of upcoming destructuring in JS. Here is an example:

[
  ["a", "b"],
  ["c", "d"],
  ["e", "f"]
].reduce(extract(function(result, first, second) {
  result[first] = second
  return result
}), {}) // => {a: "b", c: "d", e: "f"}

As you can see from the example first array parameter was extracted and applied to a decorated function as arguments. Also note that implementation is smart about first array param extraction and ignores empty arrays and only lears about index of extraction param ones:

function keyvalues(object) {
  return Object.keys(object).map(function(key) {
    return [key, object[key]]
  })
}

var actual = keyValues({
  a: "b",
  c: "d",
  e: "f"
}).reduce(extract(function(result, key, value) {
  result.push(key, value)
  return result
}), [])

Finally if you need to extract other parameter than first arary argument, or wish to be explicit there is an optional second argument to do exactly that.

Install

npm install extract

About

Decorator for extracting arrays into arguments

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published