New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

func-obj

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

func-obj

Functions over keys and values

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

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.

Keywords

map

FAQs

Package last updated on 07 Jun 2019

Did you know?

Socket

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.

Install

Related posts