Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added length range method #77

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/js/string-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,21 @@ export class StringSet {
}
return new StringSet(items, this._caseFolding);
}

/**
* Returns the minimum and maximum length of words in this set.
*
* If this set is empty, `undefined` will be returned returned.
*/
getLengthRange(): { min: number; max: number } | undefined {
if (this.isEmpty) {
return undefined;
}

const min = this.words[0].length;
const max = this.words[this.words.length - 1].length;
return { min, max };
}
}

function normalize(items: ReadonlyWord[]): void {
Expand Down
21 changes: 21 additions & 0 deletions src/js/unicode-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ export class UnicodeSet {
return this.chars.isDisjointWith(other);
}
}

/**
* Returns the minimum and maximum length of words in this set.
*
* If this set is empty, `undefined` will be returned returned.
*/
getLengthRange(): { min: number; max: number } | undefined {
if (this.chars.isEmpty) {
return this.accept.getLengthRange();
} else {
const wordRange = this.accept.getLengthRange();
if (wordRange === undefined) {
return { min: 1, max: 1 };
} else {
return {
min: Math.min(1, wordRange.min),
max: Math.max(1, wordRange.max),
};
}
}
}
}

function toWordSets(set: UnicodeSet): readonly ReadonlyWordSet[] {
Expand Down
Loading