array-differences
Find the differences between two arrays and their positions
Last updated 7 months ago by dherault .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install array-differences 
SYNC missed versions from official npm registry.

array-differences

npm version

Find the differences between two arrays and their positions.

Installation

npm install array-differences --save

or

yarn add array-differences

Usage

import arrayDifferences from 'array-differences'

const input = [0, 1, 1, 4, 1, 1]
const output = [111, 1, 112, 1, 4, 1]
const diff = arrayDifferences(input, output)

--> [['modified', 0, 111], ['inserted', 2, 112], ['deleted', 5]]

Using a custom comparison function:

const diff = arrayDifferences(
  [{ name: 'John' }, { name: 'Marie' }],
  [{ name: 'John' }, { name: 'Marie' }, { name: 'Chloe' }],
  (a, b) => a.name === b.name
)

--> [['inserted', 2, { name: 'Chloe' }]]

You can recontruct the second array by iterating over the differences on the first array or using the utility:

import { reconstructArray } from 'array-differences'

const reconstructed = reconstructArray(input, diff) // builds the output

// You can reconstruct in place:
reconstructArray(input, diff, true) // modifies input to recreate output from diff

Undefined values

The algorythm does not support arrays containing undefined values. Please replace your undefined values with a placeholder before applying it.

Performance on large arrays

Entry one is length n,

Entry two is length m,

Perfomance is n * n * m.

Therefore, it works best on small arrays. Performance is best when differences indexes are close to one another is the array.

Say thanks

Show your gratitude with a star on GitHub!

License

MIT

Current Tags

  • 1.1.5                                ...           latest (7 months ago)

7 Versions

  • 1.1.5                                ...           7 months ago
  • 1.1.4                                ...           8 months ago
  • 1.1.3                                ...           8 months ago
  • 1.1.2                                ...           8 months ago
  • 1.1.1                                ...           8 months ago
  • 1.1.0                                ...           8 months ago
  • 1.0.0                                ...           8 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |