Skip to content

Commit

Permalink
feature wrapper might return null
Browse files Browse the repository at this point in the history
  • Loading branch information
leyhline committed Jun 22, 2023
1 parent dbbcf11 commit 55612dd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
18 changes: 5 additions & 13 deletions src/Dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface Dictionary<T extends Features = Record<string, never>> {
url: string;
cacheName: string;
wrapper?: (feature: string[]) => T;
wrapper?: (feature: string[]) => T | null;
}

export const UNIDIC2: Dictionary<UnidicFeature26> = {
Expand All @@ -28,12 +28,8 @@ export const JUMANDIC: Dictionary = {

export type Features = { [key: string]: string };

function createUnidicFeature26(features: string[]): UnidicFeature26 {
const numberOfFields = 26;
console.assert(
features.length === numberOfFields,
`Invalid feature length, expected ${numberOfFields}, got ${features.length}`
);
function createUnidicFeature26(features: string[]): UnidicFeature26 | null {
if (features.length !== 26) return null;
return {
pos1: features[0],
pos2: features[1],
Expand Down Expand Up @@ -93,12 +89,8 @@ export interface UnidicFeature26 extends Features {
aModType: string;
}

function createUnidicFeature29(features: string[]): UnidicFeature29 {
const numberOfFields = 29;
console.assert(
features.length === numberOfFields,
`Invalid feature length, expected ${numberOfFields}, got ${features.length}`
);
function createUnidicFeature29(features: string[]): UnidicFeature29 | null {
if (features.length !== 29) return null;
return {
pos1: features[0],
pos2: features[1],
Expand Down
8 changes: 4 additions & 4 deletions src/MecabWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ interface MecabWorkerOptions {
* @example
* const worker = await MecabWorker.create(UNIDIC3)
* const result = await worker.parse('青森県と秋田県にまたがり所在する十和田湖、御鼻部山展望台からの展望')
* console.log(result)
* const nodes = await worker.parseToNodes('青森県と秋田県にまたがり所在する十和田湖、御鼻部山展望台からの展望')
*/
export class MecabWorker<T extends Features = Record<string, never>> {
private worker: Worker;
private wrapper?: (feature: string[]) => T;
private wrapper?: (feature: string[]) => T | null;
/**
* Helper function to call `MecabWorker.init` after construction.
*/
Expand All @@ -105,7 +105,7 @@ export class MecabWorker<T extends Features = Record<string, never>> {
return mecabWorker.init(dictionary, options).then(() => mecabWorker);
}

constructor(wrapper?: (feature: string[]) => T) {
constructor(wrapper?: (feature: string[]) => T | null) {
if (!testModuleWorkerSupport()) {
throw new Error(
"Cannot initialize MeCab. Module workers are not supported in your browser."
Expand Down Expand Up @@ -188,7 +188,7 @@ export class MecabWorker<T extends Features = Record<string, never>> {
}

/**
* Link to stackoverflow
* https://stackoverflow.com/questions/62954570/javascript-feature-detect-module-support-for-web-workers
*/
function testModuleWorkerSupport(): boolean {
let supports = false;
Expand Down
2 changes: 1 addition & 1 deletion src/mecab-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class MecabNode<T extends Features = Record<string, never>> {
readonly stat: Stat;

features: string[] = [];
feature: T = {} as T;
feature: T | null = null;

constructor(Module: Module, nodePtr: number) {
this.nextPtr = Module.getValue(nodePtr + this.nextPtrOffset, "*");
Expand Down

0 comments on commit 55612dd

Please sign in to comment.