Gathering detailed insights and metrics for align-arr
Gathering detailed insights and metrics for align-arr
Gathering detailed insights and metrics for align-arr
Gathering detailed insights and metrics for align-arr
A Typescript library to find the minimal edit path between two generic arrays.
npm install align-arr
Typescript
Module System
Node Version
NPM Version
47.8
Supply Chain
90.3
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
1,443
Last Day
1
Last Week
16
Last Month
36
Last Year
284
MIT License
4 Stars
27 Commits
1 Watchers
4 Branches
1 Contributors
Updated on Mar 16, 2024
Latest Version
1.0.0
Package Id
align-arr@1.0.0
Unpacked Size
5.29 kB
Size
2.43 kB
File Count
3
NPM Version
8.3.1
Node Version
16.14.0
Published on
Feb 18, 2023
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
77.8%
16
Compared to previous week
Last Month
-14.3%
36
Compared to previous month
Last Year
-5.3%
284
Compared to previous year
5
npm i align-arr
or
yarn add align-arr
Find the difference between two arrays of number
.
1import { align } from 'align-arr'; 2 3const source = [1, 3, 3]; 4const target = [1, 2, 3]; 5 6const alignment: Edit<number>[] = align(source, target); 7 8console.log(alignment);
1[ 2 { 3 operation: 'equal', 4 source: { position: 0, data: 1 }, 5 target: { position: 0, data: 1 }, 6 cost: 0 7 }, 8 { 9 operation: 'substitute', 10 source: { position: 1, data: 3 }, 11 target: { position: 1, data: 2 }, 12 cost: 1 13 }, 14 { 15 operation: 'equal', 16 source: { position: 2, data: 3 }, 17 target: { position: 2, data: 3 }, 18 cost: 0 19 } 20]
The console logs an array of Edit
objects describing the difference between source
and target
.
1export type Edit<S = any, T = S> = { 2 operation: Operation; 3 source: Chunk<S>; 4 target: Chunk<T>; 5 cost: number; 6}; 7 8export type Operation = 'equal' | 'insert' | 'delete' | 'substitute'; 9 10export type Chunk<T = any> = { 11 position: number; 12 data?: T; 13};
By default, the algorithm compares two elements as a === b
, and has a fixed cost of 1
for each operation.
In this example, we override the equals
method to compare between a number
and a string
, and assign a fixed cost of 10
to the substitute
operation, which causes the algorithm to favour insert
and delete
instead.
1const source = [1, 3, 3]; 2const target = ['1', '2', '3']; 3 4const alignment = align(source, target, { 5 equals: (a, b) => a === Number.parseFloat(b), 6 insCost: (a) => 1, 7 delCost: (a) => 1, 8 subCost: (a, b) => 10, 9}); 10 11console.log(alignment);
1[ 2 { 3 operation: 'equal', 4 source: { position: 0, data: 1 }, 5 target: { position: 0, data: '1' }, 6 cost: 0 7 }, 8 { 9 operation: 'insert', 10 source: { position: 1, data: undefined }, 11 target: { position: 1, data: '2' }, 12 cost: 1 13 }, 14 { 15 operation: 'delete', 16 source: { position: 1, data: 3 }, 17 target: { position: 2, data: undefined }, 18 cost: 1 19 }, 20 { 21 operation: 'equal', 22 source: { position: 2, data: 3 }, 23 target: { position: 2, data: '3' }, 24 cost: 0 25 } 26]
To contribute to align-arr, follow these steps:
git checkout -b <branch_name>
.git commit -m '<commit_message>'
git push origin <project_name>/<location>
Alternatively see the GitHub documentation on creating a pull request.
Thanks to the following people who have contributed to this project:
If you want to contact me you can reach me at guy.manzurola@gmail.com.
This project uses the following license: MIT.
No vulnerabilities found.
No security vulnerabilities found.