Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 2, 2018
1 parent e6f8ab0 commit 4e3ece6
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 349 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
nlcst-search.js
nlcst-search.min.js
116 changes: 58 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
'use strict';
'use strict'

var visit = require('unist-util-visit');
var normalize = require('nlcst-normalize');
var isLiteral = require('nlcst-is-literal');
var visit = require('unist-util-visit')
var normalize = require('nlcst-normalize')
var isLiteral = require('nlcst-is-literal')

var own = {}.hasOwnProperty;
var own = {}.hasOwnProperty

module.exports = search;
module.exports = search

var C_SPACE = ' ';
var T_WORD = 'WordNode';
var T_WHITE_SPACE = 'WhiteSpaceNode';
var C_SPACE = ' '
var T_WORD = 'WordNode'
var T_WHITE_SPACE = 'WhiteSpaceNode'

function search(tree, phrases, handler, options) {
var settings = options || {};
var apos = settings.allowApostrophes || options;
var dashes = settings.allowDashes || false;
var literals = settings.allowLiterals;
var config = {allowApostrophes: apos, allowDashes: dashes};
var byWord = {};
var length;
var index;
var key;
var firstWord;
var settings = options || {}
var apos = settings.allowApostrophes || options
var dashes = settings.allowDashes || false
var literals = settings.allowLiterals
var config = {allowApostrophes: apos, allowDashes: dashes}
var byWord = {}
var length
var index
var key
var firstWord

if (!tree || !tree.type) {
throw new Error('Expected node');
throw new Error('Expected node')
}

if (typeof phrases !== 'object') {
throw new Error('Expected object for phrases');
throw new Error('Expected object for phrases')
}

length = phrases.length;
index = -1;
length = phrases.length
index = -1

if ('length' in phrases) {
while (++index < length) {
handlePhrase(phrases[index]);
handlePhrase(phrases[index])
}
} else {
for (key in phrases) {
handlePhrase(key);
handlePhrase(key)
}
}

/* Search the tree. */
visit(tree, T_WORD, visitor);
visit(tree, T_WORD, visitor)

/* Test a phrase. */
function test(phrase, position, parent) {
var siblings = parent.children;
var node = siblings[position];
var count = siblings.length;
var queue = [node];
var expression = phrase.split(C_SPACE).slice(1);
var length = expression.length;
var index = -1;
var siblings = parent.children
var node = siblings[position]
var count = siblings.length
var queue = [node]
var expression = phrase.split(C_SPACE).slice(1)
var length = expression.length
var index = -1

/* Move one position forward. */
position++;
position++

/* Iterate over `expression`. */
while (++index < length) {
/* Allow joining white-space. */
while (position < count) {
node = siblings[position];
node = siblings[position]

if (node.type !== T_WHITE_SPACE) {
break;
break
}

queue.push(node);
position++;
queue.push(node)
position++
}

node = siblings[position];
node = siblings[position]

/* Exit if there are no nodes left, if the
* current node is not a word, or if the
Expand All @@ -86,50 +86,50 @@ function search(tree, phrases, handler, options) {
node.type !== T_WORD ||
normalize(expression[index], config) !== normalize(node, config)
) {
return null;
return null
}

queue.push(node);
position++;
queue.push(node)
position++
}

return queue;
return queue
}

/* Visitor for `WordNode`s. */
function visitor(node, position, parent) {
var word;
var phrases;
var length;
var index;
var result;
var word
var phrases
var length
var index
var result

if (!literals && isLiteral(parent, position)) {
return;
return
}

word = normalize(node, config);
phrases = own.call(byWord, word) ? byWord[word] : [];
length = phrases.length;
index = -1;
word = normalize(node, config)
phrases = own.call(byWord, word) ? byWord[word] : []
length = phrases.length
index = -1

while (++index < length) {
result = test(phrases[index], position, parent);
result = test(phrases[index], position, parent)

if (result) {
handler(result, position, parent, phrases[index]);
handler(result, position, parent, phrases[index])
}
}
}

/* Handle a phrase. */
function handlePhrase(phrase) {
firstWord = normalize(phrase.split(C_SPACE, 1)[0], config);
firstWord = normalize(phrase.split(C_SPACE, 1)[0], config)

if (own.call(byWord, firstWord)) {
byWord[firstWord].push(phrase);
byWord[firstWord].push(phrase)
} else {
byWord[firstWord] = [phrase];
byWord[firstWord] = [phrase]
}
}
}
21 changes: 14 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,42 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.0",
"nyc": "^11.0.2",
"prettier": "^1.12.1",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.0.0",
"xo": "^0.20.0"
},
"scripts": {
"build-md": "remark . --quiet --frail --output",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s nlcstSearch > nlcst-search.js",
"build-mangle": "esmangle nlcst-search.js > nlcst-search.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"guard-for-in": "off",
"no-var": "off",
"prefer-arrow-callback": "off",
"guard-for-in": "off",
"unicorn/prefer-type-error": "off"
},
"ignore": [
Expand Down
23 changes: 10 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ npm install nlcst-search
## Usage

```js
var search = require('nlcst-search');
var toString = require('nlcst-to-string');
var search = require('nlcst-search')
var toString = require('nlcst-to-string')

var tree = {
type: 'SentenceNode',
Expand All @@ -30,9 +30,7 @@ var tree = {
{type: 'WhiteSpaceNode', value: ' '},
{
type: 'WordNode',
children: [
{type: 'TextNode', value: 'do'}
]
children: [{type: 'TextNode', value: 'do'}]
},
{type: 'WhiteSpaceNode', value: ' '},
{
Expand All @@ -44,17 +42,16 @@ var tree = {
]
}
]
};

}

search(tree, ['dont'], function (nodes) {
console.log(toString(nodes));
});
search(tree, ['dont'], function(nodes) {
console.log(toString(nodes))
})
// Don’t

search(tree, ['do blocklevel'], function (nodes) {
console.log(toString(nodes));
});
search(tree, ['do blocklevel'], function(nodes) {
console.log(toString(nodes))
})
// do Block-level
```

Expand Down
Loading

0 comments on commit 4e3ece6

Please sign in to comment.