Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 28, 2018
1 parent ba71f00 commit 84e489f
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 128 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
n-gram.js
n-gram.min.js
33 changes: 16 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
'use strict';
'use strict'

module.exports = nGram;
module.exports = nGram

nGram.bigram = nGram(2);
nGram.trigram = nGram(3);
nGram.bigram = nGram(2)
nGram.trigram = nGram(3)

/* Factory returning a function that converts a given string
* to n-grams. */
// Factory returning a function that converts a value string to n-grams.
function nGram(n) {
if (typeof n !== 'number' || isNaN(n) || n < 1 || n === Infinity) {
throw new Error('`' + n + '` is not a valid argument for n-gram');
throw new Error('`' + n + '` is not a valid argument for n-gram')
}

return grams;
return grams

/* Create n-grams from a given value. */
// Create n-grams from a given value.
function grams(value) {
var nGrams = [];
var index;
var nGrams = []
var index

if (value === null || value === undefined) {
return nGrams;
return nGrams
}

value = value.slice ? value : String(value);
index = value.length - n + 1;
value = value.slice ? value : String(value)
index = value.length - n + 1

if (index < 1) {
return nGrams;
return nGrams
}

while (index--) {
nGrams[index] = value.slice(index, index + n);
nGrams[index] = value.slice(index, index + n)
}

return nGrams;
return nGrams
}
}
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,38 @@
"devDependencies": {
"browserify": "^16.0.0",
"nyc": "^12.0.0",
"prettier": "^1.14.2",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.0.0",
"tinyify": "^2.4.3",
"xo": "^0.22.0"
},
"scripts": {
"build-md": "remark . -qfo",
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"build-bundle": "browserify . -s nGram > n-gram.js",
"build-mangle": "browserify . -s nGram -p tinyify > n-gram.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"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,
"ignores": [
"n-gram.js"
Expand Down
14 changes: 7 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ npm install n-gram
## Usage

```js
var nGram = require('n-gram');
var nGram = require('n-gram')

nGram.bigram('n-gram'); // ['n-', '-g', 'gr', 'ra', 'am']
nGram(2)('n-gram'); // ['n-', '-g', 'gr', 'ra', 'am']
nGram.bigram('n-gram') // ['n-', '-g', 'gr', 'ra', 'am']
nGram(2)('n-gram') // ['n-', '-g', 'gr', 'ra', 'am']

nGram.trigram('n-gram'); // ['n-g', '-gr', 'gra', 'ram']
nGram.trigram('n-gram') // ['n-g', '-gr', 'gra', 'ram']

nGram(6)('n-gram'); // ['n-gram']
nGram(7)('n-gram'); // []
nGram(6)('n-gram') // ['n-gram']
nGram(7)('n-gram') // []

// Anything with a `.length` and `.slice` works: arrays too.
nGram.bigram(['alpha', 'bravo', 'charlie']); // [['alpha', 'bravo'], ['bravo', 'charlie']]
nGram.bigram(['alpha', 'bravo', 'charlie']) // [['alpha', 'bravo'], ['bravo', 'charlie']]
```

## API
Expand Down
Loading

0 comments on commit 84e489f

Please sign in to comment.