Skip to content

Commit

Permalink
Merge pull request #109 from hcodes/checking_substrings
Browse files Browse the repository at this point in the history
Checking substrings
  • Loading branch information
hcodes committed Aug 9, 2018
2 parents ec663b8 + ba11c3d commit b6efdd7
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## v5.0.0
- FIX: **Breaking changes**: Incorrect work of dictionary words in substrings #106.
- FIX: Comments in JSON #108.

## v4.2.1
FIX: TypeError: Cannot destructure property config of 'undefined' or 'null' #103.

Expand Down
15 changes: 15 additions & 0 deletions lib/config.js
Expand Up @@ -6,6 +6,17 @@ const debug = require('./debug');
const printDebug = debug.print;
const exitCodes = require('./exit-codes');
const knownProps = require('./config-properties');
const stripJsonComments = require('strip-json-comments');
const parseJson = require('parse-json');

function loadJson(filepath, content) {
try {
return parseJson(stripJsonComments(content));
} catch (err) {
err.message = `JSON Error in ${filepath}:\n${err.message}`;
throw err;
}
}

module.exports = {
/**
Expand All @@ -16,6 +27,10 @@ module.exports = {
*/
get(file) {
const explorer = cosmiconfig('yaspeller', {
loaders: {
'.json': loadJson,
noExt: loadJson
},
searchPlaces: [
'package.json',
'.yaspellerrc',
Expand Down
10 changes: 9 additions & 1 deletion lib/dictionary.js
Expand Up @@ -168,7 +168,15 @@ module.exports = {
// unknownWord(s)? = unknownWord(s)? and UnknownWord(s)?
// UnknownWord(s)? = UnknownWord(s)?

const preparedWord = word.replace(rePrepare, ($, $1, $2) => '[' + $1 + $1.toUpperCase() + ']' + $2);
let preparedWord = word.replace(rePrepare, ($, $1, $2) => '[' + $1 + $1.toUpperCase() + ']' + $2);

if (preparedWord.search(/\^/) !== 0) {
preparedWord = '^' + preparedWord;
}

if (preparedWord.search(/\$/) !== preparedWord.length - 1) {
preparedWord += '$';
}

try {
result.push(new RegExp(preparedWord));
Expand Down
38 changes: 19 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"yaspeller": "./bin/yaspeller"
},
"description": "Search tool typos in the text, files and websites",
"version": "4.2.1",
"version": "5.0.0",
"license": "MIT",
"homepage": "https://github.com/hcodes/yaspeller",
"repository": {
Expand All @@ -34,7 +34,7 @@
"dependencies": {
"async": "^2.6.1",
"chalk": "^2.4.1",
"commander": "^2.15.1",
"commander": "^2.17.1",
"cosmiconfig": "^5.0.5",
"entities": "^1.1.1",
"escape-html": "^1.0.3",
Expand All @@ -50,10 +50,11 @@
},
"devDependencies": {
"chai": "^4.1.0",
"eslint": "^5.0.0",
"eslint": "^5.3.0",
"istanbul": "^0.4.5",
"mocha": "^5.2.0",
"sinon": "^6.0.1"
"parse-json": "^4.0.0",
"sinon": "^6.1.4"
},
"engines": {
"node": ">=4"
Expand Down
4 changes: 4 additions & 0 deletions test/test.config.js
Expand Up @@ -19,6 +19,10 @@ describe('Config', function() {
it('get, custom config', function() {
assert.deepEqual(config.get('./test/json/no_comment.json'), ['1']);
});

it('get, custom config with comments', function() {
assert.deepEqual(config.get('./test/json/comment.json'), ['1']);
});

it('get, default config', function() {
assert.ok(Object.keys(config.get(null)).length);
Expand Down
70 changes: 53 additions & 17 deletions test/test.dictionary.js
Expand Up @@ -53,17 +53,53 @@ describe('Dictionary', function() {
});

it('isTypo()', function() {
const dict = dictionary.prepareDictionary([
'контрол'
]);

assert.isFalse(dictionary.isTypo('Контрол', dict));

const dict2 = dictionary.prepareDictionary([
'Контрол'
]);

assert.isTrue(dictionary.isTypo('контрол', dict2));
[
{
dict: [
'контрол'
],
word: 'Контрол',
result: false
},
{
dict: [
'Контрол'
],
word: 'контрол',
result: true
},
{
dict: [
'митап'
],
word: 'митап',
result: false
},
{
dict: [
'митап'
],
word: 'тестмитап',
result: true
},
{
dict: [
'митап'
],
word: 'немитап',
result: true
},
{
dict: [
'митап'
],
word: 'багмитапбаг',
result: true
}
].forEach(item => {
const dict = dictionary.prepareDictionary(item.dict);
assert.equal(item.result, dictionary.isTypo(item.word, dict), item.word);
});
});

it('removeDuplicates()', function() {
Expand Down Expand Up @@ -101,17 +137,17 @@ describe('Dictionary', function() {

it('set(), dictionary from config', function() {
dictionary.set([], ['a']);
assert.deepEqual(dictionary._dict, [/[aA]/]);
assert.deepEqual(dictionary._dict, [/^[aA]$/]);
});

it('set()', function() {
dictionary.set(['test/dict/a.json', 'test/dict/b.json'], ['a']);
assert.deepEqual(dictionary._dict, [
/[aA]/,
/[xX]yz/,
/[aA]bc/,
/CLI/,
/[dD]eps/
/^[aA]$/,
/^[xX]yz$/,
/^[aA]bc$/,
/^CLI$/,
/^[dD]eps$/
]);
});

Expand Down

0 comments on commit b6efdd7

Please sign in to comment.