-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
executable file
·620 lines (485 loc) · 14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
'use strict'
var fs = require('fs')
var path = require('path')
var test = require('tape')
var nspell = require('..')
var EN_GB = 'en-gb'
var EN_US = 'en'
var DA = 'da'
var NL = 'nl'
var DE = 'de'
var ES = 'es'
var CS = 'cs'
var codes = [EN_GB, EN_US, DA, NL, DE, ES, CS]
var dictionaries = {}
codes.forEach(function (code) {
dictionaries[code] = {
aff: fs.readFileSync(
path.join('node_modules', 'dictionary-' + code, 'index.aff')
),
dic: fs.readFileSync(
path.join('node_modules', 'dictionary-' + code, 'index.dic')
)
}
})
test('NSpell()', function (t) {
var es = nspell(dictionaries[ES])
var us
var gb
var nl
var de
var da
t.equal(typeof nspell, 'function', 'should expose a function')
t.throws(
function () {
nspell()
},
/Missing `aff` in dictionary/,
'should warn when missing `aff` (1)'
)
t.throws(
function () {
nspell({})
},
/Missing `aff` in dictionary/,
'should warn when missing `aff` (2)'
)
t.throws(
function () {
nspell([])
},
/Missing `aff` in dictionary/,
'should warn when missing `aff` (3)'
)
de = nspell(dictionaries[DE].aff)
t.equal(
de instanceof nspell,
true,
'should construct a new instance from one buffer'
)
gb = nspell(dictionaries[EN_GB].aff, dictionaries[EN_GB].dic)
t.equal(
gb instanceof nspell,
true,
'should construct a new instance from two buffers'
)
us = nspell(dictionaries[EN_US])
t.equal(
us instanceof nspell,
true,
'should construct a new instance from a dictionary object'
)
nl = nspell([dictionaries[NL]])
t.equal(
nl instanceof nspell,
true,
'should construct a new instance from a list of dictionary objects'
)
de.dictionary(dictionaries[DE].dic)
da = nspell([{aff: dictionaries[DA].aff}, {dic: dictionaries[DA].dic}])
t.equal(
da instanceof nspell,
true,
'should construct a new instance from a list of partial dictionary objects'
)
t.equal(typeof nl.data, 'object', 'should expose data')
t.equal(typeof de.rules, 'object', 'should expose rules')
t.equal(typeof da.flags, 'object', 'should expose flags')
t.equal(Array.isArray(us.compoundRules), true, 'should expose compound rules')
t.equal(
typeof us.compoundRuleCodes,
'object',
'should expose compound rule codes'
)
t.equal(
typeof us.replacementTable,
'object',
'should expose a replacement table'
)
t.test('nspell#wordCharacters()', function (st) {
st.equal(
us.wordCharacters(),
'0123456789',
'should return the defined word-characters'
)
st.equal(es.wordCharacters(), null, 'should return `null` when not defined')
st.end()
})
t.test('nspell#correct(value)', function (st) {
st.equal(
us.correct('colour'),
false,
'should return `false` when a word is not correctly spelled'
)
st.equal(
us.correct('color'),
true,
'should return `true` when a word is correctly spelled (1)'
)
st.equal(
us.correct('c'),
true,
'should return `true` when a word is correctly spelled (2)'
)
st.equal(us.correct(' '), false, 'should return `false` without word')
st.equal(us.correct('.'), false, 'should return `false` for non-words')
st.equal(
us.correct('ABDUL'),
true,
'should check for sentence case when uppercase (ok)'
)
st.equal(
us.correct('COLOUR'),
false,
'should check for sentence case when uppercase (not ok)'
)
st.equal(us.correct('Color'), true, 'should check for lowercase (ok)')
st.equal(us.correct('Colour'), false, 'should check for lowercase (not ok)')
st.equal(us.correct('Colour'), false, 'should check for lowercase (not ok)')
st.equal(
nl.correct('DVD'),
false,
'should not check uppercase for sentence case when KEEPCASE'
)
st.equal(
nl.correct('dVd'),
false,
'should not check other casing for lowercase when KEEPCASE'
)
st.equal(
nl.correct('eierlevendbarend'),
true,
'should support ONLYINCOMPOUND (ok)'
)
st.equal(
nl.correct('eier'),
false,
'should support ONLYINCOMPOUND (not ok)'
)
st.equal(us.correct('21st'), true, 'should support compounds (1)')
st.equal(us.correct('20st'), false, 'should support compounds (2)')
st.equal(us.correct('20th'), true, 'should support compounds (3)')
st.equal(us.correct('23st'), false, 'should support compounds (4)')
st.equal(us.correct('23th'), false, 'should support compounds (5)')
st.equal(us.correct('23rd'), true, 'should support compounds (6)')
st.equal(us.correct('12th'), true, 'should support compounds (7)')
st.equal(us.correct('22nd'), true, 'should support compounds (8)')
st.equal(
de.correct('.'),
false,
'should not check words shorter than `COMPOUNDMIN` (coverage)'
)
st.equal(
us.correct('thinking'),
true,
'should support rules (used to be a bug for the US dictionary)'
)
st.end()
})
t.test('nspell#suggest(value)', function (st) {
st.deepEqual(
us.suggest('color'),
[],
'should return an empty array when correct (1)'
)
st.deepEqual(
us.suggest('c'),
[],
'should return an empty array when correct (2)'
)
st.deepEqual(us.suggest('colour'), ['color'], 'should suggest alternatives')
st.deepEqual(
us.suggest('propper'),
['proper', 'propped', 'cropper', 'dropper', 'popper', 'prosper'],
'should suggest alternatives'
)
st.deepEqual(
us.suggest('Ghandi'),
[
'Brandi',
'Ghana',
'Grandee',
'Hand',
'Hands',
'Handy',
'Hanoi',
'Hindi',
'Randi',
'Shanxi'
],
'should suggest alternatives'
)
st.deepEqual(
us.suggest(' '),
[],
'should return an empty array for empty values'
)
st.ok(us.suggest('.').length, 'should try for non-words')
st.deepEqual(
us.suggest('Colour'),
['Color'],
'should suggest alternatives for sentence case'
)
st.deepEqual(
us.suggest('COLOUR'),
['COLOR'],
'should suggest alternatives for uppercase'
)
st.deepEqual(
us.suggest('coLOUR'),
['coLOR'],
'should suggest alternatives for funky-case'
)
st.deepEqual(
us.suggest('html'),
['HTML'],
'should suggest uppercase versions'
)
st.deepEqual(
us.suggest('collor'),
['color', 'colloq', 'collar'],
'should suggest removals'
)
st.not(us.suggest('coor').indexOf('color'), -1, 'should suggest additions')
st.deepEqual(
us.suggest('cloor'),
['floor', 'color'],
'should suggest switches'
)
st.deepEqual(us.suggest('cokor'), ['color'], 'should suggest insertions')
st.deepEqual(
us.suggest('bulshit'),
['bolshie'],
'should not suggest alternatives marked with `NOSUGGEST`'
)
st.deepEqual(
us.suggest('consize'),
['concise'],
'should suggest based on replacements'
)
st.deepEqual(
us.suggest('npmnpmnpmnpmnpmnpmnpmnpmnpmnpmnpmnpmnpmnpmnpm'),
[],
'should not overflow on too long values'
)
st.end()
})
t.test('nspell#add(value)', function (st) {
st.equal(
us.correct('npm'),
false,
'should initially be marked as incorrect'
)
st.deepEqual(
us.suggest('npm'),
['bpm', 'pm', 'ppm', 'rpm', 'wpm', 'Nam', 'NM', 'Np', 'NPR'],
'should initially receive suggestions'
)
st.equal(us.add('npm'), us, 'should return the context object')
st.equal(us.correct('npm'), true, 'should now mark as correct')
st.deepEqual(
us.suggest('npm'),
[],
'should now no longer receive suggestions'
)
st.end()
})
t.test('nspell#add(value, model)', function (st) {
// `azc` is a Dutch word only properly spelled in its lowercase form.
st.equal(nl.add('npm', 'azc'), nl, 'should return the context object')
st.equal(nl.correct('npm'), true, 'should match affixes (1)')
st.equal(nl.correct('NPM'), false, 'should match affixes (2)')
st.equal(nl.correct('Npm'), false, 'should match affixes (3)')
st.equal(us.correct('point'), true, 'should support models (1, GH-8)')
st.equal(us.correct('points'), true, 'should support models (2, GH-8)')
us.add('datapoint', 'point')
st.equal(us.correct('datapoint'), true, 'should support models (3, GH-8)')
st.equal(us.correct('datapoints'), true, 'should support models (4, GH-8)')
nl.remove('npm')
us.remove('datapoint')
st.end()
})
t.test('nspell#remove(value)', function (st) {
st.equal(us.correct('npm'), true, 'should initially be marked as correct')
st.deepEqual(
us.suggest('npm'),
[],
'should initially receive no suggestions'
)
st.equal(us.remove('npm'), us, 'should return the context object')
st.equal(us.correct('npm'), false, 'should now mark as incorrect')
st.deepEqual(
us.suggest('npm'),
['bpm', 'pm', 'ppm', 'rpm', 'wpm', 'Nam', 'NM', 'Np', 'NPR'],
'should now receive suggestions'
)
st.end()
})
t.test('nspell#personal(buf|string)', function (st) {
var personal = 'Supercalifragilisticexpialidocious'
var word = personal.toLowerCase()
// <3 Mary Poppins.
st.equal(us.correct(personal), false, 'set-up')
st.equal(us.personal(personal), us, 'should return the context object')
st.equal(
us.correct(personal),
true,
'should mark personal terms as correct'
)
st.deepEqual(us.suggest(word), [personal], 'should suggest personal terms')
st.equal(nl.correct('eierlevendbarend'), true, 'should forbid words (1)')
nl.personal('*eierlevendbarend\nnpm/azc\n')
st.equal(nl.correct('eierlevendbarend'), false, 'should forbid words (2)')
st.deepEqual(nl.suggest('eierlevendbaren'), [], 'should forbid words (3)')
st.equal(nl.correct('npm'), true, 'should match affixes (1)')
st.equal(nl.correct('NPM'), false, 'should match affixes (2)')
// Clean.
us.remove(personal)
nl.remove('eierlevendbarend')
nl.remove('npm')
st.end()
})
t.test('nspell#spell(value)', function (st) {
var spell
var aff
var dic
dic = ['3', 'foo', 'bar/a', 'baz/b', ''].join('\n')
aff = ['SET UTF-8', '', 'FORBIDDENWORD a', 'WARN b', ''].join('\n')
spell = nspell(aff, dic)
st.deepEqual(
spell.spell('foo'),
{correct: true, forbidden: false, warn: false},
'should include `correct: true` for correct results'
)
st.deepEqual(
spell.spell('qux'),
{correct: false, forbidden: false, warn: false},
'should include `correct: false` for incorrect results'
)
st.deepEqual(
spell.spell('bar'),
{correct: false, forbidden: true, warn: false},
'should include `forbidden: true` for forbidden results'
)
st.deepEqual(
spell.spell('baz'),
{correct: true, forbidden: false, warn: true},
'should include `warn: true` for warnings'
)
st.deepEqual(
us.suggest('dont'),
[
'cont',
'dent',
'dint',
'done',
'dong',
'font',
'dolt',
'don',
"don't",
'dona',
'dons',
'dost',
'dot',
'wont',
'Donn',
'Mont',
'Ont'
],
'should suggest alternatives including correct conjunction'
)
st.deepEqual(
us.suggest('Iffect'),
['Affect', 'Effect', 'Infect'],
'should suggest sentence case with replaced first character'
)
st.deepEqual(
us.suggest('Acnada'),
['Canada'],
'should suggest sentence case with swapped first character'
)
st.deepEqual(
us.suggest('abDUL'),
['Abdul'],
'should suggest sentence case for funky case when sentence case in dictionary'
)
st.deepEqual(
us.suggest('COLORFU'),
['COLORFUL'],
'should suggest alternatives for uppercase with added letter'
)
st.deepEqual(
us.suggest('TODO'),
['DODO', 'TOD', 'TODD', 'TOGO', 'TOJO', 'TOO', 'TOTO'],
'should suggest alternatives for uppercase input'
)
st.end()
})
t.end()
})
test('broken dictionaries', function (t) {
t.plan(1)
t.test('dictionary-cs', function (st) {
var spell
st.plan(2)
st.doesNotThrow(function () {
spell = nspell(dictionaries[CS])
}, 'should not throw for invalid affix regexes - GH-5')
st.deepEqual(
spell.suggest('znelěcznelěcznelěcznelěcznelěcznelěc'),
[],
'should work'
)
})
})
test('parse dictionaries', function (t) {
t.plan(2)
t.test('iconv and oconv', function (st) {
var dic = ['2', 'ijdel', 'ijdeltuit'].join('\n')
var aff = [
'SET UTF-8',
'',
'ICONV 1',
'ICONV ij ij',
'',
'OCONV 1',
'OCONV ij ij'
].join('\n')
var spell = nspell(aff + '\n', dic + '\n')
st.equal(spell.correct('ijdel'), true, 'should support iconv (#1)')
st.equal(spell.correct('ijdel'), true, 'should support iconv (#2)')
st.deepEqual(
spell.suggest('ijde'),
['ijdel', 'IJdel'],
'should support oconv (#1)'
)
st.deepEqual(
spell.suggest('ijde'),
['ijdel', 'IJdel'],
'should support oconv (#2)'
)
st.end()
})
t.test('slashes and comments', function (st) {
var dic = [
'5',
'aaa\\//A',
'bbb/#*',
'ccc/#* #test',
'ddd#/A',
'eee#test'
].join('\n')
var spell = nspell('SET UTF-8\n', dic)
st.equal(spell.correct('aaa/'), true, 'should see slash (/) as word')
st.deepEqual(spell.data.bbb, ['#', '*'], 'should see hash (#) as flag')
st.deepEqual(
spell.data.ccc,
['#', '*'],
'should see first hash (#) as flag and second hash as comment'
)
st.equal(spell.correct('ddd'), true, 'should see hash (#) as comment')
st.equal(spell.correct('eee'), true, 'should see hash (#) as comment')
st.end()
})
})