Skip to content

Commit 21192e6

Browse files
committed
feat: add getDifference for strings
1 parent 030ff2d commit 21192e6

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

src/string.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,33 @@ export function repeatEveryChars(str: string, repeats: number): string {
7777

7878
return result
7979
}
80+
81+
export interface DifferenceResult {
82+
index: number
83+
old?: string
84+
new?: string
85+
}
86+
87+
export function getDifference(
88+
leftStr: string,
89+
rightStr: string
90+
): DifferenceResult[] {
91+
const differenceResult: DifferenceResult[] = []
92+
if (leftStr === rightStr) return differenceResult
93+
94+
for (
95+
let index = 0;
96+
index < Math.max(leftStr.length, rightStr.length);
97+
index++
98+
) {
99+
if (leftStr[index] !== rightStr[index]) {
100+
differenceResult.push({
101+
index: index,
102+
old: leftStr[index],
103+
new: rightStr[index]
104+
})
105+
}
106+
}
107+
108+
return differenceResult
109+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`getDifference > should return the difference between two strings 1`] = `
4+
[
5+
{
6+
"index": 0,
7+
"new": "b",
8+
"old": "f",
9+
},
10+
{
11+
"index": 1,
12+
"new": "a",
13+
"old": "o",
14+
},
15+
{
16+
"index": 2,
17+
"new": "r",
18+
"old": undefined,
19+
},
20+
]
21+
`;
22+
23+
exports[`getDifference > should return the difference between two strings 2`] = `
24+
[
25+
{
26+
"index": 3,
27+
"new": "o",
28+
"old": undefined,
29+
},
30+
]
31+
`;
32+
33+
exports[`getDifference > should return the difference between two strings 3`] = `
34+
[
35+
{
36+
"index": 0,
37+
"new": "f",
38+
"old": undefined,
39+
},
40+
{
41+
"index": 1,
42+
"new": "o",
43+
"old": undefined,
44+
},
45+
{
46+
"index": 2,
47+
"new": "o",
48+
"old": undefined,
49+
},
50+
]
51+
`;
52+
53+
exports[`getDifference > should return the difference between two strings 4`] = `
54+
[
55+
{
56+
"index": 0,
57+
"new": undefined,
58+
"old": "f",
59+
},
60+
{
61+
"index": 1,
62+
"new": undefined,
63+
"old": "o",
64+
},
65+
{
66+
"index": 2,
67+
"new": undefined,
68+
"old": "o",
69+
},
70+
]
71+
`;

test/string.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest'
22

3-
import { capitalize, generateChars, randomToken } from '../src/string.js'
3+
import { capitalize, generateChars, randomToken, getDifference } from '../src/string.js'
44

55
describe('generateChars', () => {
66
test('should be defined', () => {
@@ -61,3 +61,16 @@ describe('randomToken', () => {
6161
}
6262
})
6363
})
64+
65+
describe('getDifference', () => {
66+
test('should be defined', () => {
67+
expect(getDifference).toBeDefined()
68+
})
69+
70+
test('should return the difference between two strings', () => {
71+
expect(getDifference('fo', 'bar')).toMatchSnapshot()
72+
expect(getDifference('foo', 'fooo')).toMatchSnapshot()
73+
expect(getDifference('', 'foo')).toMatchSnapshot()
74+
expect(getDifference('foo', '')).toMatchSnapshot()
75+
})
76+
})

0 commit comments

Comments
 (0)