conductor
Why I'm building conductorGitHub
v1.5.0
v1.5.0
  • Introduction
  • Overview
    • Introduction
    • Core concepts
  • API reference
    • always
    • append
    • apply
    • arity
    • branch
    • capitalize
    • compose
    • concat
    • curry
    • curryN
    • delay
    • dump
    • entries
    • equals
    • equalsBy
    • factory
    • filter
    • findIndex
    • flatten
    • flip
    • forEach
    • get
    • head
    • ifElse
    • identity
    • into
    • isPromise
    • iterate
    • join
    • keys
    • map
    • merge
    • mergeBy
    • next
    • not
    • pluck
    • prepend
    • random
    • reduce
    • replace
    • slice
    • some
    • split
    • take
    • then
    • toLowerCase
    • transduce
    • transformers
      • transformers/filter
      • transformers/map
    • type
    • upsert
    • values
  • Guides
    • example use cases
    • checkGuards
Powered by GitBook
On this page
  • description
  • example
  • inserting a new value
  • updating a value
  1. API reference

upsert

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' }]
PrevioustypeNextvalues

Last updated 7 years ago