Skip to content

Commit 7fc2af3

Browse files
Added length range method (#77)
1 parent 8d684b7 commit 7fc2af3

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/js/string-set.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,21 @@ export class StringSet {
349349
}
350350
return new StringSet(items, this._caseFolding);
351351
}
352+
353+
/**
354+
* Returns the minimum and maximum length of words in this set.
355+
*
356+
* If this set is empty, `undefined` will be returned returned.
357+
*/
358+
getLengthRange(): { min: number; max: number } | undefined {
359+
if (this.isEmpty) {
360+
return undefined;
361+
}
362+
363+
const min = this.words[0].length;
364+
const max = this.words[this.words.length - 1].length;
365+
return { min, max };
366+
}
352367
}
353368

354369
function normalize(items: ReadonlyWord[]): void {

src/js/unicode-set.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,27 @@ export class UnicodeSet {
191191
return this.chars.isDisjointWith(other);
192192
}
193193
}
194+
195+
/**
196+
* Returns the minimum and maximum length of words in this set.
197+
*
198+
* If this set is empty, `undefined` will be returned returned.
199+
*/
200+
getLengthRange(): { min: number; max: number } | undefined {
201+
if (this.chars.isEmpty) {
202+
return this.accept.getLengthRange();
203+
} else {
204+
const wordRange = this.accept.getLengthRange();
205+
if (wordRange === undefined) {
206+
return { min: 1, max: 1 };
207+
} else {
208+
return {
209+
min: Math.min(1, wordRange.min),
210+
max: Math.max(1, wordRange.max),
211+
};
212+
}
213+
}
214+
}
194215
}
195216

196217
function toWordSets(set: UnicodeSet): readonly ReadonlyWordSet[] {

0 commit comments

Comments
 (0)