Skip to content

Commit 42c5a0c

Browse files
committed
Refactor to improve bundle size
1 parent 7ad3fbf commit 42c5a0c

18 files changed

+234
-379
lines changed

lib/add.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ var NO_CODES = []
99
// Add `value` to the checker.
1010
function add(value, model) {
1111
var self = this
12-
var dict = self.data
13-
var codes = dict[model] || NO_CODES
1412

15-
push(dict, value, codes, self)
13+
push(self.data, value, self.data[model] || NO_CODES, self)
1614

1715
return self
1816
}

lib/dictionary.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,28 @@ module.exports = add
77
// Add a dictionary file.
88
function add(buf) {
99
var self = this
10-
var compound = self.compoundRules
11-
var compoundCodes = self.compoundRuleCodes
1210
var index = -1
13-
var length = compound.length
1411
var rule
1512
var source
1613
var character
1714
var offset
18-
var count
1915

2016
parse(buf, self, self.data)
2117

2218
// Regenerate compound expressions.
23-
while (++index < length) {
24-
rule = compound[index]
19+
while (++index < self.compoundRules.length) {
20+
rule = self.compoundRules[index]
2521
source = ''
26-
2722
offset = -1
28-
count = rule.length
2923

30-
while (++offset < count) {
24+
while (++offset < rule.length) {
3125
character = rule.charAt(offset)
32-
33-
source +=
34-
compoundCodes[character].length === 0
35-
? character
36-
: '(?:' + compoundCodes[character].join('|') + ')'
26+
source += self.compoundRuleCodes[character].length
27+
? '(?:' + self.compoundRuleCodes[character].join('|') + ')'
28+
: character
3729
}
3830

39-
compound[index] = new RegExp(source, 'i')
31+
self.compoundRules[index] = new RegExp(source, 'i')
4032
}
4133

4234
return self

lib/index.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ proto.personal = require('./personal.js')
1818

1919
// Construct a new spelling context.
2020
function NSpell(aff, dic) {
21-
var length
22-
var index
21+
var index = -1
2322
var dictionaries
2423

2524
if (!(this instanceof NSpell)) {
@@ -57,14 +56,11 @@ function NSpell(aff, dic) {
5756
this.rules = aff.rules
5857
this.flags = aff.flags
5958

60-
length = dictionaries ? dictionaries.length : 0
61-
index = -1
62-
63-
while (++index < length) {
64-
dic = dictionaries[index]
65-
66-
if (dic && dic.dic) {
67-
this.dictionary(dic.dic)
59+
if (dictionaries) {
60+
while (++index < dictionaries.length) {
61+
if (dictionaries[index].dic) {
62+
this.dictionary(dictionaries[index].dic)
63+
}
6864
}
6965
}
7066
}

lib/personal.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@ module.exports = add
55
// Add a dictionary.
66
function add(buf) {
77
var self = this
8-
var flags = self.flags
98
var lines = buf.toString('utf8').split('\n')
10-
var length = lines.length
119
var index = -1
1210
var line
1311
var forbidden
1412
var word
15-
var model
1613
var flag
1714

1815
// Ensure there’s a key for `FORBIDDENWORD`: `false` cannot be set through an
1916
// affix file so its safe to use as a magic constant.
20-
flag = flags.FORBIDDENWORD || false
21-
flags.FORBIDDENWORD = flag
17+
if (self.flags.FORBIDDENWORD === undefined) self.flags.FORBIDDENWORD = false
18+
flag = self.flags.FORBIDDENWORD
2219

23-
while (++index < length) {
20+
while (++index < lines.length) {
2421
line = lines[index].trim()
2522

2623
if (!line) {
@@ -29,14 +26,13 @@ function add(buf) {
2926

3027
line = line.split('/')
3128
word = line[0]
32-
model = line[1]
3329
forbidden = word.charAt(0) === '*'
3430

3531
if (forbidden) {
3632
word = word.slice(1)
3733
}
3834

39-
self.add(word, model)
35+
self.add(word, line[1])
4036

4137
if (forbidden) {
4238
self.data[word].push(flag)

lib/spell.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ module.exports = spell
88
// Check spelling of `word`.
99
function spell(word) {
1010
var self = this
11-
var dict = self.data
12-
var flags = self.flags
1311
var value = form(self, word, true)
1412

1513
// Hunspell also provides `root` (root word of the input word), and `compound`
1614
// (whether `word` was compound).
1715
return {
1816
correct: self.correct(word),
19-
forbidden: Boolean(value && flag(flags, 'FORBIDDENWORD', dict[value])),
20-
warn: Boolean(value && flag(flags, 'WARN', dict[value]))
17+
forbidden: Boolean(
18+
value && flag(self.flags, 'FORBIDDENWORD', self.data[value])
19+
),
20+
warn: Boolean(value && flag(self.flags, 'WARN', self.data[value]))
2121
}
2222
}

0 commit comments

Comments
 (0)