
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
string-differ
Advanced tools
A library to compare and transform strings. This library can:
npm i string-differ
This library uses a rough implementation of Myers algorithm to calculate the shortest edit script between two strings. It assumes that a string can be converted to another using 3 types of operations:
These types describe the operations that can be performed on string s1 to convert it to string s2.
Note: string-differ v1 uses Levenshtein distance to compare strings and is usually slower than v2, especially for strings with smaller differences.
This method generates an operation for every character in the base string (s1).
import { getStepsForTransformation } from 'string-differ';
const s1 = 'initial string';
const s2 = 'final string';
const steps = getStepsForTransformation("Char", { s1, s2 });
console.log(steps);
will output:
[
{ type: 'delete', value: 0 },
{ type: 'delete', value: 1 },
{ type: 'insert', value: 'f' },
{ type: 'retain', value: 2 },
{ type: 'delete', value: 3 },
{ type: 'delete', value: 4 },
{ type: 'insert', value: 'n' },
{ type: 'retain', value: 5 },
{ type: 'retain', value: 6 },
{ type: 'retain', value: 7 },
{ type: 'retain', value: 8 },
{ type: 'retain', value: 9 },
{ type: 'retain', value: 10 },
{ type: 'retain', value: 11 },
{ type: 'retain', value: 12 },
{ type: 'retain', value: 13 }
]
For delete and retain operations, the value field in the output contains the character's index in the base string (s1). However, for insert operations, the value field contains the character to be inserted.
This is a minor optimization over character operations. If the output type is specified as "Range", certain consecutive operations of the same type are grouped together.
import { getStepsForTransformation } from 'string-differ';
const s1 = 'initial string';
const s2 = 'final string';
const steps = getStepsForTransformation("Range", { s1, s2 });
console.log(steps);
will output:
[
{ type: 'delete', startIndex: 0, endIndex: 1 },
{ type: 'insert', value: 'f' },
{ type: 'retain', startIndex: 2, endIndex: 2 },
{ type: 'delete', startIndex: 3, endIndex: 4 },
{ type: 'insert', value: 'n' },
{ type: 'retain', startIndex: 5, endIndex: 13 }
]
For "Range" output type, the delete and retain operations will have startIndex and endIndex fields which represent the range of indices in the base string (s1). The insert operations, however, contain the value field which may specify one or more than one characters to be inserted.
Given a set of operations, and a base string (s1), the transformString function can be used to apply the steps to s1 to get the resultant string s2.
import { transformString } from 'string-differ';
const operations = [
{ type: 'delete', startIndex: 0, endIndex: 1 },
{ type: 'insert', value: 'f' },
{ type: 'retain', startIndex: 2, endIndex: 2 },
{ type: 'delete', startIndex: 3, endIndex: 4 },
{ type: 'insert', value: 'n' },
{ type: 'retain', startIndex: 5, endIndex: 13 }
];
const s1 = 'base string';
const s2 = transformString("Range", s1, operations);
console.log(s2);
will output:
'final string'
import { transformString } from 'string-differ';
const operations = [
{ type: 'delete', value: 0 },
{ type: 'delete', value: 1 },
{ type: 'insert', value: 'f' },
{ type: 'retain', value: 2 },
{ type: 'delete', value: 3 },
{ type: 'delete', value: 4 },
{ type: 'insert', value: 'n' },
{ type: 'retain', value: 5 },
{ type: 'retain', value: 6 },
{ type: 'retain', value: 7 },
{ type: 'retain', value: 8 },
{ type: 'retain', value: 9 },
{ type: 'retain', value: 10 },
{ type: 'retain', value: 11 },
{ type: 'retain', value: 12 },
{ type: 'retain', value: 13 }
];
const s1 = 'base string';
const s2 = transformString("Char", s1, operations);
console.log(s2);
will output:
'final string'
This library is distributed under the MIT License. See LICENSE for more information.
FAQs
A Javascript library to compare and transform strings.
We found that string-differ demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.