Skip to content

Commit ff2e27f

Browse files
committed
feat: string util to slice pattern from start of string
1 parent dc80605 commit ff2e27f

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

electron/common/string/__tests__/string-utils.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
equalsIgnoreCase,
33
includesIgnoreCase,
4+
sliceStart,
45
toUpperSnakeCase,
56
} from '../string.utils';
67

@@ -107,4 +108,32 @@ describe('string-utils', () => {
107108
expect(result).toEqual('FOO_BAR_BAZ');
108109
});
109110
});
111+
112+
describe('#sliceStart', () => {
113+
test('when the pattern is found at the start of the string then returns the original string, the matched pattern, and the remaining string', () => {
114+
const text = 'foo bar baz';
115+
const regex = /^foo/;
116+
117+
const result = sliceStart({ text, regex });
118+
119+
expect(result).toEqual({
120+
match: 'foo',
121+
original: 'foo bar baz',
122+
remaining: ' bar baz',
123+
});
124+
});
125+
126+
test('when the pattern is not found at the start of the string then returns the original string, undefined for the matched pattern, and the original string for the remaining string', () => {
127+
const text = 'foo bar baz';
128+
const regex = /^bar/;
129+
130+
const result = sliceStart({ text, regex });
131+
132+
expect(result).toEqual({
133+
match: undefined,
134+
original: 'foo bar baz',
135+
remaining: 'foo bar baz',
136+
});
137+
});
138+
});
110139
});

electron/common/string/string.utils.ts

+55
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,58 @@ export function equalsIgnoreCase(a: Maybe<string>, b: Maybe<string>): boolean {
1515
export function toUpperSnakeCase(value: string): string {
1616
return snakeCase(value).toUpperCase();
1717
}
18+
19+
/**
20+
* Inspired by Ruby's String#slice method.
21+
* Slices the pattern from the start of the input text.
22+
* Returns an object containing the matched pattern, original text, and remaining text.
23+
*/
24+
export function sliceStart(options: {
25+
/**
26+
* The input text to slice the pattern from.
27+
*/
28+
text: string;
29+
/**
30+
* The pattern to match at the start of the input text.
31+
* Must include the ^ anchor to match the start of the string.
32+
*/
33+
regex: RegExp;
34+
}): {
35+
/**
36+
* The string that matched the pattern at the start of the input text.
37+
*/
38+
match?: string;
39+
/**
40+
* The original input text echoed back.
41+
*/
42+
original: string;
43+
/**
44+
* The remaining text after the matched pattern.
45+
*/
46+
remaining: string;
47+
} {
48+
const { text, regex } = options;
49+
50+
// If a pattern is found, the result will be an array; otherwise, null.
51+
// The first element of the array will be the matched text.
52+
const matchResult = text.match(regex);
53+
54+
if (matchResult) {
55+
const [match] = matchResult;
56+
const original = text;
57+
const remaining = text.slice(match.length);
58+
59+
return {
60+
match,
61+
original,
62+
remaining,
63+
};
64+
}
65+
66+
// No match, so no change.
67+
return {
68+
match: undefined,
69+
original: text,
70+
remaining: text,
71+
};
72+
}

0 commit comments

Comments
 (0)