upsert

Insert or updates an item in an array

upsert :: (Function predicate, Any item, Array input) => Array output

description

Inserts or updates (upserts) an item in an input array. The predicate function checks for the item's existence in the input array. If it does not exist, a new array is returned with the values from the input array and the item appended to them.

Like all functions in conductor, upsert is a pure function and will not modify the input array.

example

inserting a new value

import { upsert } from 'conductor'

const characters = [{ id: 1, name: 'Luke' }]
const han = { id: 2, name: 'Han' }
const hasHan = character => character.id === 2

upsert(hasHan, han, characters) // [{ id: 1, name: 'Luke' }, { id: 2, name: 'Han' }]

updating a value

import { upsert } from 'conductor'

const characters = [{ id: 4, firstname: 'Anakin' }]
const darthVader = { id: 4, firstname: 'Darth Vader' }
const hasAnakin = character => character.id === 4

upsert(hasAnakin, handarthVader characters) // [{ id: 4, name: 'Darth Vader' }]

Last updated