|
| 1 | +/*-------------------------------------------------------------------------- |
| 2 | +
|
| 3 | +TypeBox |
| 4 | +
|
| 5 | +The MIT License (MIT) |
| 6 | +
|
| 7 | +Copyright (c) 2017-2025 Haydn Paterson |
| 8 | +
|
| 9 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +of this software and associated documentation files (the "Software"), to deal |
| 11 | +in the Software without restriction, including without limitation the rights |
| 12 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +copies of the Software, and to permit persons to whom the Software is |
| 14 | +furnished to do so, subject to the following conditions: |
| 15 | +
|
| 16 | +The above copyright notice and this permission notice shall be included in |
| 17 | +all copies or substantial portions of the Software. |
| 18 | +
|
| 19 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +THE SOFTWARE. |
| 26 | +
|
| 27 | +---------------------------------------------------------------------------*/ |
| 28 | + |
| 29 | +// -------------------------------------------------------------------------- |
| 30 | +// IsBetween |
| 31 | +// -------------------------------------------------------------------------- |
| 32 | +function IsBetween(value: number, min: number, max: number): boolean { |
| 33 | + return value >= min && value <= max |
| 34 | +} |
| 35 | +// -------------------------------------------------------------------------- |
| 36 | +// IsRegionalIndicator |
| 37 | +// -------------------------------------------------------------------------- |
| 38 | +function IsRegionalIndicator(value: number): boolean { |
| 39 | + return IsBetween(value, 0x1F1E6, 0x1F1FF) |
| 40 | +} |
| 41 | +// -------------------------------------------------------------------------- |
| 42 | +// IsVariationSelector |
| 43 | +// -------------------------------------------------------------------------- |
| 44 | +function IsVariationSelector(value: number): boolean { |
| 45 | + return IsBetween(value, 0xFE00, 0xFE0F) |
| 46 | +} |
| 47 | +// -------------------------------------------------------------------------- |
| 48 | +// IsCombiningMark |
| 49 | +// -------------------------------------------------------------------------- |
| 50 | +function IsCombiningMark(value: number): boolean { |
| 51 | + return ( |
| 52 | + IsBetween(value, 0x0300, 0x036F) || |
| 53 | + IsBetween(value, 0x1AB0, 0x1AFF) || |
| 54 | + IsBetween(value, 0x1DC0, 0x1DFF) || |
| 55 | + IsBetween(value, 0xFE20, 0xFE2F) |
| 56 | + ) |
| 57 | +} |
| 58 | +// -------------------------------------------------------------------------- |
| 59 | +// CodePointLength |
| 60 | +// -------------------------------------------------------------------------- |
| 61 | +function CodePointLength(value: number): number { |
| 62 | + return value > 0xFFFF ? 2 : 1 |
| 63 | +} |
| 64 | +// -------------------------------------------------------------------------- |
| 65 | +// ConsumeModifiers (helper) |
| 66 | +// -------------------------------------------------------------------------- |
| 67 | +function ConsumeModifiers(value: string, index: number): number { |
| 68 | + while (index < value.length) { |
| 69 | + const point = value.codePointAt(index)! |
| 70 | + if (IsCombiningMark(point) || IsVariationSelector(point)) { |
| 71 | + index += CodePointLength(point) |
| 72 | + } else { |
| 73 | + break |
| 74 | + } |
| 75 | + } |
| 76 | + return index |
| 77 | +} |
| 78 | +// -------------------------------------------------------------------------- |
| 79 | +// NextGraphemeClusterIndex |
| 80 | +// -------------------------------------------------------------------------- |
| 81 | +function NextGraphemeClusterIndex(value: string, clusterStart: number): number { |
| 82 | + const startCP = value.codePointAt(clusterStart)! |
| 83 | + let clusterEnd = clusterStart + CodePointLength(startCP) |
| 84 | + // Consume combining marks & variation selectors |
| 85 | + clusterEnd = ConsumeModifiers(value, clusterEnd) |
| 86 | + // Handle multi-ZWJ sequences |
| 87 | + while (clusterEnd < value.length - 1 && value[clusterEnd] === '\u200D') { |
| 88 | + const nextCP = value.codePointAt(clusterEnd + 1)! |
| 89 | + clusterEnd += 1 + CodePointLength(nextCP) |
| 90 | + clusterEnd = ConsumeModifiers(value, clusterEnd) |
| 91 | + } |
| 92 | + // Handle regional indicator pairs (flags) |
| 93 | + if ( |
| 94 | + IsRegionalIndicator(startCP) && |
| 95 | + clusterEnd < value.length && |
| 96 | + IsRegionalIndicator(value.codePointAt(clusterEnd)!) |
| 97 | + ) { |
| 98 | + clusterEnd += CodePointLength(value.codePointAt(clusterEnd)!) |
| 99 | + } |
| 100 | + return clusterEnd |
| 101 | +} |
| 102 | +// -------------------------------------------------------------------------- |
| 103 | +// IsGraphemeCodePoint |
| 104 | +// -------------------------------------------------------------------------- |
| 105 | +function IsGraphemeCodePoint(value: number): boolean { |
| 106 | + return ( |
| 107 | + IsBetween(value, 0xD800, 0xDBFF) || // High surrogate |
| 108 | + IsBetween(value, 0x0300, 0x036F) || // Combining diacritical marks |
| 109 | + (value === 0x200D) // Zero-width joiner |
| 110 | + ) |
| 111 | +} |
| 112 | +// -------------------------------------------------------------------------- |
| 113 | +// GraphemeCount |
| 114 | +// -------------------------------------------------------------------------- |
| 115 | +/** Returns the number of grapheme clusters in a string */ |
| 116 | +export function GraphemeCount(value: string): number { |
| 117 | + let count = 0 |
| 118 | + let index = 0 |
| 119 | + while (index < value.length) { |
| 120 | + index = NextGraphemeClusterIndex(value, index) |
| 121 | + count++ |
| 122 | + } |
| 123 | + return count |
| 124 | +} |
| 125 | +// -------------------------------------------------------------------------- |
| 126 | +// IsMinLength |
| 127 | +// -------------------------------------------------------------------------- |
| 128 | +/** Checks if a string has at least a minimum number of grapheme clusters */ |
| 129 | +export function IsMinLength(value: string, minLength: number): boolean { |
| 130 | + let count = 0 |
| 131 | + let index = 0 |
| 132 | + while (index < value.length) { |
| 133 | + index = NextGraphemeClusterIndex(value, index) |
| 134 | + count++ |
| 135 | + if (count >= minLength) return true |
| 136 | + } |
| 137 | + return false |
| 138 | +} |
| 139 | +// -------------------------------------------------------------------------- |
| 140 | +// IsMaxLength |
| 141 | +// -------------------------------------------------------------------------- |
| 142 | +/** Checks if a string has at most a maximum number of grapheme clusters */ |
| 143 | +export function IsMaxLength(value: string, maxLength: number): boolean { |
| 144 | + let count = 0 |
| 145 | + let index = 0 |
| 146 | + while (index < value.length) { |
| 147 | + index = NextGraphemeClusterIndex(value, index) |
| 148 | + count++ |
| 149 | + if (count > maxLength) return false |
| 150 | + } |
| 151 | + return true |
| 152 | +} |
| 153 | +// -------------------------------------------------------------------------- |
| 154 | +// IsMinLengthFast |
| 155 | +// -------------------------------------------------------------------------- |
| 156 | +/** Fast check for minimum grapheme length, falls back to full check if needed */ |
| 157 | +export function IsMinLengthFast(value: string, minLength: number): boolean { |
| 158 | + let index = 0 |
| 159 | + while (index < value.length) { |
| 160 | + if (IsGraphemeCodePoint(value.charCodeAt(index))) { |
| 161 | + return IsMinLength(value, minLength) |
| 162 | + } |
| 163 | + index++ |
| 164 | + if (index >= minLength) return true |
| 165 | + } |
| 166 | + return false |
| 167 | +} |
| 168 | +// -------------------------------------------------------------------------- |
| 169 | +// IsMaxLengthFast |
| 170 | +// -------------------------------------------------------------------------- |
| 171 | +/** Fast check for maximum grapheme length, falls back to full check if needed */ |
| 172 | +export function IsMaxLengthFast(value: string, maxLength: number): boolean { |
| 173 | + let index = 0 |
| 174 | + while (index < value.length) { |
| 175 | + if (IsGraphemeCodePoint(value.charCodeAt(index))) { |
| 176 | + return IsMaxLength(value, maxLength) |
| 177 | + } |
| 178 | + index++ |
| 179 | + if (index > maxLength) return false |
| 180 | + } |
| 181 | + return true |
| 182 | +} |
0 commit comments