Comparing version 1.1.2 to 2.0.0
41
index.js
@@ -1,12 +0,17 @@ | ||
'use strict' | ||
export var bigram = nGram(2) | ||
export var trigram = nGram(3) | ||
module.exports = nGram | ||
nGram.bigram = nGram(2) | ||
nGram.trigram = nGram(3) | ||
// 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') | ||
/** | ||
* Factory returning a function that converts a value string to n-grams. | ||
* | ||
* @param {number} n | ||
*/ | ||
export function nGram(n) { | ||
if ( | ||
typeof n !== 'number' || | ||
Number.isNaN(n) || | ||
n < 1 || | ||
n === Number.POSITIVE_INFINITY | ||
) { | ||
throw new Error('`' + n + '` is not a valid argument for `n-gram`') | ||
} | ||
@@ -16,6 +21,14 @@ | ||
// Create n-grams from a given value. | ||
/** | ||
* Create n-grams from a given value. | ||
* | ||
* @param {string|string[]} [value] | ||
*/ | ||
function grams(value) { | ||
/** @type {(typeof value)[]} */ | ||
var nGrams = [] | ||
/** @type {number} */ | ||
var index | ||
/** @type {typeof value} */ | ||
var source | ||
@@ -26,4 +39,4 @@ if (value === null || value === undefined) { | ||
value = value.slice ? value : String(value) | ||
index = value.length - n + 1 | ||
source = value.slice ? value : String(value) | ||
index = source.length - n + 1 | ||
@@ -35,3 +48,3 @@ if (index < 1) { | ||
while (index--) { | ||
nGrams[index] = value.slice(index, index + n) | ||
nGrams[index] = source.slice(index, index + n) | ||
} | ||
@@ -38,0 +51,0 @@ |
{ | ||
"name": "n-gram", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Get n-grams from text", | ||
@@ -27,31 +27,30 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"browserify": "^16.0.0", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.0.0", | ||
"remark-cli": "^7.0.0", | ||
"remark-preset-wooorm": "^6.0.0", | ||
"tape": "^4.0.0", | ||
"tinyify": "^2.0.0", | ||
"xo": "^0.25.0" | ||
"@types/tape": "^4.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
}, | ||
"scripts": { | ||
"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-bundle && npm run build-mangle", | ||
"test-api": "node test", | ||
"test-coverage": "nyc --reporter lcov tape test.js", | ||
"test": "npm run format && npm run build && npm run test-coverage" | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"prettier": { | ||
@@ -67,6 +66,7 @@ "tabWidth": 2, | ||
"prettier": true, | ||
"esnext": false, | ||
"ignores": [ | ||
"n-gram.js" | ||
] | ||
"rules": { | ||
"import/no-mutable-exports": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
}, | ||
@@ -77,3 +77,8 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
} | ||
} |
@@ -12,2 +12,5 @@ # n-gram | ||
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
[npm][]: | ||
@@ -22,8 +25,8 @@ | ||
```js | ||
var nGram = require('n-gram') | ||
import {bigram, trigram, nGram} = require('n-gram') | ||
nGram.bigram('n-gram') // ['n-', '-g', 'gr', 'ra', 'am'] | ||
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'] | ||
trigram('n-gram') // ['n-g', '-gr', 'gra', 'ram'] | ||
@@ -34,3 +37,3 @@ nGram(6)('n-gram') // ['n-gram'] | ||
// Anything with a `.length` and `.slice` works: arrays too. | ||
nGram.bigram(['alpha', 'bravo', 'charlie']) // [['alpha', 'bravo'], ['bravo', 'charlie']] | ||
bigram(['alpha', 'bravo', 'charlie']) // [['alpha', 'bravo'], ['bravo', 'charlie']] | ||
``` | ||
@@ -40,5 +43,8 @@ | ||
This package exports the following identifiers: `ngram`, `bigram`, and `trigram`. | ||
There is no default export. | ||
### `nGram(n)` | ||
Factory returning a function that converts a given value to n-grams. | ||
Create a function that converts a given value to n-grams. | ||
@@ -48,7 +54,7 @@ Want padding? | ||
### `nGram.bigram(value)` | ||
### `bigram(value)` | ||
Shortcut for `nGram(2)`. | ||
### `nGram.trigram(value)` | ||
### `trigram(value)` | ||
@@ -63,5 +69,5 @@ Shortcut for `nGram(3)`. | ||
[build-badge]: https://img.shields.io/travis/words/n-gram.svg | ||
[build-badge]: https://github.com/words/n-gram/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/words/n-gram | ||
[build]: https://github.com/words/n-gram/actions | ||
@@ -68,0 +74,0 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/words/n-gram.svg |
6386
5
63
87
Yes
10