npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

func-obj

v1.1.0

Published

Functions over keys and values

Downloads

17

Readme

func-obj

Function over object keys and values into a new object

pipeline status coverage report

Install

$ npm install func-obj

Usage

const { map, filter, fold } = require('func-obj');

const newObject = map({foo: 'bar'}, (key, value) => [value, key]);
//=> {bar: 'foo'}

newObject = await map({foo: 'bar'}, (key, value) => Promise.resolve([value, key]), {thenable: true});
//=> {bar: 'foo'}

newObject = filter(object, (key, value) => value === true);
//=> {foo: true}

newObject = await filter(
  {foo: null, bar: {foo: null}},
  (key, value) => new Promise((resolve, reject)=> resolve(value !== null)),
  {deep: true, thenable: true}
);
//=> {foo: true}

newObject = filter({foo: true, bar: false}, ['bar']);
//=> {bar: false}

const obj = { foo: 2, bar: { foo: 1, bar: { foo: 1, bar: 1 } } };
newObject = await fold(obj, (ack, a, b) => ack + b, 1, { deep: true, thenable: true });
// => 5

newObject = await fold(obj, (ack, a, b) => ack + b, 0, { deep: true });
// => 5

API

map(source, mapper, options?)

source

Type: object

Source object to copy properties from.

mapper

Type: Function

Mapping function.

  • It has signature mapper(sourceKey, sourceValue, source).
  • It must
    • return a two item array: [targetKey, targetValue].
    • or a promise that resolve on two item array: [targetKey, targetValue].

options

Type: object

deep

Type: boolean Default: false

Recurse nested objects and objects in arrays.

target

Type: object Default: {}

Target object to map properties on to.

thenable

Type: object Default: false

Tells if the mapper is handle like a promise or not.

filter(source, filter, options?)

filter(source, includeKeys, options?)

source

Type: object

Source object to filter properties from.

filter

Type: Function

If is a function, predicate function that determines whether a property should be assigned to the new object. The function has the signature filterFunction(sourceKey, sourceValue, source). It must

  • return a boolean.
  • or a promise that resolve on a boolean.

includeKeys

Type: string[]

Array of property names that should be assigned to the new object.

options

Type: object

deep

Type: boolean Default: false

Recurse nested objects and objects in arrays.

thenable

Type: object Default: false

Tells if the mapper is handle like a promise or not.

fold(source, reduce, accumulator, options?)

source

Type: object

Source object to fold properties from.

reduce

Type: Function

  • It has signature reducer(accumulator, key, value, source).
  • It must
    • return a value that results from the reduction.
    • or a promise that resolve a value that results from the reduction.

accumulator

Type: any

A value to use as the first argument.

options

Type: object

deep

Type: boolean Default: false

Recurse nested objects and objects in arrays.

thenable

Type: object Default: false

Tells if the mapper is handle like a promise or not.