Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/compat/_internal/regexUnicode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Matches a single visible symbol (grapheme cluster) in a Unicode `string`,
* combining surrogate pairs, combining marks, variation selectors, Fitzpatrick
* modifiers, regional-indicator pairs, and zero-width-joiner sequences into a
* single match.
*
* Ported from lodash to preserve identical behavior.
* @link https://github.com/lodash/lodash/blob/4.17.21-es/_unicodeSize.js
*/

const rsAstralRange = '\\ud800-\\udfff';
const rsComboMarksRange = '\\u0300-\\u036f';
const reComboHalfMarksRange = '\\ufe20-\\ufe2f';
const rsComboSymbolsRange = '\\u20d0-\\u20ff';
const rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange;
const rsVarRange = '\\ufe0e\\ufe0f';

const rsAstral = `[${rsAstralRange}]`;
const rsCombo = `[${rsComboRange}]`;
const rsFitz = '\\ud83c[\\udffb-\\udfff]';
const rsModifier = `(?:${rsCombo}|${rsFitz})`;
const rsNonAstral = `[^${rsAstralRange}]`;
const rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}';
const rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]';
const rsZWJ = '\\u200d';

const reOptMod = `${rsModifier}?`;
const rsOptVar = `[${rsVarRange}]?`;
const rsOptJoin = `(?:${rsZWJ}(?:${[rsNonAstral, rsRegional, rsSurrPair].join('|')})${rsOptVar}${reOptMod})*`;
const rsSeq = rsOptVar + reOptMod + rsOptJoin;
const rsSymbol = `(?:${[`${rsNonAstral}${rsCombo}?`, rsCombo, rsRegional, rsSurrPair, rsAstral].join('|')})`;

export const regexUnicode = RegExp(`${rsFitz}(?=${rsFitz})|${rsSymbol}${rsSeq}`, 'g');
36 changes: 5 additions & 31 deletions src/compat/_internal/unicodeSize.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,16 @@
import { regexUnicode } from './regexUnicode.ts';

/**
* Counts the number of visible symbols (grapheme clusters) in a Unicode `string`,
* combining surrogate pairs, combining marks, variation selectors, Fitzpatrick
* modifiers, regional-indicator pairs, and zero-width-joiner sequences into a
* single symbol.
* Counts the number of visible symbols (grapheme clusters) in a Unicode `string`.
*
* Ported from lodash to preserve identical behavior.
* @link https://github.com/lodash/lodash/blob/4.17.21-es/_unicodeSize.js
*/

const rsAstralRange = '\\ud800-\\udfff';
const rsComboMarksRange = '\\u0300-\\u036f';
const reComboHalfMarksRange = '\\ufe20-\\ufe2f';
const rsComboSymbolsRange = '\\u20d0-\\u20ff';
const rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange;
const rsVarRange = '\\ufe0e\\ufe0f';

const rsAstral = `[${rsAstralRange}]`;
const rsCombo = `[${rsComboRange}]`;
const rsFitz = '\\ud83c[\\udffb-\\udfff]';
const rsModifier = `(?:${rsCombo}|${rsFitz})`;
const rsNonAstral = `[^${rsAstralRange}]`;
const rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}';
const rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]';
const rsZWJ = '\\u200d';

const reOptMod = `${rsModifier}?`;
const rsOptVar = `[${rsVarRange}]?`;
const rsOptJoin = `(?:${rsZWJ}(?:${[rsNonAstral, rsRegional, rsSurrPair].join('|')})${rsOptVar}${reOptMod})*`;
const rsSeq = rsOptVar + reOptMod + rsOptJoin;
const rsSymbol = `(?:${[`${rsNonAstral}${rsCombo}?`, rsCombo, rsRegional, rsSurrPair, rsAstral].join('|')})`;

const reUnicode = RegExp(`${rsFitz}(?=${rsFitz})|${rsSymbol}${rsSeq}`, 'g');

export function unicodeSize(str: string): number {
let result = 0;
reUnicode.lastIndex = 0;
regexUnicode.lastIndex = 0;

while (reUnicode.test(str)) {
while (regexUnicode.test(str)) {
++result;
}

Expand Down
11 changes: 11 additions & 0 deletions src/compat/_internal/unicodeToArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { regexUnicode } from './regexUnicode.ts';

/**
* Splits a Unicode `string` into an array of its visible symbols (grapheme clusters).
*
* Ported from lodash to preserve identical behavior.
* @link https://github.com/lodash/lodash/blob/4.17.21-es/_unicodeToArray.js
*/
export function unicodeToArray(str: string): string[] {
return str.match(regexUnicode) || [];
}
7 changes: 7 additions & 0 deletions src/compat/string/split.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ describe('split', () => {
const result = split('abc', 'b').join(',');
expect(result).toBe('a,c');
});

it('should split unicode strings into symbols when `separator` is an empty string', () => {
expect(split('Ame\u0301lie', '')).toEqual(['A', 'm', 'e\u0301', 'l', 'i', 'e']);
expect(split('aπŸ˜€b', '')).toEqual(['a', 'πŸ˜€', 'b']);
expect(split('πŸ‘¨β€πŸ‘©β€πŸ‘§πŸ‡°πŸ‡·', '')).toEqual(['πŸ‘¨β€πŸ‘©β€πŸ‘§', 'πŸ‡°πŸ‡·']);
expect(split('aπŸ˜€b', '', 2)).toEqual(['a', 'πŸ˜€']);
});
});
10 changes: 9 additions & 1 deletion src/compat/string/split.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { regexMultiByte } from '../_internal/regexMultiByte.ts';
import { unicodeToArray } from '../_internal/unicodeToArray.ts';
import { toString } from '../util/toString.ts';

/**
Expand Down Expand Up @@ -36,6 +38,12 @@
*/
export function split(string: string | null | undefined, index: string | number, guard: object): string[];

export function split(string: any, separator?: any, limit?: any): string[] {

Check warning on line 41 in src/compat/string/split.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 41 in src/compat/string/split.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 41 in src/compat/string/split.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return toString(string).split(separator as string, limit);
const str = toString(string);

if (separator === '' && regexMultiByte.test(str)) {
return unicodeToArray(str).slice(0, limit === undefined ? undefined : limit >>> 0);
}

return str.split(separator as string, limit);
}
Loading