From 7fc2af36bfb27475611456ef4440c99b9fcdad0d Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Wed, 13 Sep 2023 18:16:25 +0200 Subject: [PATCH] Added length range method (#77) --- src/js/string-set.ts | 15 +++++++++++++++ src/js/unicode-set.ts | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/js/string-set.ts b/src/js/string-set.ts index 954f843..10da931 100644 --- a/src/js/string-set.ts +++ b/src/js/string-set.ts @@ -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 { diff --git a/src/js/unicode-set.ts b/src/js/unicode-set.ts index 2bdc823..266a7e5 100644 --- a/src/js/unicode-set.ts +++ b/src/js/unicode-set.ts @@ -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[] {