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

prairie

v4.1.2

Published

Add/Modify fields on an object with composable functions.

Downloads

42

Readme

Prairie

Version 4 is ESM.

Add new fields/properties to an object based on the values of existing properties.

Composable utility functions that make it easy to edit/replace properties of objects. For best results learn about _.flow() and read the Lodash FP Guide.

All functions have been curried so they can be called with one argument at a time.

If you have used _.set() and _.update() but want more this is the library for you!

Table of Contents

createObj

Create a new object based path and value. Dot notation or an array of strings will result in nested objects.

Parameters

  • path (string | Array) The path used for key creation.
  • value any The thing used for value of key.

Examples

createObj('foo.bar', 'happy') // => { foo: { bar: 'happy' } }
createObj('foo', 'bar') // => { foo: 'bar' }
createObj('foo')('bar') // => { foo: 'bar' }
createObj('baz', { a: 1 }) // => { baz: { a: 1 } }

Returns Object New object with value placed on the last key of path.

toObject

Convert a collection into new object defined by path for key and value.

Parameters

Examples

toObject('a', 'b', [{a: 'a1', b: 'b2'}]) // => { a1: 'b2' }

Returns Object New object that is similar to map(getValue), keyBy(getKey).

setIn

Rearranged _.set args to setIn(path, object, value)

Type: function

Parameters

  • path string The path of the property to replace.
  • object Object The object that to set value on.
  • value any The value to place on path.

Examples

setIn('foo', {}, 'bar') // => { foo: 'bar' }
setIn('a', { b: 1 }, 2) // => { a: 2, b: 1 }

Returns Object New object with value set at path.

setVal

Rearranged _.set args to setVal(value, object, path)

Type: function

Examples

setVal(value, object, path)

setState

Normal lodash _.set with no rearg. setVal(object, path, value)

Examples

setVal(object, path, value)

setField

Set field. Like _.update but transformer is given the entire item instead of only the field.

Parameters

  • path string The path of the property to replace.
  • transformer Function Transformer given entire item. Return value set at path.
  • item Object The item to add or replace field on.

Returns Object Item with path updated with result of transformer.

addField

Set field like setField but only if it's value is empty.

Parameters

  • path string The path of the property to set.
  • transformer Function Transformer given entire item. Return value set at path.
  • item Object The item to update field on.

setFieldHas

Replace field only if it is already set. Transformer given entire item.

Parameters

  • path string The path of the property to replace.
  • transformer Function Transformer given entire item. Return value set at path.
  • item Object The item to update field on.

Returns Object Item with path updated with result of transformer.

setFieldWhen

Set field when boolCheck is true. Otherwise return item untouched.

Parameters

  • path string The path of the property to set.
  • transformer Function Transformer given entire item. Should return value of path.
  • boolCheck Function A function that returns true when field should be set.
  • item Object The item to update field on.

Returns Object Item with path updated with result of transformer.

replaceField

Replace field only if found. Transformer gets field value. Probably just use _.update() unless you want the check beforehand.

updateToWhen

Replace field with result of transformer when boolCheck return true.

Parameters

  • transformer Function Transformer given value at path of item. Return replacement value.
  • boolCheck Function A function that returns true when field should be replaced.
  • path string The path of the property to update.
  • item Object The item to conditionally update field on.

Examples

const toArray = updateToWhen(Array, _.isPlainObject, 'foo')
toArray({ foo: { a: 'happy' } }) // => { foo: [{ a: 'happy' }] }

Returns Object Item with conditional transformer applied to path.

updateTo

Rearranged _.update args to transformer, path, item

Parameters

  • transformer Function Transformer given value at path of item. Return replacement value.
  • path string The path of the property to get.
  • item Object The item to update field on.

Returns Object Item with transformer applied to property at path.

setFieldWith

Set field on item. Transformer given value of withId property.

Parameters

  • path string The path of the property to get.
  • withId string The path of the property to send to transformer.
  • transformer Function Transformer given value of withId property.

Returns ItemTransformer Result of transformer set at field item.

mergeFields

Replace item with result of transformer.

Parameters

  • transformer Function Accepts single param that is item. Should return a new Object.
  • item Object

Examples

mergeFields(({ a, b }) => ({ a4: a * 4, b3: b * 3 }), { a: 2, b: 3 });
// => { a: 2, b: 3, a4: 8, b3: 9 }

Returns Object Merged result of transformer on top of item.

mergeWith

Merge source on top of item.

Parameters

  • source Object Object to apply on top of item.
  • item Object Object that values of source will be applied.

Examples

mergeWith({ a: 1 })({ a: 2, b: 4 });
// => { a: 1, b: 4 }

Returns Object Merged result of surce on top of item.

mergeFieldsWith

Replace item. Transformer given value of withId property.

Parameters

  • withId string The path of the property to send to transformer.
  • transformer Function Sent item property at path of withId. Should return new Object.
  • item Object The object to work with.

Returns Object Result of transformer set at field item.

copy

Copy value of getPath to setPath only if getPath finds something. Otherwise item left untouched.

Parameters

  • getPath string The source path.
  • setPath string The destination path.
  • item Object The object to work with.

move

Move property from one names to another.

Parameters

Examples

move('foo', 'bar', { foo: 1, baz: 2 }) // => { bar: 1, baz: 2 }

Returns Object Result after the move. Value at getPath removed and added to setPath.

moveAll

Map some keys.

Parameters

  • renamer Function The function to send each key. Should return new key string.
  • renameKeys Array An array of source paths.
  • item Object The object to work with.

Examples

move('foo', 'bar', { foo: 1, baz: 2 }) // => { bar: 1, baz: 2 }

Returns Object Result after the move. Value at getPath removed and added to setPath.

renameFields

Move property from one names to another.

Parameters

  • renameObj Object Object where each key will be moved to the value path. If value is a function it is sent the old key and will return the new one.
  • item Object The object to work with.

Examples

const rename = renameFields({ foo: 'bar', bin_baz: _.camelCase })
  rename({ foo: 1, bin_baz: 2, bar: 3, other: 4 })
  // => { bar: 1, binBaz: 2, other: 4 }

Returns Object Result after the renames.

findValueAt

Return the first value of paths. 0, null, and false are valid values.

Parameters

  • getPaths Array An array of source paths.
  • item Object The item to look for values on.

Examples

findAt(['c', 'b', 'a'])({ a: 'foo', b: 'bar', c: null }) // => null
findAt(['c', 'b', 'a'])({ a: 'foo', b: false, c: '' }) // => false

Returns any The first truthy value found at one of the getPaths.

findAt

Return the first truthy value of paths.

Parameters

  • getPaths Array An array of source paths.
  • item Object The item to look for values on.

Examples

findAt(['c', 'b', 'a'])({ a: 'foo', b: 'bar', c: null }) // => 'bar'
findAt(['c', 'b', 'a'])({ a: 'foo', b: false, c: '' }) // => 'foo'

Returns any The first truthy value found at one of the getPaths.

getFields

Return an object with same keys as object argument. Values replaced with result of value selector.

Parameters

  • structuredSelector Object Object where each value is a selector accepting item.
  • item Object The object to work with.

Examples

getFields({bar: _.get('foo')}, { foo: 'happy'}) // => { bar: 'happy' }
getFields({bar: 'foo'})({ foo: 'happy'}) // => { bar: 'happy' }

Returns Object2 Result after each value is passed the item.

doProp

Return result of calling transformer with property value at path.

Parameters

  • transformer Function Transformer given value at path of item.
  • path string The path of the property to get.
  • item Object The item to get property value on.

Examples

doProp(_.isString, 'foo')({ foo: 'bar' }) // => true
doProp(_.isString, 'foo')({ foo: 2 }) // => false

propDo

Return result of calling transformer with property value at path.

Parameters

  • path string The path of the property to get.
  • transformer Function Transformer given value at path of item.
  • item Object The item to get property value on.

Examples

propDo('foo', _.isString)({ foo: 'bar' }) // => true
propDo('foo', _.isString)({ foo: 2 }) // => false

doPropOf

Create a function that will accept a path string and send its value of object to transformer.

Type: Function

hasMethodAt

Check if property has a method at path. An example of using doProp().

Type: Function

Examples

hasMethodAt(path)(object)

hasMethodOf

Check if property at path is a function. An example of using doPropOf().

Type: Function

Examples

hasMethodAt(path)(object)

transformHas

Replace entire item if has field. Transformer given value at path.

Type: Function