-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Erik Michelson <opensource@erik.michelson.eu>
- Loading branch information
1 parent
0761d1d
commit d937751
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<!-- | ||
SPDX-FileCopyrightText: 2024 Erik Michelson <opensource@erik.michelson.eu> | ||
SPDX-License-Identifier: MIT | ||
--> | ||
|
||
# array-changeset | ||
|
||
This tiny library can be used to calculate the changeset between two arrays and applying these changes to another array. | ||
|
||
The library is **ESM-only**. See [this note by sindresorhus][esm-note] to learn more about that. | ||
|
||
## Installation | ||
|
||
### Using npm | ||
|
||
```shell | ||
npm install array-changeset | ||
``` | ||
|
||
## Usage | ||
|
||
```javascript | ||
import { Changeset } from 'array-changeset' | ||
|
||
const arrayA = ['foo', '!'] | ||
const arrayB = ['foo', 'bar'] | ||
|
||
// Use fromDiff to create a changeset from the difference between two arrays | ||
const changeset = Changeset.fromDiff(arrayA, arrayB) | ||
|
||
// Check if there are any changes from arrayA to arrayB | ||
console.log(changeset.hasChanges()) // true | ||
|
||
// Access the added and removed elements | ||
console.log(changeset.getAdded()) // ['bar'] | ||
console.log(changeset.getRemoved()) // ['!'] | ||
|
||
// Modify the changeset (arrayA and arrayB are not modified) | ||
changeset.add('!') | ||
changeset.remove('bar') | ||
changeset.remove('more') | ||
changeset.uniquelyAddItem('unique', (a, b) => a === b) // The comparator is optional and defaults to fast-deep-equal | ||
changeset.uniquelyAddItem('unique', (a, b) => a === b) | ||
|
||
console.log(changeset.getAdded()) // ['unique'] | ||
console.log(changeset.getRemoved()) // ['more'] | ||
|
||
// Apply the changeset to another array (without modifying arrayC) | ||
const arrayC = ['much', 'more', 'foo', '!'] | ||
const modifiedC = changeset.applyOnto(arrayC) | ||
console.log(modifiedC) // ['much', 'foo', '!', 'unique'] | ||
|
||
// Clear the changeset | ||
changeset.clear() | ||
|
||
console.log(changeset.hasChanges()) // false | ||
``` | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. | ||
|
||
[esm-note]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c |