Skip to content

Commit 66a2782

Browse files
committed
test(util): 💍 add failing sfx() test
1 parent 5a0f830 commit 66a2782

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

packages/json-joy/src/util/diff/__tests__/str.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
import {PATCH_OP_TYPE, type Patch, diff, diffEdit, overlap, normalize, apply, src, dst, invert} from '../str';
1+
import {PATCH_OP_TYPE, type Patch, diff, diffEdit, overlap, normalize, apply, src, dst, invert, pfx, sfx} from '../str';
22
import {assertPatch} from './util';
33

4+
describe('pfx()', () => {
5+
test('finds common prefixes', () => {
6+
expect(pfx('abc', 'b')).toEqual(0);
7+
expect(pfx('abc', 'a')).toEqual(1);
8+
expect(pfx('abc', 'ab')).toEqual(2);
9+
expect(pfx('abc', 'abc')).toEqual(3);
10+
expect(pfx('abc', 'abcd')).toEqual(3);
11+
expect(pfx('abc', 'abcde')).toEqual(3);
12+
expect(pfx('👨‍🍳', '👨‍🍳')).toEqual(5);
13+
expect(pfx('👨‍🍳', '👨‍🍳chef')).toEqual(5);
14+
expect(pfx('👨‍🍳chef', '👨‍🍳')).toEqual(5);
15+
expect(pfx('👨‍🍳👨‍🍳', '👨‍🍳')).toEqual(5);
16+
expect('👨‍🍳chef'.slice(0, 5)).toBe('👨‍🍳');
17+
});
18+
});
19+
20+
describe('sfx()', () => {
21+
test('finds common suffixes', () => {
22+
expect(sfx('abc', 'b')).toEqual(0);
23+
expect(sfx('abc', 'c')).toEqual(1);
24+
expect(sfx('abc', 'bc')).toEqual(2);
25+
expect(sfx('abc', 'abc')).toEqual(3);
26+
expect(sfx('abc', '_abc')).toEqual(3);
27+
expect(sfx('abc', 'abcd')).toEqual(0);
28+
expect(sfx('👨‍🍳', '👨‍🍳')).toEqual(5);
29+
// expect(sfx('👨‍🍳', '👨‍🍳chef')).toEqual(5);
30+
// expect(sfx('👨‍🍳chef', '👨‍🍳')).toEqual(5);
31+
// expect(sfx('👨‍🍳👨‍🍳', '👨‍🍳')).toEqual(5);
32+
});
33+
});
34+
435
describe('normalize()', () => {
536
test('joins consecutive same type operations', () => {
637
expect(

packages/json-joy/src/util/diff/str.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ const diffNoCommonAffix = (src: string, dst: string): Patch => {
388388
* @param txt2 Second string.
389389
* @return The number of characters common to the start of each string.
390390
*/
391-
export const pfx = (txt1: string, txt2: string) => {
391+
export const pfx = (txt1: string, txt2: string): number => {
392392
if (!txt1 || !txt2 || txt1.charAt(0) !== txt2.charAt(0)) return 0;
393393
let min = 0;
394394
let max = Math.min(txt1.length, txt2.length);

0 commit comments

Comments
 (0)