-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
50 lines (42 loc) · 1.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export const bigram = nGram(2)
export const trigram = nGram(3)
/**
* 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`')
}
return grams
/**
* Create n-grams from a given value.
*
* @template {string|Array<unknown>} T
* @param {T} [value]
* @returns {T extends any[] ? T : Array<string>}
*/
function grams(value) {
/** @type {T extends any[] ? T : Array<string>} */
// @ts-expect-error: pretty sure this is fine.
const nGrams = []
if (value === null || value === undefined) {
return nGrams
}
const source = typeof value.slice === 'function' ? value : String(value)
let index = source.length - n + 1
if (index < 1) {
return nGrams
}
while (index--) {
nGrams[index] = source.slice(index, index + n)
}
return nGrams
}
}