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
4 changes: 0 additions & 4 deletions .ncurc

This file was deleted.

6 changes: 6 additions & 0 deletions .ncurc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {localCooldown} from '@cto.af/ncurc';

export default {
dep: ['prod', 'dev', 'packageManager'],
cooldown: await localCooldown(),
};
33 changes: 20 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import ansi from 'ansi-regex';
import emojiRegex from 'emoji-regex';

const ASCII_REGEX = /^[\x20-\x7e]*$/;
const DEFAULT_LOCALE = new Intl.Segmenter().resolvedOptions().locale;
const DEFAULT_LOCALE = new Intl.Locale(
new Intl.Segmenter().resolvedOptions().locale
).maximize();
const EMOJI_REGEX = emojiRegex();
const ANSI = ansi();
const ANSI_CAP = new RegExp(`(${ANSI.source})`, 'g');
Expand Down Expand Up @@ -62,12 +64,12 @@ const NO_EXTRAS = {
*/

/**
* @typedef {object} StringWidthOptions
* Options for creating a StringWidth instance.
* @typedef {object} StringWidthOptions Options for creating a StringWidth
* instance.
*
* @prop {string} [locale] The locale to use for grapheme segmentation and to
* determine if we are in a CJK context for code points that have ambiguous
* East Asian Width. Defaults to systems's locale.
* @prop {string|Intl.Locale} [locale] The locale to use for grapheme
* segmentation and to determine if we are in a CJK context for code points
* that have ambiguous East Asian Width. Defaults to systems's locale.
* @prop {ExtraWidths} [extraWidths] A lookup map for code points whose width
* you would like to override. Might be a Map<number, number>, UnicodeTrie,
* or anything else that has a `get(codePoint: number) => number` method.
Expand All @@ -90,10 +92,11 @@ const NO_EXTRAS = {
* than individual sequences (e.g. cursor movements) or overlapping pairs.
*/
export class StringWidth {
#graphemes;
#isCJK;
#extraWidths;
#graphemes;
#includeANSI;
#isCJK;
#locale;

/**
* Create a StringWidth instance.
Expand All @@ -111,18 +114,22 @@ export class StringWidth {
includeANSI: false,
...opts,
};
this.#graphemes = new Intl.Segmenter(options.locale, {

this.#locale = (typeof options.locale === 'string') ?
new Intl.Locale(options.locale).maximize() :
options.locale.maximize();

this.#graphemes = new Intl.Segmenter(this.#locale, {
granularity: 'grapheme',
});

// NOT options, but original opts
if (typeof opts?.isCJK === 'boolean') {
this.#isCJK = options.isCJK;
} else {
const loc = new Intl.Locale(options.locale).maximize();
// Script is never undefined after going through Segmenter
this.#isCJK = ['Hans', 'Hant', 'Kore', 'Jpan']
.includes(/** @type {string} */ (loc.script));
.includes(/** @type {string} */ (this.#locale.script));
}
this.#extraWidths = options.extraWidths;
this.#includeANSI = options.includeANSI;
Expand All @@ -139,10 +146,10 @@ export class StringWidth {

/**
* Resolved locale for grapheme segmentation.
* @returns {string}
* @returns {Intl.Locale}
*/
get locale() {
return this.#graphemes.resolvedOptions().locale;
return this.#locale;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,23 @@
"license": "MIT",
"dependencies": {
"@cto.af/unicode-trie-runtime": "3.2.11",
"ansi-regex": "~6.4.0",
"ansi-regex": "~6.5.0",
"emoji-regex": "~11.0.0"
},
"devDependencies": {
"@cto.af/eslint-config": "^7.0.3",
"@cto.af/eslint-config": "^7.0.4",
"@cto.af/ncurc": "^0.0.3",
"@cto.af/unicode-trie": "^3.2.11",
"@types/node": "^26.6.5",
"@types/node": "^26.6.6",
"@typescript/native": "npm:typescript@^7.0.2",
"eslint": "^10.13.0",
"eslint": "^10.14.0",
"eslint-plugin-mocha": "^12.0.2",
"mocha": "^12.0.3",
"superc8": "^12.7.0",
"typedoc": "^0.28.20",
"typescript": "npm:@typescript/typescript6@^6.0.2"
},
"packageManager": "pnpm@12.15.0",
"packageManager": "pnpm@12.18.0",
"engines": {
"node": ">= 22"
}
Expand Down
Loading
Loading