https://github.com/webreflection/majinbuu
Morph an array into another via Levenshtein distance
https://github.com/webreflection/majinbuu
Last synced: over 1 year ago
JSON representation
Morph an array into another via Levenshtein distance
- Host: GitHub
- URL: https://github.com/webreflection/majinbuu
- Owner: WebReflection
- License: isc
- Created: 2017-09-05T09:24:58.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-29T19:35:09.000Z (over 8 years ago)
- Last Synced: 2025-03-04T03:55:04.810Z (over 1 year ago)
- Language: HTML
- Homepage:
- Size: 167 KB
- Stars: 30
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Majin Buu
[](https://opensource.org/licenses/ISC) [](https://travis-ci.org/WebReflection/majinbuu) [](https://coveralls.io/github/WebReflection/majinbuu?branch=master) [](https://github.com/WebReflection/donate)
Apply the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) to transform / morph an Array into another, performing the least amount of needed `.splice(...)` operations.
```js
const abra = ['a', 'b', 'r', 'a'];
majinbuu(abra, ['c', 'a', 'd', 'a', 'b', 'r', 'a']);
abra; // now ['c', 'a', 'd', 'a', 'b', 'r', 'a']
```
It is also possible to intercept all splice calls using an `aura`,
which augments the `splice` method of the list,
delegating the interceptor one.
```js
const abra = ['a', 'b', 'r', 'a'];
const interceptor = {
splice(index, removal, ...items) {
console.log(index, removal, ...items);
abra.splice.apply(abra, arguments);
}
};
const aura = majinbuu.aura(interceptor, abra);
majinbuu(aura, ['c', 'a', 'd', 'a', 'b', 'r', 'a']);
// 0 0 "c"
// 2 0 "d" "a"
```
The optional third argument avoid processing grids that are too big (comparing lists with too many items).
```js
const noMoreThan1K = 1000;
majinbuu(list1, list2, noMoreThan1K);
```
If the square of the `list1` and `list2` product is higher than `noMoreThan1K`,
the splice operation will remove all `list1` items and push all `list2`.
### Compatibility
Every. JavaScript. Engine.