Skip to content

pullAllWith does not abide by the same contract as pullAllBy or pullAll #6004

@robross0606

Description

@robross0606

Unlike pullAll and pullAllBy, pullAllWith appears to match every item in array with every item in values until one of them returns true. This means it does not abide by the same logical contract as the other two functions.

This leads to pullAllWith removing all items from arrays if the comparator uses !==:

Example

Code

const nixEm = [{ name: 'moe' }, { name: 'curly' }]
const keepEm = [{ name: 'manny' }, { name: 'shemp' }]

{
  const stooges = ['manny', 'moe', 'curly', 'shemp']
  _.pullAll(
    stooges,
    nixEm.map(n => n.name)
  )
  console.log(`pullAllResults: ${JSON.stringify(stooges)}`)
}

{
  const stooges = [{ name: 'manny' }, { name: 'moe' }, { name: 'curly' }, { name: 'shemp' }]
  _.pullAllBy(stooges, nixEm, 'name')
  console.log(`pullAllByResults: ${JSON.stringify(stooges)}`)
}

{
  const stooges = [{ name: 'manny' }, { name: 'moe' }, { name: 'curly' }, { name: 'shemp' }]
  _.pullAllWith(stooges, keepEm, (a, b) => {
    const match = a.name !== b.name
    console.log(`${a.name} !== ${b.name} = ${match ? 'pull' : 'keep'}`)
    return match
  })
  console.log(`pullAllWithResults: ${JSON.stringify(stooges)}`)
}

Output

'pullAllResults: ["manny","shemp"]'
'pullAllByResults: [{"name":"manny"},{"name":"shemp"}]'
"manny !== manny = keep"
"moe !== manny = pull"
"curly !== manny = pull"
"shemp !== manny = pull"
"manny !== shemp = pull"
"pullAllWithResults: []"

Note how pullAll and pullAllBy result in the expected results while pullAllWith continues to compare everything in arrays with everything in values until it ultimately pulls everything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions