Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
objectfn
Advanced tools
Readme
map
, reduce
, forEach
, and filter
for plain objects. Lazy evaluation, supports functional and imperative syntax, no dependencies.
I wanted a library that has no dependencies and gives me the basic map/reduce/filter for use on objects. Any existing library I found has boatloads of dependencies, provides tons more extra tools, and/or is unmaintained. So here's ObjectFn
, just for you!
Also, big props to @declandewet for the initial implementation of this library!
Using a terminal:
$ npm install objectfn -S
Usage is straightforward. Just import what you need and use it on an object.
Takes data first, callback last.
const {map, reduce, filter, forEach} = require('objectfn')
const obj = { foo: 'bar', wow: 'doge' }
map(obj, (val) => val.toUpperCase())
// { foo: 'BAR', wow: 'DOGE' }
reduce(obj, (acc, val, key) => (acc[key.toUpperCase()] = val, acc), {})
// { FOO: 'bar', WOW: 'doge' }
filter(obj, (val, key) => key !== 'foo')
// { wow: 'doge' }
forEach(obj, console.log.bind(console))
// bar foo 0 { foo: 'bar', wow: 'doge' }
// doge wow 1 { foo: 'bar', wow: 'doge' }
Takes callback first, data last. Each method is automatically curried.
const {map, reduce, filter, forEach} = require('objectfn')
const obj = { foo: 'bar', wow: 'doge' }
const upcaseValues = map((val) => val.toUpperCase())
upcaseValues(obj)
// { foo: 'BAR', wow: 'DOGE' }
const upcaseKeys = reduce((acc, val, key) => (acc[key.toUpperCase()] = key, acc), {})
upcaseKeys(obj)
// { FOO: 'bar', WOW: 'doge' }
const ignoreFoo = filter((val, key) => key !== 'foo')
ignoreFoo(obj)
// { wow: 'doge' }
const logValues = forEach(console.log.bind(console))
logValues(obj)
// bar foo 0 { foo: 'bar', wow: 'doge' }
// doge wow 1 { foo: 'bar', wow: 'doge' }
(value, key, index, object)
with the exception of reduce
.
value
is the current key's valuekey
is the current key's nameindex
is the 0-based index of the current keyobject
is the original object.reduce
has a method signature of (accumulator, value, key, index, object)
.
accumulator
is any initial value onto which you want to iteratively reduce from object
.reduce
In objectfn
, the act of passing an accumulator to the reduce
method is required, which is better for readability/accessibility (developer intentions are made more obvious), has no immediate disadvantages and is one of the two reasons objectfn
is able to support both functional and imperative syntaxes.
This means that this will work:
let obj = { one: 1, two: 2, three: 3, four: 4 }
reduce(obj, (acc, val) => acc + val, 0) // => 10
But this will not:
let obj = { one: 1, two: 2, three: 3, four: 4 }
reduce(obj, (prevVal, currVal) => prevVal + currVal) // => wat?
this
objectfn
offers no mechanism for binding the this
context of the callback via the last parameter. This is one of two reasons why objectfn
is able to support both functional and imperative syntaxes. If you want this behavior, it is still possible (and far more readable) to do so using Function.prototype.bind
:
map(obj, fn.bind(/* value to use as `this` goes here */))
FAQs
map, reduce, and filter for objects, lazy evaluation and no dependencies
The npm package objectfn receives a total of 22 weekly downloads. As such, objectfn popularity was classified as not popular.
We found that objectfn demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.