···11+---
22+"@atcute/util-text": minor
33+---
44+55+add isGraphemeLengthInRange function
+25
packages/misc/util-text/lib/index.rn.ts
···88export const getGraphemeLength = (text: string): number => {
99 return countGraphemes(text);
1010};
1111+1212+/**
1313+ * checks if the grapheme length of a string is within the specified range
1414+ * @param text string to check
1515+ * @param min minimum grapheme length (inclusive)
1616+ * @param max maximum grapheme length (inclusive)
1717+ * @returns true if the grapheme length is within range
1818+ */
1919+export const isGraphemeLengthInRange = (text: string, min: number, max: number): boolean => {
2020+ const utf16Len = text.length;
2121+2222+ // UTF-16 length < min means grapheme count < min
2323+ if (utf16Len < min) {
2424+ return false;
2525+ }
2626+2727+ // if there's no minimum constraint and UTF-16 length is within max,
2828+ // grapheme count is definitely within max
2929+ if (min === 0 && utf16Len <= max) {
3030+ return true;
3131+ }
3232+3333+ const count = countGraphemes(text);
3434+ return count >= min && count <= max;
3535+};
+35
packages/misc/util-text/lib/index.ts
···15151616 return count;
1717};
1818+1919+/**
2020+ * checks if the grapheme length of a string is within the specified range
2121+ * @param text string to check
2222+ * @param min minimum grapheme length (inclusive)
2323+ * @param max maximum grapheme length (inclusive)
2424+ * @returns true if the grapheme length is within range
2525+ */
2626+export const isGraphemeLengthInRange = (text: string, min: number, max: number): boolean => {
2727+ const utf16Len = text.length;
2828+2929+ // UTF-16 length < min means grapheme count < min
3030+ if (utf16Len < min) {
3131+ return false;
3232+ }
3333+3434+ // if there's no minimum constraint and UTF-16 length is within max,
3535+ // grapheme count is definitely within max
3636+ if (min === 0 && utf16Len <= max) {
3737+ return true;
3838+ }
3939+4040+ // count graphemes with early termination
4141+ const iterator = segmenter.segment(text)[Symbol.iterator]();
4242+ let count = 0;
4343+4444+ while (!iterator.next().done) {
4545+ count++;
4646+ if (count > max) {
4747+ return false;
4848+ }
4949+ }
5050+5151+ return count >= min;
5252+};