array-edit-steps
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

array-edit-steps

Find the difference between two arrays and compute the minimal steps to transform one array to another.

Installing

Using npm:

$ npm install array-edit-steps

Using yarn:

$ yarn add array-edit-steps

Using unpkg CDN:

<script src="https://unpkg.com/array-edit-steps/umd/array-edit-steps.min.js"></script>

Usage

const getEditSteps = require('array-edit-steps')

const steps = getEditSteps(['w', 'x', 'y', 'z'], ['x', 'y', 'u', 'v'])

// steps = [
//   [ 'e', 3, 'v' ], -- Edit item at index 3 to 'v' --> ['w', 'x', 'y', 'v']
//   [ 'i', 3, 'u' ], -- Insert 'u' at index 3       --> ['w', 'x', 'y', 'u', 'v']
//   [ 'd', 0 ],      -- Delete item at index 0      --> ['x', 'y', 'u', 'v']
// ]

With options provided as the third argument:

const getEditSteps = require('array-edit-steps')

const steps = getEditSteps(
  [{ id: 1 }, { id: 2 }, { id: 3 }],
  [{ id: 2 }, { id: 1 }, { id: 3 }],
  {
    preferEdit: true,                // prefer edit over insert/delete
    equals: (a, b) => a.id === b.id, // custom equal function
  }
)

// steps = [ [ 'e', 1, { id: 1 } ], [ 'e', 0, { id: 2 } ] ]

Strings and array-like objects are supported as well.

TypeScript

array-edit-steps includes TypeScript definitions.

import getEditSteps from 'array-edit-steps'

const steps = getEditSteps('arr', 'array')

Package Sidebar

Install

npm i array-edit-steps

Weekly Downloads

4

Version

0.1.4

License

MIT

Unpacked Size

22.3 kB

Total Files

10

Last publish

Collaborators

  • klo112358