8000 Replace lodash.partition() with local function · wimpyprogrammer/strings-to-regex@99948ea · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit 99948ea

Browse files
Replace lodash.partition() with local function
Remove lodash dependency. Shrink demo bundle from 73 KB to 4 KB.
1 parent 12fd5be commit 99948ea

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,13 @@
4242
"ts"
4343
]
4444
},
45-
"dependencies": {
46-
"lodash": "^4.17.10"
47-
},
4845
"devDependencies": {
4946
"@babel/cli": "^7.0.0-beta.54",
5047
"@babel/core": "^7.0.0-beta.54",
5148
"@babel/preset-env": "^7.0.0-beta.54",
5249
"@babel/preset-typescript": "^7.0.0-beta.54",
5350
"@types/es6-shim": "^0.31.37",
5451
"@types/jest": "^23.3.1",
55-
"@types/lodash": "^4.14.114",
5652
"husky": "^0.14.3",
5753
"jest": "^23.4.1",
5854
"prettier": "^1.13.7",

src/utils/trie.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import { partition } from 'lodash';
21
import { Char, ICharTrie } from '../types/charTrie';
32

43
const leafNode = new Map() as ICharTrie;
54

5+
function groupWordsByHeadChar(
6+
words: string[],
7+
firstChar: string
8+
): [string[], string[]] {
9+
const matched: string[] = [];
10+
const missed: string[] = [];
11+
12+
words.forEach(word => {
13+
if (word[0] === firstChar) {
14+
matched.push(word);
15+
} else {
16+
missed.push(word);
17+
}
18+
});
19+
20+
return [matched, missed];
21+
}
22+
623
/**
724
* Arrange a head character and its suffixes into a trie.
825
* Flatten leaves of the character trie containing a single letter.
@@ -38,13 +55,13 @@ function buildUnique(words: string[]): ICharTrie {
3855
if (wordToMatch === '') {
3956
// End of the target word reached. Include an empty string to signify that
4057
// a word ends at this spot, and group any remaining words in the trie.
41-
const [, nonEmptyWords] = partition(words, word => word === '');
58+
const nonEmptyWords = words.filter(word => word !== '');
4259
return new Map([['', leafNode], ...build(nonEmptyWords)]) as ICharTrie;
4360
}
4461

4562
// Begin a new trie containing all words starting with the same letter as wordToMatch
4663
const charToMatch = wordToMatch[0];
47-
const [wordsMatched, wordsMissed] = partition(words, ['[0]', charToMatch]);
64+
const [wordsMatched, wordsMissed] = groupWordsByHeadChar(words, charToMatch);
4865

4966
const tailsMatched = wordsMatched.map(word => word.substring(1));
5067
const tailsMatchedGrouped = build(tailsMatched);

yarn.lock

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,6 @@
551551
version "23.3.1"
552552
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-23.3.1.tgz#a4319aedb071d478e6f407d1c4578ec8156829cf"
553553

554-
"@types/lodash@^4.14.114":
555-
version "4.14.114"
556-
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.114.tgz#e6f251af5994dd0d7ce141f9241439b4f40270f6"
557-
558554
"@webassemblyjs/ast@1.5.13":
559555
version "1.5.13"
560556
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.5.13.tgz#81155a570bd5803a30ec31436bc2c9c0ede38f25"

0 commit comments

Comments
 (0)
0