Skip to content

Commit 525f854

Browse files
committed
feat(dictionary): add addDictionary method to support dictionary creation
1 parent d021e8a commit 525f854

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/index.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -355,18 +355,6 @@ var LeoProfanity = {
355355
return this.wordDictionary[name]
356356
},
357357

358-
/**
359-
* Add dictionary
360-
*
361-
* @todo complete it
362-
* @private
363-
* @param {string} name
364-
* @param {Array.string} data
365-
*/
366-
addDictionary: function (name, data) {
367-
368-
},
369-
370358
/**
371359
* Load word list from dictionary to using in the filter
372360
*
@@ -384,6 +372,24 @@ var LeoProfanity = {
384372
// clone
385373
this.words = JSON.parse(JSON.stringify(this.getDictionary(name)))
386374
},
375+
376+
/**
377+
* Add or create dictionary
378+
*
379+
* @example
380+
* // create new dictionary
381+
* filter.addDictionary('th', ['หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า'])
382+
*
383+
* @public
384+
* @param {string} name dictionary name
385+
* @param {Array.string} words dictionary words
386+
*/
387+
addDictionary: function (name, words) {
388+
this.wordDictionary[name] = words
389+
this.loadDictionary(name)
390+
391+
return this;
392+
},
387393
};
388394

389395
if (typeof module !== 'undefined' && module.exports != null) {

test/index.spec.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,6 @@ describe('getDictionary', function () {
280280
}
281281
});
282282

283-
describe('addDictionary', function () {
284-
it('should return valid result', function () {
285-
// TODO: complete it
286-
});
287-
});
288-
289283
describe('loadDictionary', function () {
290284
it('should load "en" dictionary', function () {
291285
filter.loadDictionary()
@@ -317,3 +311,15 @@ describe('loadDictionary', function () {
317311
});
318312
}
319313
});
314+
315+
describe('addDictionary', function () {
316+
it('should add new dictionary', function () {
317+
filter.loadDictionary();
318+
var name = 'th';
319+
var words = ['หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า'];
320+
321+
filter.addDictionary(name, words);
322+
323+
expect(filter.list().length).to.equal(words.length);
324+
});
325+
});

0 commit comments

Comments
 (0)