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
9 changes: 1 addition & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,7 @@
"caseInsensitive": true
},
"groups": ["builtin", "external", "internal", "parent", ["sibling", "index"], "type"],
"newlines-between": "always",
"pathGroups": [
{
"pattern": "@{src,commands}/**",
"group": "internal",
"position": "after"
}
]
"newlines-between": "always"
}
],
"sort-imports": "off"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"lint-staged": "^17.0.8",
"prettier": "^3.9.5",
"rimraf": "^6.1.3",
"semantic-release": "^25.0.5",
"semantic-release": "^25.0.6",
"typedoc": "^0.28.20",
"validate-package-exports": "^1.2.1",
"vitest": "^4.1.10"
Expand Down
50 changes: 25 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/codePointLength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Get the number of Unicode code points in `input`, unlike `string.length` which counts UTF-16 code units.
*
* @example
* ```ts
* codePointLength('hello'); // 5
* codePointLength('😀'); // 1
* '😀'.length; // 2
* ```
*/
export function codePointLength(input: string): number {
return [...input].length;
}
22 changes: 22 additions & 0 deletions src/graphemeLength.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const segmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });

/**
* Get the number of grapheme clusters (user-perceived characters) in `input`, unlike `string.length` which counts UTF-16 code units.
*
* @example
* ```ts
* graphemeLength('hello'); // 5
* graphemeLength('👨‍👩‍👧‍👦'); // 1
* '👨‍👩‍👧‍👦'.length; // 11
* ```
*/
export function graphemeLength(input: string): number {
let count = 0;

// eslint-disable-next-line @typescript-eslint/naming-convention,@typescript-eslint/no-unused-vars
for (const _ of segmenter.segment(input)) {
count++;
}

return count;
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export * from './capitalize.js';
export * from './cartesian.js';
export * from './chunk.js';
export * from './clamp.js';
export * from './codePointLength.js';
export * from './combinations.js';
export * from './comment.js';
export * from './compose.js';
Expand Down Expand Up @@ -41,6 +42,7 @@ export * from './getPaths.js';
export * from './getRandomItem.js';
export * from './getSortedObject.js';
export * from './getType.js';
export * from './graphemeLength.js';
export * from './has.js';
export * from './hasAdditionalProperties.js';
export * from './inRange.js';
Expand Down
23 changes: 23 additions & 0 deletions test/codePointLength.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from 'vitest';

import { codePointLength } from '../src/codePointLength.js';

describe('codePointLength()', () => {
it('Empty input returns 0', () => {
expect(codePointLength('')).toBe(0);
});

it('Counts ASCII characters', () => {
expect(codePointLength('hello')).toBe(5);
});

it('Counts a single character made of a surrogate pair as 1', () => {
expect(codePointLength('😀')).toBe(1);
expect('😀'.length).toBe(2);
});

it('Counts a mix of ASCII and surrogate pair characters', () => {
expect(codePointLength('a😀b😀c')).toBe(5);
expect('a😀b😀c'.length).toBe(7);
});
});
32 changes: 32 additions & 0 deletions test/graphemeLength.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest';

import { graphemeLength } from '../src/graphemeLength.js';

describe('graphemeLength()', () => {
it('Empty input returns 0', () => {
expect(graphemeLength('')).toBe(0);
});

it('Counts ASCII characters', () => {
expect(graphemeLength('hello')).toBe(5);
});

it('Counts a single character made of a surrogate pair as 1', () => {
expect(graphemeLength('😀')).toBe(1);
expect('😀'.length).toBe(2);
});

it('Counts a ZWJ emoji sequence as a single grapheme', () => {
expect(graphemeLength('👨‍👩‍👧‍👦')).toBe(1);
expect('👨‍👩‍👧‍👦'.length).toBe(11);
});

it('Counts a combining character sequence as a single grapheme', () => {
expect(graphemeLength('é')).toBe(1);
expect('é'.length).toBe(2);
});

it('Counts a mix of ASCII and multi-code-unit graphemes', () => {
expect(graphemeLength('a😀b👨‍👩‍👧‍👦c')).toBe(5);
});
});