Comparing version 2.0.1 to 2.0.2
@@ -8,18 +8,24 @@ /** | ||
n: number | ||
): <T extends string | string[]>(value?: T) => T[] | ||
): <T extends string | unknown[]>( | ||
value?: T | undefined | ||
) => T extends any[] ? T : string[] | ||
/** | ||
* Create n-grams from a given value. | ||
* | ||
* @template {string|string[]} T | ||
* @template {string|Array<unknown>} T | ||
* @param {T} [value] | ||
* @returns {T[]} | ||
* @returns {T extends any[] ? T : Array<string>} | ||
*/ | ||
export function bigram<T extends string | string[]>(value?: T): T[] | ||
export function bigram<T extends string | unknown[]>( | ||
value?: T | undefined | ||
): T extends any[] ? T : string[] | ||
/** | ||
* Create n-grams from a given value. | ||
* | ||
* @template {string|string[]} T | ||
* @template {string|Array<unknown>} T | ||
* @param {T} [value] | ||
* @returns {T[]} | ||
* @returns {T extends any[] ? T : Array<string>} | ||
*/ | ||
export function trigram<T extends string | string[]>(value?: T): T[] | ||
export function trigram<T extends string | unknown[]>( | ||
value?: T | undefined | ||
): T extends any[] ? T : string[] |
22
index.js
@@ -1,3 +0,3 @@ | ||
export var bigram = nGram(2) | ||
export var trigram = nGram(3) | ||
export const bigram = nGram(2) | ||
export const trigram = nGram(3) | ||
@@ -24,13 +24,10 @@ /** | ||
* | ||
* @template {string|string[]} T | ||
* @template {string|Array<unknown>} T | ||
* @param {T} [value] | ||
* @returns {T[]} | ||
* @returns {T extends any[] ? T : Array<string>} | ||
*/ | ||
function grams(value) { | ||
/** @type {T[]} */ | ||
var nGrams = [] | ||
/** @type {number} */ | ||
var index | ||
/** @type {string|string[]} */ | ||
var source | ||
/** @type {T extends any[] ? T : Array<string>} */ | ||
// @ts-expect-error: pretty sure this is fine. | ||
const nGrams = [] | ||
@@ -41,4 +38,4 @@ if (value === null || value === undefined) { | ||
source = value.slice ? value : String(value) | ||
index = source.length - n + 1 | ||
const source = typeof value.slice === 'function' ? value : String(value) | ||
let index = source.length - n + 1 | ||
@@ -50,3 +47,2 @@ if (index < 1) { | ||
while (index--) { | ||
// @ts-ignore | ||
nGrams[index] = source.slice(index, index + n) | ||
@@ -53,0 +49,0 @@ } |
{ | ||
"name": "n-gram", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Get n-grams from text", | ||
@@ -39,16 +39,15 @@ "license": "MIT", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
"xo": "^0.52.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && 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-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -65,8 +64,3 @@ }, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"import/no-mutable-exports": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
"prettier": true | ||
}, | ||
@@ -73,0 +67,0 @@ "remarkConfig": { |
@@ -10,9 +10,33 @@ # n-gram | ||
## Contents | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`nGram(n)`](#ngramn) | ||
* [`bigram(value)`](#bigramvalue) | ||
* [`trigram(value)`](#trigramvalue) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Related](#related) | ||
* [Contribute](#contribute) | ||
* [Security](#security) | ||
* [License](#license) | ||
## What is this? | ||
This package gets you [bigrams][wiki] (or any n-gram, really). | ||
## When should I use this? | ||
You’re probably dealing with natural language, and know you need this, if | ||
you’re here! | ||
## Install | ||
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -22,2 +46,16 @@ npm install n-gram | ||
In Deno with [`esm.sh`][esmsh]: | ||
```js | ||
import {nGram} from 'https://esm.sh/n-gram@2' | ||
``` | ||
In browsers with [`esm.sh`][esmsh]: | ||
```html | ||
<script type="module"> | ||
import {nGram} from 'https://esm.sh/n-gram@2?bundle' | ||
</script> | ||
``` | ||
## Use | ||
@@ -42,3 +80,3 @@ | ||
This package exports the following identifiers: `ngram`, `bigram`, and `trigram`. | ||
This package exports the identifiers `nGram`, `bigram`, and `trigram`. | ||
There is no default export. | ||
@@ -50,3 +88,3 @@ | ||
Want padding? | ||
Want padding (to include partial matches)? | ||
Use something like the following: `nGram(2)(' ' + value + ' ')` | ||
@@ -62,2 +100,27 @@ | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports no additional types. | ||
## Compatibility | ||
This package is at least compatible with all maintained versions of Node.js. | ||
As of now, that is Node.js 14.14+, 16.0+, and 18.0+. | ||
It also works in Deno and modern browsers. | ||
## Related | ||
* [`wooorm/franc`](https://github.com/wooorm/franc) | ||
— natural language detection | ||
## Contribute | ||
Yes please! | ||
See [How to Contribute to Open Source][contribute]. | ||
## Security | ||
This package is safe. | ||
## License | ||
@@ -87,2 +150,6 @@ | ||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c | ||
[esmsh]: https://esm.sh | ||
[license]: license | ||
@@ -93,1 +160,5 @@ | ||
[wiki]: https://en.wikipedia.org/wiki/N-gram | ||
[typescript]: https://www.typescriptlang.org | ||
[contribute]: https://opensource.guide/how-to-contribute/ |
8159
9
72
158
30456