3.0.0 • Published 5 years ago

diff-sorted-array v3.0.0

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

Build Status Coverage

diff-sorted-array

Diff two sorted array for best performance.

Install

$ npm i diff-sorted-array

Usage

const {diff, justDiff, asc, desc} = require('diff-sorted-array')

diff(a, b)

  • a Array
  • b Array
const a = [2, 3, 1]
const b = [3, 4, 2]

const result = diff(a, b, sorter)

result.unchanged
// [2, 3]

result.added
// [4]

result.deleted
// [1]

justDiff(a, b, sorter)

  • sorter Function(a: any, b: any): number the compareFunction of Array.prototype.sort(compareFunction)

Sometimes we want to do the sorting ourself, so that we can manage the process to increase performance.

justDiff only accepts two arrays that both have already been sorted to speed up the matching.

const sorter = (a, b) => a > b
  ? 1
  : - 1

const a = [2, 3, 1]
const b = [3, 4, 2]

a.sort(sorter)
b.sort(sorter)

justDiff(a, b, sorter)
// The same result as above

asc and desc

Built-in sorter to sort arrays in ascending or descending order.

License

MIT