diff --git a/packages/runes2/dist/index.cjs.development.cjs b/packages/runes2/dist/index.cjs.development.cjs index d78d178..6d34ea7 100644 --- a/packages/runes2/dist/index.cjs.development.cjs +++ b/packages/runes2/dist/index.cjs.development.cjs @@ -15,8 +15,14 @@ var EnumRunesCode; EnumRunesCode[EnumRunesCode["DIACRITICAL_MARKS_END"] = 8447] = "DIACRITICAL_MARKS_END"; EnumRunesCode[EnumRunesCode["ZWJ"] = 8205] = "ZWJ"; })(EnumRunesCode || (EnumRunesCode = {})); -const GRAPHEMS = /*#__PURE__*/Object.freeze([0x0308, 0x0937, 0x0937, 0x093F, 0x093F, 0x0BA8, 0x0BBF, 0x0BCD, 0x0E31, 0x0E33, 0x0E40, 0x0E49, 0x1100, 0x1161, 0x11A8]); -function _runes(string) { +const GRAPHEMS = /*#__PURE__*/Object.freeze([0x0308, 0x0937, 0x093F, 0x0BA8, 0x0BBF, 0x0BCD, 0x0E31, 0x0E33, 0x0E40, 0x0E49, 0x1100, 0x1161, 0x11A8]); +var EnumCodeUnits; +(function (EnumCodeUnits) { + EnumCodeUnits[EnumCodeUnits["unit_1"] = 1] = "unit_1"; + EnumCodeUnits[EnumCodeUnits["unit_2"] = 2] = "unit_2"; + EnumCodeUnits[EnumCodeUnits["unit_4"] = 4] = "unit_4"; +})(EnumCodeUnits || (EnumCodeUnits = {})); +function runes(string) { if (typeof string !== 'string') { throw new TypeError('string cannot be undefined or null'); } @@ -75,7 +81,7 @@ function isDiacriticalMark(string) { return typeof string === 'string' && betweenInclusive(string.charCodeAt(0), 8400, 8447); } function isGraphem(string) { - return typeof string === 'string' && GRAPHEMS.indexOf(string.charCodeAt(0)) !== -1; + return typeof string === 'string' && GRAPHEMS.includes(string.charCodeAt(0)); } function isZeroWidthJoiner(string) { return typeof string === 'string' && string.charCodeAt(0) === 8205; @@ -89,7 +95,7 @@ function betweenInclusive(value, lower, upper) { return value >= lower && value <= upper; } function substring(string, start, width) { - const chars = _runes(string); + const chars = runes(string); if (start === undefined) { return string; } @@ -105,29 +111,32 @@ function substring(string, start, width) { return chars.slice(start, endIndex).join(''); } { - Object.defineProperty(_runes, 'runes', { - value: _runes + Object.defineProperty(runes, 'runes', { + value: runes }); - Object.defineProperty(_runes, 'default', { - value: _runes + Object.defineProperty(runes, 'default', { + value: runes }); - Object.defineProperty(_runes, "__esModule", { + Object.defineProperty(runes, "__esModule", { value: true }); - Object.defineProperty(_runes, 'substr', { + Object.defineProperty(runes, 'substr', { value: substring }); - Object.defineProperty(_runes, 'substring', { + Object.defineProperty(runes, 'substring', { value: substring }); - Object.defineProperty(_runes, 'EnumRunesCode', { + Object.defineProperty(runes, 'EnumRunesCode', { value: EnumRunesCode }); - Object.defineProperty(_runes, 'GRAPHEMS', { + Object.defineProperty(runes, 'EnumCodeUnits', { + value: EnumCodeUnits + }); + Object.defineProperty(runes, 'GRAPHEMS', { value: GRAPHEMS }); } // @ts-ignore -module.exports = _runes; +module.exports = runes; //# sourceMappingURL=index.cjs.development.cjs.map diff --git a/packages/runes2/dist/index.cjs.production.min.cjs b/packages/runes2/dist/index.cjs.production.min.cjs index cd9af6e..3052c2f 100644 --- a/packages/runes2/dist/index.cjs.production.min.cjs +++ b/packages/runes2/dist/index.cjs.production.min.cjs @@ -11,26 +11,28 @@ var e; e[e.DIACRITICAL_MARKS_END = 8447] = "DIACRITICAL_MARKS_END", e[e.ZWJ = 8205] = "ZWJ"; }(e || (e = {})); -const n = Object.freeze([ 0x0308, 0x0937, 0x0937, 0x093F, 0x093F, 0x0BA8, 0x0BBF, 0x0BCD, 0x0E31, 0x0E33, 0x0E40, 0x0E49, 0x1100, 0x1161, 0x11A8 ]); +const n = Object.freeze([ 0x0308, 0x0937, 0x093F, 0x0BA8, 0x0BBF, 0x0BCD, 0x0E31, 0x0E33, 0x0E40, 0x0E49, 0x1100, 0x1161, 0x11A8 ]); -function _runes(e) { +var t; + +function runes(e) { if ("string" != typeof e) throw new TypeError("string cannot be undefined or null"); const n = []; - let r = 0, t = 0; - for (;r < e.length; ) t += nextUnits(r + t, e), isGraphem(e[r + t]) && t++, isVariationSelector(e[r + t]) && t++, - isDiacriticalMark(e[r + t]) && t++, isZeroWidthJoiner(e[r + t]) ? t++ : (n.push(e.substring(r, r + t)), - r += t, t = 0); + let t = 0, r = 0; + for (;t < e.length; ) r += nextUnits(t + r, e), isGraphem(e[t + r]) && r++, isVariationSelector(e[t + r]) && r++, + isDiacriticalMark(e[t + r]) && r++, isZeroWidthJoiner(e[t + r]) ? r++ : (n.push(e.substring(t, t + r)), + t += r, r = 0); return n; } function nextUnits(e, n) { - const r = n[e]; + const t = n[e]; if (!function isFirstOfSurrogatePair(e) { return e && betweenInclusive(e[0].charCodeAt(0), 55296, 56319); - }(r) || e === n.length - 1) return 1; - const t = r + n[e + 1]; + }(t) || e === n.length - 1) return 1; + const r = t + n[e + 1]; let i = n.substring(e + 2, e + 5); - return isRegionalIndicator(t) && isRegionalIndicator(i) || function isFitzpatrickModifier(e) { + return isRegionalIndicator(r) && isRegionalIndicator(i) || function isFitzpatrickModifier(e) { return betweenInclusive(codePointFromSurrogatePair(e), 127995, 127999); }(i) ? 4 : 2; } @@ -48,7 +50,7 @@ function isDiacriticalMark(e) { } function isGraphem(e) { - return "string" == typeof e && -1 !== n.indexOf(e.charCodeAt(0)); + return "string" == typeof e && n.includes(e.charCodeAt(0)); } function isZeroWidthJoiner(e) { @@ -59,32 +61,36 @@ function codePointFromSurrogatePair(e) { return (e.charCodeAt(0) - 55296 << 10) + (e.charCodeAt(1) - 56320) + 0x10000; } -function betweenInclusive(e, n, r) { - return e >= n && e <= r; +function betweenInclusive(e, n, t) { + return e >= n && e <= t; } -function substring(e, n, r) { - const t = _runes(e); +function substring(e, n, t) { + const r = runes(e); if (void 0 === n) return e; - if (n >= t.length) return ""; - const i = t.length - n; - let o = n + (void 0 === r ? i : r); - return o > n + i && (o = void 0), t.slice(n, o).join(""); + if (n >= r.length) return ""; + const i = r.length - n; + let u = n + (void 0 === t ? i : t); + return u > n + i && (u = void 0), r.slice(n, u).join(""); } -Object.defineProperty(_runes, "runes", { - value: _runes -}), Object.defineProperty(_runes, "default", { - value: _runes -}), Object.defineProperty(_runes, "__esModule", { +!function(e) { + e[e.unit_1 = 1] = "unit_1", e[e.unit_2 = 2] = "unit_2", e[e.unit_4 = 4] = "unit_4"; +}(t || (t = {})), Object.defineProperty(runes, "runes", { + value: runes +}), Object.defineProperty(runes, "default", { + value: runes +}), Object.defineProperty(runes, "__esModule", { value: !0 -}), Object.defineProperty(_runes, "substr", { +}), Object.defineProperty(runes, "substr", { value: substring -}), Object.defineProperty(_runes, "substring", { +}), Object.defineProperty(runes, "substring", { value: substring -}), Object.defineProperty(_runes, "EnumRunesCode", { +}), Object.defineProperty(runes, "EnumRunesCode", { value: e -}), Object.defineProperty(_runes, "GRAPHEMS", { +}), Object.defineProperty(runes, "EnumCodeUnits", { + value: t +}), Object.defineProperty(runes, "GRAPHEMS", { value: n -}), module.exports = _runes; +}), module.exports = runes; //# sourceMappingURL=index.cjs.production.min.cjs.map diff --git a/packages/runes2/dist/index.d.ts b/packages/runes2/dist/index.d.ts index dc0981f..20ecea2 100644 --- a/packages/runes2/dist/index.d.ts +++ b/packages/runes2/dist/index.d.ts @@ -13,8 +13,13 @@ export declare const enum EnumRunesCode { ZWJ = 8205 } export declare const GRAPHEMS: readonly number[]; -declare function _runes(string: string): string[]; -export declare function nextUnits(i: number, string: string): 1 | 2 | 4; +export declare const enum EnumCodeUnits { + unit_1 = 1, + unit_2 = 2, + unit_4 = 4 +} +export declare function runes(string: string): string[]; +export declare function nextUnits(i: number, string: string): EnumCodeUnits; export declare function isFirstOfSurrogatePair(string: string): boolean; export declare function isRegionalIndicator(string: string): boolean; export declare function isFitzpatrickModifier(string: string): boolean; @@ -27,8 +32,7 @@ export declare function betweenInclusive(value: number, lower: number, upper: nu export declare function substring(string: string, start?: number, width?: number): string; export { - _runes as default, - _runes as runes, + runes as default, substring as substr, }; diff --git a/packages/runes2/dist/index.esm.mjs b/packages/runes2/dist/index.esm.mjs index 02a14a4..32b4886 100644 --- a/packages/runes2/dist/index.esm.mjs +++ b/packages/runes2/dist/index.esm.mjs @@ -9,24 +9,26 @@ var i; i[i.DIACRITICAL_MARKS_END = 8447] = "DIACRITICAL_MARKS_END", i[i.ZWJ = 8205] = "ZWJ"; }(i || (i = {})); -const r = Object.freeze([ 0x0308, 0x0937, 0x0937, 0x093F, 0x093F, 0x0BA8, 0x0BBF, 0x0BCD, 0x0E31, 0x0E33, 0x0E40, 0x0E49, 0x1100, 0x1161, 0x11A8 ]); +const t = Object.freeze([ 0x0308, 0x0937, 0x093F, 0x0BA8, 0x0BBF, 0x0BCD, 0x0E31, 0x0E33, 0x0E40, 0x0E49, 0x1100, 0x1161, 0x11A8 ]); -function _runes(i) { +var r; + +function runes(i) { if ("string" != typeof i) throw new TypeError("string cannot be undefined or null"); - const r = []; - let t = 0, e = 0; - for (;t < i.length; ) e += nextUnits(t + e, i), isGraphem(i[t + e]) && e++, isVariationSelector(i[t + e]) && e++, - isDiacriticalMark(i[t + e]) && e++, isZeroWidthJoiner(i[t + e]) ? e++ : (r.push(i.substring(t, t + e)), - t += e, e = 0); - return r; + const t = []; + let r = 0, n = 0; + for (;r < i.length; ) n += nextUnits(r + n, i), isGraphem(i[r + n]) && n++, isVariationSelector(i[r + n]) && n++, + isDiacriticalMark(i[r + n]) && n++, isZeroWidthJoiner(i[r + n]) ? n++ : (t.push(i.substring(r, r + n)), + r += n, n = 0); + return t; } -function nextUnits(i, r) { - const t = r[i]; - if (!isFirstOfSurrogatePair(t) || i === r.length - 1) return 1; - const e = t + r[i + 1]; - let n = r.substring(i + 2, i + 5); - return isRegionalIndicator(e) && isRegionalIndicator(n) || isFitzpatrickModifier(n) ? 4 : 2; +function nextUnits(i, t) { + const r = t[i]; + if (!isFirstOfSurrogatePair(r) || i === t.length - 1) return 1; + const n = r + t[i + 1]; + let e = t.substring(i + 2, i + 5); + return isRegionalIndicator(n) && isRegionalIndicator(e) || isFitzpatrickModifier(e) ? 4 : 2; } function isFirstOfSurrogatePair(i) { @@ -50,7 +52,7 @@ function isDiacriticalMark(i) { } function isGraphem(i) { - return "string" == typeof i && -1 !== r.indexOf(i.charCodeAt(0)); + return "string" == typeof i && t.includes(i.charCodeAt(0)); } function isZeroWidthJoiner(i) { @@ -61,18 +63,22 @@ function codePointFromSurrogatePair(i) { return (i.charCodeAt(0) - 55296 << 10) + (i.charCodeAt(1) - 56320) + 0x10000; } -function betweenInclusive(i, r, t) { - return i >= r && i <= t; +function betweenInclusive(i, t, r) { + return i >= t && i <= r; } -function substring(i, r, t) { - const e = _runes(i); - if (void 0 === r) return i; - if (r >= e.length) return ""; - const n = e.length - r; - let o = r + (void 0 === t ? n : t); - return o > r + n && (o = void 0), e.slice(r, o).join(""); +function substring(i, t, r) { + const n = runes(i); + if (void 0 === t) return i; + if (t >= n.length) return ""; + const e = n.length - t; + let o = t + (void 0 === r ? e : r); + return o > t + e && (o = void 0), n.slice(t, o).join(""); } -export { i as EnumRunesCode, r as GRAPHEMS, betweenInclusive, codePointFromSurrogatePair, _runes as default, isDiacriticalMark, isFirstOfSurrogatePair, isFitzpatrickModifier, isGraphem, isRegionalIndicator, isVariationSelector, isZeroWidthJoiner, nextUnits, _runes as runes, substring as substr, substring }; +!function(i) { + i[i.unit_1 = 1] = "unit_1", i[i.unit_2 = 2] = "unit_2", i[i.unit_4 = 4] = "unit_4"; +}(r || (r = {})); + +export { r as EnumCodeUnits, i as EnumRunesCode, t as GRAPHEMS, betweenInclusive, codePointFromSurrogatePair, runes as default, isDiacriticalMark, isFirstOfSurrogatePair, isFitzpatrickModifier, isGraphem, isRegionalIndicator, isVariationSelector, isZeroWidthJoiner, nextUnits, runes, substring as substr, substring }; //# sourceMappingURL=index.esm.mjs.map diff --git a/packages/runes2/readme.md b/packages/runes2/readme.md index 0d768b4..bee0bf6 100644 --- a/packages/runes2/readme.md +++ b/packages/runes2/readme.md @@ -44,9 +44,9 @@ import { runes } from 'runes2'; [...'๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ'] // => [ '๐Ÿ‘ฉ', '', '๐Ÿ‘ฉ', '', '๐Ÿ‘ง', '', '๐Ÿ‘ฆ' ] // Runes -_runes('โ™ฅ๏ธ') // => ['โ™ฅ๏ธ'] -_runes('Emoji ๐Ÿค–') // => ['E', 'm', 'o', 'j', 'i', ' ', '๐Ÿค–'] -_runes('๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ') // => ['๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ'] +runes('โ™ฅ๏ธ') // => ['โ™ฅ๏ธ'] +runes('Emoji ๐Ÿค–') // => ['E', 'm', 'o', 'j', 'i', ' ', '๐Ÿค–'] +runes('๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ') // => ['๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ'] ``` @@ -57,5 +57,5 @@ _runes('๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ') // => ['๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ'] '๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘งa'.substring(1) // => '๏ฟฝโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘งa' // Runes -_runes.substr('๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘งa', 1) // => 'a' +runes.substr('๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘งa', 1) // => 'a' ```