Skip to content

Commit 5b23c11

Browse files
Rename string library and its export
1 parent 326e7dc commit 5b23c11

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/utils/string.spec.ts renamed to src/utils/trie.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,95 @@
1-
import { groupByCommonHead } from './string';
1+
import { build } from './trie';
22

3-
describe('groupByCommonHead', () => {
4-
it('returns empty object when no words', () => {
5-
expect(groupByCommonHead([])).toEqual({});
3+
describe('build', () => {
4+
it('returns empty trie when no words', () => {
5+
expect(build([])).toEqual({});
66
});
77

88
it('returns the entirety of words when no common head', () => {
99
const words = ['bar', 'foo'];
10-
expect(groupByCommonHead(words)).toEqual({
10+
expect(build(words)).toEqual({
1111
bar: {},
1212
foo: {},
1313
});
1414
});
1515

1616
it('returns the entirety of repeated equal words', () => {
1717
const words = ['foo', 'foo', 'foo', 'foo'];
18-
expect(groupByCommonHead(words)).toEqual({
18+
expect(build(words)).toEqual({
1919
foo: {},
2020
});
2121
});
2222

2323
it('returns the entirety of multiple, repeated equal words', () => {
2424
const words = ['bar', 'bar', 'bar', 'bar', 'foo', 'foo', 'foo', 'foo'];
25-
expect(groupByCommonHead(words)).toEqual({
25+
expect(build(words)).toEqual({
2626
bar: {},
2727
foo: {},
2828
});
2929
});
3030

3131
it('returns an empty string for partial words', () => {
3232
const words = ['foo', 'foobar'];
33-
expect(groupByCommonHead(words)).toEqual({
33+
expect(build(words)).toEqual({
3434
foo: { '': {}, bar: {} },
3535
});
3636
});
3737

3838
it('returns the common head of two words', () => {
3939
const words = ['bar', 'baz'];
40-
expect(groupByCommonHead(words)).toEqual({
40+
expect(build(words)).toEqual({
4141
ba: { r: {}, z: {} },
4242
});
4343
});
4444

4545
it('returns multiple depths of partial words', () => {
4646
const words = ['foo', 'foobar', 'foobaz'];
47-
expect(groupByCommonHead(words)).toEqual({
47+
expect(build(words)).toEqual({
4848
foo: { '': {}, ba: { r: {}, z: {} } },
4949
});
5050
});
5151

5252
it('returns the common head of multiple words', () => {
5353
const words = ['bar', 'baz', 'quux', 'quuz'];
54-
expect(groupByCommonHead(words)).toEqual({
54+
expect(build(words)).toEqual({
5555
ba: { r: {}, z: {} },
5656
quu: { x: {}, z: {} },
5757
});
5858
});
5959

6060
it('preserves leading whitespace', () => {
6161
const words = [' foo', 'foo'];
62-
expect(groupByCommonHead(words)).toEqual({
62+
expect(build(words)).toEqual({
6363
' foo': {},
6464
foo: {},
6565
});
6666
});
6767

6868
it('preserves trailing whitespace', () => {
6969
const words = ['foo ', 'foo'];
70-
expect(groupByCommonHead(words)).toEqual({
70+
expect(build(words)).toEqual({
7171
foo: { '': {}, ' ': {} },
7272
});
7373
});
7474

7575
it('preserves mid-word whitespace', () => {
7676
const words = ['foo bar', 'foobar'];
77-
expect(groupByCommonHead(words)).toEqual({
77+
expect(build(words)).toEqual({
7878
foo: { ' bar': {}, bar: {} },
7979
});
8080
});
8181

8282
it('is case-sensitive with string heads', () => {
8383
const words = ['Foo', 'foo'];
84-
expect(groupByCommonHead(words)).toEqual({
84+
expect(build(words)).toEqual({
8585
Foo: {},
8686
foo: {},
8787
});
8888
});
8989

9090
it('is case-sensitive with string tails', () => {
9191
const words = ['foo', 'foO'];
92-
expect(groupByCommonHead(words)).toEqual({
92+
expect(build(words)).toEqual({
9393
fo: { o: {}, O: {} },
9494
});
9595
});

src/utils/string.ts renamed to src/utils/trie.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function mergeGroups(headChar: Char, tailGroup: ICharTrie): ICharTrie {
3232
* @param words A list of words to parse
3333
* @returns A trie of words grouped by the initial characters they share
3434
*/
35-
function groupUniqueByCommonHead(words: string[]): ICharTrie {
35+
function buildUnique(words: string[]): ICharTrie {
3636
if (words.length === 0) {
3737
return leafNode;
3838
}
@@ -43,23 +43,23 @@ function groupUniqueByCommonHead(words: string[]): ICharTrie {
4343
// End of the target word reached. Include an empty string to signify that
4444
// a word ends at this spot, and group any remaining words in the trie.
4545
const [, nonEmptyWords] = partition(words, word => word === '');
46-
return { '': leafNode, ...groupByCommonHead(nonEmptyWords) };
46+
return { '': leafNode, ...build(nonEmptyWords) };
4747
}
4848

4949
// Begin a new trie containing all words starting with the same letter as wordToMatch
5050
const charToMatch = wordToMatch[0];
5151
const [wordsMatched, wordsMissed] = partition(words, ['[0]', charToMatch]);
5252

5353
const tailsMatched = map(wordsMatched, word => word.substring(1));
54-
const tailsMatchedGrouped = groupByCommonHead(tailsMatched);
54+
const tailsMatchedGrouped = build(tailsMatched);
5555

5656
const groupWithChildren = mergeGroups(charToMatch, tailsMatchedGrouped);
5757

58-
return { ...groupWithChildren, ...groupByCommonHead(wordsMissed) };
58+
return { ...groupWithChildren, ...build(wordsMissed) };
5959
}
6060

61-
/** @borrows groupUniqueByCommonHead as groupByCommonHead */
62-
export function groupByCommonHead(words: string[]): ICharTrie {
61+
/** @borrows buildUnique as build */
62+
export function build(words: string[]): ICharTrie {
6363
const uniqueWords = uniq(words);
64-
return groupUniqueByCommonHead(uniqueWords);
64+
return buildUnique(uniqueWords);
6565
}

0 commit comments

Comments
 (0)