modern-ahocorasick
Advanced tools
Comparing version 1.0.1 to 1.0.2-alpha.1
@@ -1,92 +0,85 @@ | ||
'use strict'; | ||
class AhoCorasick { | ||
constructor(keywords) { | ||
const { failure, gotoFn, output } = this._buildTables(keywords); | ||
this.gotoFn = gotoFn; | ||
this.output = output; | ||
this.failure = failure; | ||
} | ||
_buildTables(keywords) { | ||
const gotoFn = { | ||
0: {} | ||
}; | ||
const output = {}; | ||
let state = 0; | ||
for (const word of keywords) { | ||
let curr = 0; | ||
for (const l of word) { | ||
if (gotoFn[curr] && l in gotoFn[curr]) { | ||
curr = gotoFn[curr][l]; | ||
} | ||
else { | ||
state++; | ||
gotoFn[curr][l] = state; | ||
gotoFn[state] = {}; | ||
curr = state; | ||
output[state] = []; | ||
} | ||
} | ||
output[curr].push(word); | ||
// src/index.ts | ||
var AhoCorasick = class { | ||
constructor(keywords) { | ||
const { failure, gotoFn, output } = this._buildTables(keywords); | ||
this.gotoFn = gotoFn; | ||
this.output = output; | ||
this.failure = failure; | ||
} | ||
_buildTables(keywords) { | ||
const gotoFn = { | ||
0: {} | ||
}; | ||
const output = {}; | ||
let state = 0; | ||
for (const word of keywords) { | ||
let curr = 0; | ||
for (const l of word) { | ||
if (gotoFn[curr] && l in gotoFn[curr]) { | ||
curr = gotoFn[curr][l]; | ||
} else { | ||
state++; | ||
gotoFn[curr][l] = state; | ||
gotoFn[state] = {}; | ||
curr = state; | ||
output[state] = []; | ||
} | ||
const failure = {}; | ||
const xs = []; | ||
// f(s) = 0 for all states of depth 1 (the ones from which the 0 state can transition to) | ||
for (const l in gotoFn[0]) { | ||
const state = gotoFn[0][l]; | ||
failure[state] = 0; | ||
xs.push(state); | ||
} | ||
while (xs.length > 0) { | ||
const r = xs.shift(); | ||
if (r !== undefined) { | ||
for (const l in gotoFn[r]) { | ||
const s = gotoFn[r][l]; | ||
xs.push(s); | ||
// set state = f(r) | ||
let state = failure[r]; | ||
while (state > 0 && !(l in gotoFn[state])) { | ||
state = failure[state]; | ||
} | ||
if (l in gotoFn[state]) { | ||
const fs = gotoFn[state][l]; | ||
failure[s] = fs; | ||
output[s] = [...output[s], ...output[fs]]; | ||
} | ||
else { | ||
failure[s] = 0; | ||
} | ||
} | ||
} | ||
// for each symbol a such that g(r, a) = s | ||
} | ||
return { | ||
gotoFn, | ||
output, | ||
failure | ||
}; | ||
} | ||
output[curr].push(word); | ||
} | ||
search(str) { | ||
let state = 0; | ||
const results = []; | ||
// eslint-disable-next-line unicorn/no-for-loop | ||
for (let i = 0; i < str.length; i++) { | ||
const l = str[i]; | ||
while (state > 0 && !(l in this.gotoFn[state])) { | ||
state = this.failure[state]; | ||
} | ||
// 使用 object ,表情符号出现问题 | ||
if (!(l in this.gotoFn[state])) { | ||
continue; | ||
} | ||
state = this.gotoFn[state][l]; | ||
if (this.output[state].length > 0) { | ||
const foundStrs = this.output[state]; | ||
results.push([i, foundStrs]); | ||
} | ||
const failure = {}; | ||
const xs = []; | ||
for (const l in gotoFn[0]) { | ||
const state2 = gotoFn[0][l]; | ||
failure[state2] = 0; | ||
xs.push(state2); | ||
} | ||
while (xs.length > 0) { | ||
const r = xs.shift(); | ||
if (r !== void 0) { | ||
for (const l in gotoFn[r]) { | ||
const s = gotoFn[r][l]; | ||
xs.push(s); | ||
let state2 = failure[r]; | ||
while (state2 > 0 && !(l in gotoFn[state2])) { | ||
state2 = failure[state2]; | ||
} | ||
if (l in gotoFn[state2]) { | ||
const fs = gotoFn[state2][l]; | ||
failure[s] = fs; | ||
output[s] = [...output[s], ...output[fs]]; | ||
} else { | ||
failure[s] = 0; | ||
} | ||
} | ||
return results; | ||
} | ||
} | ||
} | ||
module.exports = AhoCorasick; | ||
return { | ||
gotoFn, | ||
output, | ||
failure | ||
}; | ||
} | ||
search(str) { | ||
let state = 0; | ||
const results = []; | ||
for (let i = 0; i < str.length; i++) { | ||
const l = str[i]; | ||
while (state > 0 && !(l in this.gotoFn[state])) { | ||
state = this.failure[state]; | ||
} | ||
if (!(l in this.gotoFn[state])) { | ||
continue; | ||
} | ||
state = this.gotoFn[state][l]; | ||
if (this.output[state].length > 0) { | ||
const foundStrs = this.output[state]; | ||
results.push([i, foundStrs]); | ||
} | ||
} | ||
return results; | ||
} | ||
}; | ||
export { | ||
AhoCorasick as default | ||
}; |
{ | ||
"name": "modern-ahocorasick", | ||
"version": "1.0.1", | ||
"version": "1.0.2-alpha.1", | ||
"description": "modern-ahocorasick", | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "types/index.d.ts", | ||
"type": "module", | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
}, | ||
"./*": "./*" | ||
}, | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"./dist/*", | ||
"./dist/index.d.ts" | ||
] | ||
} | ||
}, | ||
"scripts": { | ||
"dev": "pnpm clean && pnpm dts && cross-env NODE_ENV=development rollup --config rollup.config.ts --configPlugin typescript -w", | ||
"build": "pnpm clean && pnpm dts && cross-env NODE_ENV=production rollup --config rollup.config.ts --configPlugin typescript", | ||
"build:tsc": "cross-env NODE_ENV=development tsc --build tsconfig.json", | ||
"dts": "tsc --emitDeclarationOnly -p tsconfig.dts.json", | ||
"dev": "tsup --watch --sourcemap", | ||
"build:dev": "tsup --sourcemap", | ||
"build": "tsup", | ||
"test:dev": "vitest", | ||
@@ -20,7 +37,6 @@ "test": "vitest run", | ||
"ls:pack": "npm pack --dry-run", | ||
"prepare": "ts-patch install -s", | ||
"semantic-release": "semantic-release", | ||
"sync": "cnpm sync modern-ahocorasick" | ||
}, | ||
"packageManager": "pnpm@8.6.9", | ||
"packageManager": "pnpm@9.1.0", | ||
"repository": { | ||
@@ -46,40 +62,21 @@ "type": "git", | ||
"devDependencies": { | ||
"@rollup/plugin-alias": "^5.0.0", | ||
"@rollup/plugin-commonjs": "^25.0.3", | ||
"@rollup/plugin-json": "^6.0.0", | ||
"@rollup/plugin-node-resolve": "^15.1.0", | ||
"@rollup/plugin-replace": "^5.0.2", | ||
"@rollup/plugin-terser": "^0.4.3", | ||
"@rollup/plugin-typescript": "^11.1.2", | ||
"@tsconfig/recommended": "^1.0.2", | ||
"@types/klaw": "^3.0.3", | ||
"@types/lodash": "^4.14.197", | ||
"@types/lodash-es": "^4.17.8", | ||
"@types/node": "^20.4.9", | ||
"@types/semantic-release": "^20.0.1", | ||
"@vitest/coverage-v8": "^0.34.1", | ||
"@icebreakers/eslint-config": "^0.1.0", | ||
"@tsconfig/recommended": "^1.0.6", | ||
"@types/klaw": "^3.0.6", | ||
"@types/lodash": "^4.17.1", | ||
"@types/lodash-es": "^4.17.12", | ||
"@types/node": "^20.12.12", | ||
"@vitest/coverage-v8": "^1.6.0", | ||
"cross-env": "^7.0.3", | ||
"defu": "^6.1.2", | ||
"del": "^7.0.0", | ||
"eslint": "8.47.0", | ||
"eslint-config-icebreaker": "^1.2.2", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-prettier": "^5.0.0", | ||
"fast-sort": "^3.4.0", | ||
"defu": "^6.1.4", | ||
"del": "^7.1.0", | ||
"eslint": "9.2.0", | ||
"klaw": "^4.1.0", | ||
"lodash": "^4.17.21", | ||
"lodash-es": "^4.17.21", | ||
"only-allow": "^1.1.1", | ||
"prettier": "^3.0.1", | ||
"rollup": "^3.28.0", | ||
"rollup-plugin-visualizer": "^5.9.2", | ||
"semantic-release": "^21.0.7", | ||
"ts-node": "^10.9.1", | ||
"ts-patch": "^3.0.2", | ||
"tsconfig-paths": "^4.2.0", | ||
"tslib": "^2.6.1", | ||
"typescript": "^5.1.6", | ||
"typescript-transform-paths": "^3.4.6", | ||
"vitest": "^0.34.1" | ||
"semantic-release": "^23.1.1", | ||
"tsup": "^8.0.2", | ||
"typescript": "^5.4.5", | ||
"vitest": "^1.6.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18
7
203
Yes
9987
1
1