0.0.5 • Published 5 years ago

@sullux/fp-light-set v0.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

home

fp-light-set

npm i @sullux/fp-light-set
source
test

The set function returns a newly-composed object or array with the given deep property set to the given value.

set

set(path, value, input)

  • path: string, number or array of strings/numbers. This is the key, index or path of keys/indexes to the property of the input object.
  • value: the new value to set on the input object.
  • input: the object or array on which the new value is being set.

The set function creates a deep copy of the input with the new value interpolated into the result at the offset denoted by the path. If parts of the path are missing, those parts are created. If a path part is a number, it is assumed to be an offset into an array; otherwise, it is assumed to be a property of an object.

The set function can be particularly useful in mapping operations as in the following example.

const { map } = require('@sullux/fp-light-map')
const { set } = require('@sullux/fp-light-set')

const people = [
  { name: 'jane' },
  { name: 'jamal' },
  { name: 'zhang' },
]

const inactive = map(set('active', false))

inactive(people)
/*
[
  { name: 'jane', active: false },
  { name: 'jamal', active: false },
  { name: 'xing', active: false },
]
*/

setOn

setOn(input, path, value)

Same functionality as set, but with the arguments rearranged so that the value is the most significant (last) argument.

const records = pipe(
  map(setOn({}, 'value'))
  map(set('createTime', Date.now())),
  Array.from,
  setOn({}, 'records')
)

console.log(records([1, 2, 3]))
/*
{
  Records: [
    { value: 1, createTime: 1552058411957 },
    { value: 2, createTime: 1552058411958 },
    { value: 3, createTime: 1552058411959 },
  ]
}
*/

setValue

setValue(input, key, value)

  • input: the object on which to set the value
  • key: the input key or index at which to set the value
  • value: the new value of the key or index

The setValue function creates a shallow copy of the input object/array with the value interpolated into the result at the given key.

const { setValue } = require('@sullux/fp-light-set')

setValue({ foo: 7, bar: 'baz' }, 'foo', 42)
// { foo: 42, bar: 'baz' }

setValue([7, 8, 13], 1, 42)
// [7, 42, 13]

setObjectValue

setObjectValue(input, key, value)

The setObjectValue function creates a shallow copy of the input object with the value interpolated into the result at the given key. This is the default behavior of setValue when the input is not an array.

setArrayValue

setArrayValue(input, key, value)

The setArrayValue function creates a shallow copy of the input array with the value interpolated into the result at the given key. This is the default behavior of setValue when the input is an array.

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago