Skip to content

Commit

Permalink
Added length range method
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Sep 13, 2023
1 parent 8d684b7 commit 9c569f9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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

0 comments on commit 9c569f9

Please sign in to comment.