mb-trimwidth 0.0.4 โ†’ 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,13 +1,26 @@
1
1
  # mbTrimWidth
2
2
 
3
3
  [![NPM version](https://badgen.net/npm/v/mb-trimwidth)](https://www.npmjs.com/package/mb-trimwidth)
4
- [![Build Status](https://travis-ci.com/KiKiKi-KiKi/mbTrimWidth.svg?branch=main)](https://travis-ci.com/github/KiKiKi-KiKi/mbTrimWidth/)
5
- [![Coverage Status](https://coveralls.io/repos/github/KiKiKi-KiKi/mbTrimWidth/badge.svg?branch=main)](https://coveralls.io/github/KiKiKi-KiKi/mbTrimWidth?branch=main)
4
+ [![CI](https://github.com/KiKiKi-KiKi/mbTrimWidth/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/KiKiKi-KiKi/mbTrimWidth/actions/workflows/ci.yml)
6
5
  [![ISC License](http://img.shields.io/badge/license-ISC-green.svg?style=flat)](https://github.com/KiKiKi-KiKi/mbTrimWidth/blob/master/package.json)
7
6
 
8
7
  A module of trim multibyte string by character width.
9
8
  This will work even if contained Unicode, emoji, and surrogate pairs.
10
9
 
10
+ ## Requirements
11
+
12
+ This package uses [`Intl.Segmenter`](https://developer.mozilla.org/en-US/docs/Web/API/Intl/Segmenter) to correctly count grapheme clusters, including ZWJ emoji sequences (e.g. `๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ`) and combining characters (e.g. `pอช`).
13
+
14
+ | Environment | Minimum version |
15
+ |---|---|
16
+ | Node.js | 16+ |
17
+ | Chrome | 87+ |
18
+ | Firefox | 125+ |
19
+ | Safari | 14.1+ |
20
+
21
+ > [!NOTE]
22
+ > `Intl.Segmenter` is **not** available in older environments. If you need to support them, a polyfill such as [`@formatjs/intl-segmenter`](https://formatjs.io/docs/polyfills/intl-segmenter/) is required.
23
+
11
24
  ## install
12
25
 
13
26
  ```sh
package/dist-cjs/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mbStringWidth = exports.mbTrimWidth = void 0;
4
- var mbTrimWidth_1 = require("./mbTrimWidth");
4
+ const mbTrimWidth_1 = require("./mbTrimWidth");
5
5
  Object.defineProperty(exports, "mbTrimWidth", { enumerable: true, get: function () { return mbTrimWidth_1.mbTrimWidth; } });
6
- var mbStringWidth_1 = require("./mbStringWidth");
6
+ const mbStringWidth_1 = require("./mbStringWidth");
7
7
  Object.defineProperty(exports, "mbStringWidth", { enumerable: true, get: function () { return mbStringWidth_1.mbStringWidth; } });
8
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,6CAA4C;AAGnC,4FAHA,yBAAW,OAGA;AAFpB,iDAAgD;AAGvC,8FAHA,6BAAa,OAGA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAGnC,4FAHA,yBAAW,OAGA;AAFpB,mDAAgD;AAGvC,8FAHA,6BAAa,OAGA"}
@@ -1,3 +1,3 @@
1
- declare type MbStringWidth = (str: string) => number;
1
+ type MbStringWidth = (str: string) => number;
2
2
  export declare const mbStringWidth: MbStringWidth;
3
3
  export {};
@@ -1,27 +1,9 @@
1
1
  "use strict";
2
- var __read = (this && this.__read) || function (o, n) {
3
- var m = typeof Symbol === "function" && o[Symbol.iterator];
4
- if (!m) return o;
5
- var i = m.call(o), r, ar = [], e;
6
- try {
7
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
- }
9
- catch (error) { e = { error: error }; }
10
- finally {
11
- try {
12
- if (r && !r.done && (m = i["return"])) m.call(i);
13
- }
14
- finally { if (e) throw e.error; }
15
- }
16
- return ar;
17
- };
18
- var __spreadArray = (this && this.__spreadArray) || function (to, from) {
19
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
20
- to[j] = from[i];
21
- return to;
22
- };
23
2
  Object.defineProperty(exports, "__esModule", { value: true });
24
3
  exports.mbStringWidth = void 0;
25
- var mbStringWidth = function (str) { return __spreadArray([], __read(str)).length; };
4
+ const segmenter = new Intl.Segmenter();
5
+ const mbStringWidth = (str) => {
6
+ return [...segmenter.segment(str)].length;
7
+ };
26
8
  exports.mbStringWidth = mbStringWidth;
27
9
  //# sourceMappingURL=mbStringWidth.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mbStringWidth.js","sourceRoot":"","sources":["../src/mbStringWidth.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAEO,IAAM,aAAa,GAAkB,UAAC,GAAG,IAAK,OAAA,yBAAI,GAAG,GAAE,MAAM,EAAf,CAAe,CAAC;AAAxD,QAAA,aAAa,iBAA2C"}
1
+ {"version":3,"file":"mbStringWidth.js","sourceRoot":"","sources":["../src/mbStringWidth.ts"],"names":[],"mappings":";;;AAAA,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;AAIhC,MAAM,aAAa,GAAkB,CAAC,GAAG,EAAE,EAAE;IAClD,OAAO,CAAC,GAAG,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AAC5C,CAAC,CAAC;AAFW,QAAA,aAAa,iBAExB"}
@@ -1,3 +1,3 @@
1
- declare type MbTrimWidth = (str: string, max: number, ellipsis?: string) => string;
1
+ type MbTrimWidth = (str: string, max: number, ellipsis?: string) => string;
2
2
  export declare const mbTrimWidth: MbTrimWidth;
3
3
  export {};
@@ -1,30 +1,13 @@
1
1
  "use strict";
2
- var __read = (this && this.__read) || function (o, n) {
3
- var m = typeof Symbol === "function" && o[Symbol.iterator];
4
- if (!m) return o;
5
- var i = m.call(o), r, ar = [], e;
6
- try {
7
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
8
- }
9
- catch (error) { e = { error: error }; }
10
- finally {
11
- try {
12
- if (r && !r.done && (m = i["return"])) m.call(i);
13
- }
14
- finally { if (e) throw e.error; }
15
- }
16
- return ar;
17
- };
18
- var __spreadArray = (this && this.__spreadArray) || function (to, from) {
19
- for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
20
- to[j] = from[i];
21
- return to;
22
- };
23
2
  Object.defineProperty(exports, "__esModule", { value: true });
24
3
  exports.mbTrimWidth = void 0;
25
- var mbTrimWidth = function (str, max, ellipsis) {
26
- var strArray = __spreadArray([], __read(str));
27
- var maxLen = ellipsis ? max - __spreadArray([], __read(ellipsis)).length : max;
4
+ const segmenter = new Intl.Segmenter();
5
+ const mbTrimWidth = (str, max, ellipsis) => {
6
+ const strArray = [...segmenter.segment(str)].map((s) => s.segment);
7
+ const ellipsisArray = ellipsis
8
+ ? [...segmenter.segment(ellipsis)].map((s) => s.segment)
9
+ : [];
10
+ const maxLen = ellipsis ? max - ellipsisArray.length : max;
28
11
  if (strArray.length <= max) {
29
12
  return str;
30
13
  }
@@ -32,9 +15,9 @@ var mbTrimWidth = function (str, max, ellipsis) {
32
15
  return '';
33
16
  }
34
17
  if (maxLen <= 0 && ellipsis) {
35
- return __spreadArray(__spreadArray([], __read(strArray)), __read(ellipsis)).slice(0, max).join('');
18
+ return [...strArray, ...ellipsisArray].slice(0, max).join('');
36
19
  }
37
- return __spreadArray(__spreadArray([], __read(strArray.slice(0, maxLen))), [ellipsis]).join('');
20
+ return [...strArray.slice(0, maxLen), ellipsis].join('');
38
21
  };
39
22
  exports.mbTrimWidth = mbTrimWidth;
40
23
  //# sourceMappingURL=mbTrimWidth.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mbTrimWidth.js","sourceRoot":"","sources":["../src/mbTrimWidth.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAEO,IAAM,WAAW,GAAgB,UAAC,GAAG,EAAE,GAAG,EAAE,QAAQ;IACzD,IAAM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AC,EAAE,CAAC;IACP,MAAM,MAAM,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC;IAE3D,IAAI,QAAQ,CAAC,MAAM,IAAI,GAAG,EAAE,CAAC;QAC3B,OAAO,GAAG,CAAC;IACb,CAAC;IAED,IAAI,MAAM,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;QAC9B,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,IAAI,MAAM,IAAI,CAAC,IAAI,QAAQ,EAAE,CAAC;QAC5B,OAAO,CAAC,GAAG,QAAQ,EAAE,GAAG,aAAa,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,CAAC,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,EAAE,QAAQ,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC3D,CAAC,CAAC;AApBW,QAAA,WAAW,eAoBtB"}
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -1,5 +1,4 @@
1
1
  import { mbTrimWidth } from './mbTrimWidth';
2
2
  import { mbStringWidth } from './mbStringWidth';
3
-
4
3
  export { mbTrimWidth };
5
4
  export { mbStringWidth };
@@ -0,0 +1,5 @@
1
+ import { mbTrimWidth } from './mbTrimWidth';
2
+ import { mbStringWidth } from './mbStringWidth';
3
+ export { mbTrimWidth };
4
+ export { mbStringWidth };
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,OAAO,EAAE,WAAW,EAAE,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ type MbStringWidth = (str: string) => number;
2
+ export declare const mbStringWidth: MbStringWidth;
3
+ export {};
@@ -0,0 +1,5 @@
1
+ const segmenter = new Intl.Segmenter();
2
+ export const mbStringWidth = (str) => {
3
+ return [...segmenter.segment(str)].length;
4
+ };
5
+ //# sourceMappingURL=mbStringWidth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mbStringWidth.js","sourceRoot":"","sources":["../src/mbStringWidth.ts"],"names":[],"mappings":"AAAA,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;AAIvC,MAAM,CAAC,MAAM,aAAa,GAAkB,CAAC,GAAG,EAAE,EAAE;IAClD,OAAO,CAAC,GAAG,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AAC5C,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ type MbTrimWidth = (str: string, max: number, ellipsis?: string) => string;
2
+ export declare const mbTrimWidth: MbTrimWidth;
3
+ export {};
@@ -0,0 +1,19 @@
1
+ const segmenter = new Intl.Segmenter();
2
+ export const mbTrimWidth = (str, max, ellipsis) => {
3
+ const strArray = [...segmenter.segment(str)].map((s) => s.segment);
4
+ const ellipsisArray = ellipsis
5
+ ? [...segmenter.segment(ellipsis)].map((s) => s.segment)
6
+ : [];
7
+ const maxLen = ellipsis ? max - ellipsisArray.length : max;
8
+ if (strArray.length <= max) {
9
+ return str;
10
+ }
11
+ if (maxLen === 0 && !ellipsis) {
12
+ return '';
13
+ }
14
+ if (maxLen <= 0 && ellipsis) {
15
+ return [...strArray, ...ellipsisArray].slice(0, max).join('');
16
+ }
17
+ return [...strArray.slice(0, maxLen), ellipsis].join('');
18
+ };
19
+ //# sourceMappingURL=mbTrimWidth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mbTrimWidth.js","sourceRoot":"","sources":["../src/mbTrimWidth.ts"],"names":[],"mappings":"A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  {
2
2
  "name": "mb-trimwidth",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "A module of trim multibyte string by character width. This will work even if contained Unicode, emoji, and surrogate pairs.",
5
5
  "main": "dist-cjs/index.js",
6
6
  "module": "dist-esm/index.js",
7
7
  "types": "dist-cjs/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist-esm/index.d.ts",
12
+ "default": "./dist-esm/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./dist-cjs/index.d.ts",
16
+ "default": "./dist-cjs/index.js"
17
+ }
18
+ }
19
+ },
20
+ "files": [
21
+ "dist-cjs",
22
+ "dist-esm"
23
+ ],
8
24
  "repository": {
9
25
  "type": "git",
10
26
  "url": "git+https://github.com/KiKiKi-KiKi/mbTrimWidth.git"
@@ -26,8 +42,8 @@
26
42
  },
27
43
  "homepage": "https://github.com/KiKiKi-KiKi/mbTrimWidth#readme",
28
44
  "scripts": {
29
- "build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir ./dist-cjs",
30
- "build:esm": "tsc --project tsconfig.build.json --module esNext --outDir ./dist-esm",
45
+ "build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir ./dist-cjs && echo '{\"type\":\"commonjs\"}' > dist-cjs/package.json",
46
+ "build:esm": "tsc --project tsconfig.build.json --module esNext --outDir ./dist-esm && echo '{\"type\":\"module\"}' > dist-esm/package.json",
31
47
  "build": "npm run build:cjs && npm run build:esm",
32
48
  "test": "jest --runInBand",
33
49
  "test:w": "jest --runInBand --watch",
@@ -38,18 +54,17 @@
38
54
  "prepublishOnly": "npm run build"
39
55
  },
40
56
  "devDependencies": {
41
- "@types/jest": "^26.0.24",
42
- "@typescript-eslint/eslint-plugin": "^4.28.2",
43
- "@typescript-eslint/parser": "^4.28.2",
44
- "coveralls": "^3.1.1",
45
- "eslint": "^7.30.0",
46
- "eslint-config-airbnb-base": "^14.2.1",
47
- "eslint-config-prettier": "^8.3.0",
48
- "eslint-plugin-import": "^2.23.4",
49
- "jest": "^27.0.6",
50
- "prettier": "^2.3.2",
51
- "ts-jest": "^27.0.3",
52
- "ts-node": "^10.0.0",
53
- "typescript": "^4.3.5"
57
+ "@eslint/js": "^9.0.0",
58
+ "@types/jest": "^30.0.0",
59
+ "@typescript-eslint/eslint-plugin": "^8.57.1",
60
+ "@typescript-eslint/parser": "^8.57.1",
61
+ "eslint": "^10.1.0",
62
+ "eslint-config-prettier": "^10.1.8",
63
+ "globals": "^16.0.0",
64
+ "jest": "^30.3.0",
65
+ "prettier": "^3.8.1",
66
+ "ts-jest": "^29.4.6",
67
+ "ts-node": "^10.9.2",
68
+ "typescript": "^5.9.3"
54
69
  }
55
70
  }
package/.eslintignore DELETED
@@ -1 +0,0 @@
1
- /.eslintrc.js
package/.eslintrc.js DELETED
@@ -1,45 +0,0 @@
1
- module.exports = {
2
- env: {
3
- browser: true,
4
- es2021: true,
5
- node: true,
6
- },
7
- extends: [
8
- 'airbnb-base',
9
- 'plugin:@typescript-eslint/recommended',
10
- 'plugin:@typescript-eslint/recommended-requiring-type-checking',
11
- 'prettier',
12
- ],
13
- plugins: ['@typescript-eslint'],
14
- parser: '@typescript-eslint/parser',
15
- parserOptions: {
16
- project: './tsconfig.json',
17
- },
18
- settings: {
19
- 'import/extensions': ['.js', '.jsx', '.ts', '.tsx'],
20
- 'import/parsers': {
21
- '@typescript-eslint/parser': ['.ts', '.tsx'],
22
- },
23
- 'import/resolver': {
24
- node: {
25
- extensions: ['.js', '.jsx', '.ts', '.tsx'],
26
- },
27
- },
28
- },
29
- rules: {
30
- 'import/extensions': [
31
- 'error',
32
- 'ignorePackages',
33
- {
34
- js: 'never',
35
- jsx: 'never',
36
- ts: 'never',
37
- tsx: 'never',
38
- },
39
- ],
40
- 'import/prefer-default-export': 'off',
41
- 'newline-before-return': 'error',
42
- 'no-console': 'warn',
43
- 'no-var': 'error',
44
- },
45
- };
package/.prettierrc DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "printWidth": 120,
3
- "tabWidth": 2,
4
- "semi": true,
5
- "singleQuote": true,
6
- "trailingComma": "es5",
7
- "arrowParens": "always",
8
- "useTabs": false,
9
- "endOfLine": "lf"
10
- }
package/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - 'stable'
4
- - 'lts/*'
5
- script: 'npm run test:coverage && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls'
package/jest.config.js DELETED
@@ -1,182 +0,0 @@
1
- /*
2
- * For a detailed explanation regarding each configuration property, visit:
3
- * https://jestjs.io/docs/configuration
4
- */
5
-
6
- module.exports = {
7
- // All imported modules in your tests should be mocked automatically
8
- // automock: false,
9
-
10
- // Stop running tests after `n` failures
11
- // bail: 0,
12
-
13
- // The directory where Jest should store its cached dependency information
14
- // cacheDirectory: "/private/var/folders/v2/jqr7_2zd4_b5p498blctpf480000gn/T/jest_dx",
15
-
16
- // Automatically clear mock calls and instances between every test
17
- clearMocks: true,
18
-
19
- // Indicates whether the coverage information should be collected while executing the test
20
- // collectCoverage: false,
21
-
22
- // An array of glob patterns indicating a set of files for which coverage information should be collected
23
- // collectCoverageFrom: undefined,
24
-
25
- // The directory where Jest should output its coverage files
26
- coverageDirectory: 'coverage',
27
-
28
- // An array of regexp pattern strings used to skip coverage collection
29
- // coveragePathIgnorePatterns: [
30
- // "/node_modules/"
31
- // ],
32
-
33
- // Indicates which provider should be used to instrument code for coverage
34
- coverageProvider: 'v8',
35
-
36
- // A list of reporter names that Jest uses when writing coverage reports
37
- // coverageReporters: [
38
- // "json",
39
- // "text",
40
- // "lcov",
41
- // "clover"
42
- // ],
43
-
44
- // An object that configures minimum threshold enforcement for coverage results
45
- // coverageThreshold: undefined,
46
-
47
- // A path to a custom dependency extractor
48
- // dependencyExtractor: undefined,
49
-
50
- // Make calling deprecated APIs throw helpful error messages
51
- // errorOnDeprecated: false,
52
-
53
- // Force coverage collection from ignored files using an array of glob patterns
54
- // forceCoverageMatch: [],
55
-
56
- // A path to a module which exports an async function that is triggered once before all test suites
57
- // globalSetup: undefined,
58
-
59
- // A path to a module which exports an async function that is triggered once after all test suites
60
- // globalTeardown: undefined,
61
-
62
- // A set of global variables that need to be available in all test environments
63
- // globals: {},
64
-
65
- // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
66
- // maxWorkers: "50%",
67
-
68
- // An array of directory names to be searched recursively up from the requiring module's location
69
- // moduleDirectories: [
70
- // "node_modules"
71
- // ],
72
-
73
- // An array of file extensions your modules use
74
- moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json', 'node'],
75
-
76
- // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
77
- // moduleNameMapper: {},
78
-
79
- // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
80
- // modulePathIgnorePatterns: [],
81
-
82
- // Activates notifications for test results
83
- // notify: false,
84
-
85
- // An enum that specifies notification mode. Requires { notify: true }
86
- // notifyMode: "failure-change",
87
-
88
- // A preset that is used as a base for Jest's configuration
89
- preset: 'ts-jest',
90
-
91
- // Run tests from one or more projects
92
- // projects: undefined,
93
-
94
- // Use this configuration option to add custom reporters to Jest
95
- // reporters: undefined,
96
-
97
- // Automatically reset mock state between every test
98
- // resetMocks: false,
99
-
100
- // Reset the module registry before running each individual test
101
- // resetModules: false,
102
-
103
- // A path to a custom resolver
104
- // resolver: undefined,
105
-
106
- // Automatically restore mock state between every test
107
- // restoreMocks: false,
108
-
109
- // The root directory that Jest should scan for tests and modules within
110
- // rootDir: undefined,
111
-
112
- // A list of paths to directories that Jest should use to search for files in
113
- roots: ['<rootDir>/tests/'],
114
-
115
- // Allows you to use a custom runner instead of Jest's default test runner
116
- // runner: "jest-runner",
117
-
118
- // The paths to modules that run some code to configure or set up the testing environment before each test
119
- // setupFiles: [],
120
-
121
- // A list of paths to modules that run some code to configure or set up the testing framework before each test
122
- // setupFilesAfterEnv: [],
123
-
124
- // The number of seconds after which a test is considered as slow and reported as such in the results.
125
- // slowTestThreshold: 5,
126
-
127
- // A list of paths to snapshot serializer modules Jest should use for snapshot testing
128
- // snapshotSerializers: [],
129
-
130
- // The test environment that will be used for testing
131
- testEnvironment: 'node',
132
-
133
- // Options that will be passed to the testEnvironment
134
- // testEnvironmentOptions: {},
135
-
136
- // Adds a location field to test results
137
- // testLocationInResults: false,
138
-
139
- // The glob patterns Jest uses to detect test files
140
- // testMatch: [
141
- // "**/__tests__/**/*.[jt]s?(x)",
142
- // "**/?(*.)+(spec|test).[tj]s?(x)"
143
- // ],
144
-
145
- // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
146
- // testPathIgnorePatterns: [
147
- // "/node_modules/"
148
- // ],
149
-
150
- // The regexp pattern or array of patterns that Jest uses to detect test files
151
- // testRegex: [],
152
-
153
- // This option allows the use of a custom results processor
154
- // testResultsProcessor: undefined,
155
-
156
- // This option allows use of a custom test runner
157
- // testRunner: "jest-circus/runner",
158
-
159
- // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
160
- // testURL: "http://localhost",
161
-
162
- // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
163
- // timers: "real",
164
-
165
- // A map from regular expressions to paths to transformers
166
- // transform: { '^.+.(js|jsx|ts|tsx)$': 'ts-jest' },
167
-
168
- // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
169
- transformIgnorePatterns: ['/node_modules/', '\\.pnp\\.[^\\/]+$'],
170
-
171
- // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
172
- // unmockedModulePathPatterns: undefined,
173
-
174
- // Indicates whether each individual test should be reported during the run
175
- // verbose: undefined,
176
-
177
- // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
178
- // watchPathIgnorePatterns: [],
179
-
180
- // Whether to use watchman for file crawling
181
- // watchman: true,
182
- };
@@ -1,3 +0,0 @@
1
- type MbStringWidth = (str: string) => number;
2
-
3
- export const mbStringWidth: MbStringWidth = (str) => [...str].length;
@@ -1,20 +0,0 @@
1
- type MbTrimWidth = (str: string, max: number, ellipsis?: string) => string;
2
-
3
- export const mbTrimWidth: MbTrimWidth = (str, max, ellipsis) => {
4
- const strArray = [...str] as string[];
5
- const maxLen = ellipsis ? max - [...ellipsis].length : max;
6
-
7
- if (strArray.length <= max) {
8
- return str;
9
- }
10
-
11
- if (maxLen === 0 && !ellipsis) {
12
- return '';
13
- }
14
-
15
- if (maxLen <= 0 && ellipsis) {
16
- return [...strArray, ...ellipsis].slice(0, max).join('');
17
- }
18
-
19
- return [...strArray.slice(0, maxLen), ellipsis].join('');
20
- };
@@ -1,134 +0,0 @@
1
- import { mbTrimWidth, mbStringWidth } from '../src/index';
2
-
3
- describe('mbTrimWidth', () => {
4
- describe('English text', () => {
5
- const str = 'Lorem ipsum dolor sit amet';
6
-
7
- test('Trim text', () => {
8
- const trimText = mbTrimWidth(str, 10);
9
- expect(trimText).toBe('Lorem ipsu');
10
- expect(trimText.length).toBe(10);
11
- });
12
-
13
- test('Trim text and add ellipsis', () => {
14
- const trimText = mbTrimWidth(str, 10, 'โ€ฆ');
15
- expect(trimText).toBe('Lorem ipsโ€ฆ');
16
- expect(trimText.length).toBe(10);
17
- });
18
-
19
- test('When trim width > text length return origin text', () => {
20
- const trimText = mbTrimWidth(str, 100);
21
- expect(trimText).toBe(str);
22
- });
23
-
24
- test('When trim width 0 return ""', () => {
25
- const trimText = mbTrimWidth(str, 0);
26
- expect(trimText).toBe('');
27
- expect(trimText.length).toBe(0);
28
-
29
- const trimText2 = mbTrimWidth(str, 1, 'โ€ฆ');
30
- expect(trimText2).toBe('L');
31
- expect(trimText2.length).toBe(1);
32
- });
33
-
34
- test('When trim width < ellipsis width, ignore ellipsis', () => {
35
- const trimText = mbTrimWidth(str, 1, '...');
36
- expect(trimText).toBe('L');
37
- expect(trimText.length).toBe(1);
38
- });
39
- });
40
-
41
- describe('Unicode text', () => {
42
- const str = 'ๅญใ‚‚ใ‹ใฃใ“ใ†ใฎใ€ŒใŠใ˜ใŽ็Œซๅ›ฃใ€ใ‚’็ช“ใŒใตใฟๆˆธๆฃšใพใ—ใงใ™ใ€‚';
43
-
44
- test('String not contain emoji & Surrogate pairs', () => {
45
- expect(str.length).toBe(26);
46
- });
47
-
48
- test('Trim text', () => {
49
- const trimText = mbTrimWidth(str, 12);
50
- expect(trimText).toBe('ๅญใ‚‚ใ‹ใฃใ“ใ†ใฎใ€ŒใŠใ˜ใŽ็Œซ');
51
- expect(trimText.length).toBe(12);
52
- });
53
-
54
- test('Trim text and add ellipsis', () => {
55
- const trimText = mbTrimWidth(str, 12, 'โ€ฆ');
56
- expect(trimText).toBe('ๅญใ‚‚ใ‹ใฃใ“ใ†ใฎใ€ŒใŠใ˜ใŽโ€ฆ');
57
- expect(trimText.length).toBe(12);
58
- });
59
-
60
- test('When trim width > text length return origin text', () => {
61
- const trimText = mbTrimWidth(str, 100);
62
- expect(trimText).toBe(str);
63
- });
64
- });
65
-
66
- describe('Contains emoji text', () => {
67
- const str = 'I love ๐Ÿ„๐ŸŒŠ and ๐Ÿ•';
68
-
69
- test('Check text length', () => {
70
- console.log(str, 'length:', str.length);
71
-
72
- expect(str.length).toBe(18);
73
- expect(mbStringWidth(str)).toBe(15);
74
- });
75
-
76
- test('Trim text', () => {
77
- const trimText = mbTrimWidth(str, 9);
78
- expect(trimText).toBe('I love ๐Ÿ„๐ŸŒŠ');
79
- expect(mbStringWidth(trimText)).toBe(9);
80
- });
81
-
82
- test('Trim text and add ellipsis', () => {
83
- const trimText = mbTrimWidth(str, 9, 'โ€ฆ');
84
- expect(trimText).toBe('I love ๐Ÿ„โ€ฆ');
85
- expect(mbStringWidth(trimText)).toBe(9);
86
- });
87
- });
88
-
89
- describe('Contains emoji text (JP)', () => {
90
- const str = '็งใฏ๐Ÿ„๐ŸŒŠใจ๐Ÿ•ใ‚’ใ—ใพใ™';
91
-
92
- test('Check text length', () => {
93
- console.log(str, 'length:', str.length);
94
-
95
- expect(str.length).toBe(13);
96
- expect(mbStringWidth(str)).toBe(10);
97
- });
98
-
99
- test('Trim text', () => {
100
- const trimText = mbTrimWidth(str, 6);
101
- expect(trimText).toBe('็งใฏ๐Ÿ„๐ŸŒŠใจ๐Ÿ•');
102
- expect(mbStringWidth(trimText)).toBe(6);
103
- });
104
-
105
- test('Trim text and add ellipsis', () => {
106
- const trimText = mbTrimWidth(str, 6, 'โ€ฆ');
107
- expect(trimText).toBe('็งใฏ๐Ÿ„๐ŸŒŠใจโ€ฆ');
108
- expect(mbStringWidth(trimText)).toBe(6);
109
- });
110
- });
111
-
112
- describe('Contains emoji & surrogate pairs text (JP)', () => {
113
- const str = '๐ŸŒ•ใฎๅคœใฏ๐ ฎท้‡Žๅฑ‹ใง๐ฉธฝใ‚’้ฃŸในใŸใ„๐Ÿ˜‡';
114
-
115
- test('Check text length', () => {
116
- console.log(str, 'length:', str.length);
117
-
118
- expect(str.length).toBe(19);
119
- expect(mbStringWidth(str)).toBe(15);
120
- });
121
-
122
- test('Trim text', () => {
123
- const trimText = mbTrimWidth(str, 9);
124
- expect(trimText).toBe('๐ŸŒ•ใฎๅคœใฏ๐ ฎท้‡Žๅฑ‹ใง๐ฉธฝ');
125
- expect(mbStringWidth(trimText)).toBe(9);
126
- });
127
-
128
- test('Trim text and add ellipsis', () => {
129
- const trimText = mbTrimWidth(str, 9, 'โ€ฆ');
130
- expect(trimText).toBe('๐ŸŒ•ใฎๅคœใฏ๐ ฎท้‡Žๅฑ‹ใงโ€ฆ');
131
- expect(mbStringWidth(trimText)).toBe(9);
132
- });
133
- });
134
- });
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "exclude": ["tests/**/*", "**/?(*.)+(spec|test).[tj]s?(x)"]
4
- }
package/tsconfig.json DELETED
@@ -1,74 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
-
5
- /* Basic Options */
6
- // "incremental": true, /* Enable incremental compilation */
7
- "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
8
- "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
9
- "lib": ["dom", "dom.iterable", "ES2017"] /* Specify library files to be included in the compilation. */,
10
- "allowJs": true /* Allow javascript files to be compiled. */,
11
- "checkJs": true /* Report errors in .js files. */,
12
- // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
13
- "declaration": true /* Generates corresponding '.d.ts' file. */,
14
- // "declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
15
- "sourceMap": true /* Generates corresponding '.map' file. */,
16
- // "outFile": "./", /* Concatenate and emit output to single file. */
17
- // "outDir": "./", /* Redirect output structure to the directory. */
18
- // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19
- // "composite": true, /* Enable project compilation */
20
- // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
21
- // "removeComments": true, /* Do not emit comments to output. */
22
- // "noEmit": true, /* Do not emit outputs. */
23
- // "importHelpers": true, /* Import emit helpers from 'tslib'. */
24
- "downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,
25
- // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26
-
27
- /* Strict Type-Checking Options */
28
- "strict": true /* Enable all strict type-checking options. */,
29
- // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
30
- // "strictNullChecks": true, /* Enable strict null checks. */
31
- // "strictFunctionTypes": true, /* Enable strict checking of function types. */
32
- // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
33
- // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
34
- "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
35
- // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36
-
37
- /* Additional Checks */
38
- "noUnusedLocals": true /* Report errors on unused locals. */,
39
- "noUnusedParameters": true /* Report errors on unused parameters. */,
40
- "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
41
- // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
42
- // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
43
- // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
44
- // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
45
-
46
- /* Module Resolution Options */
47
- // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
48
- // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
49
- // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
50
- // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
51
- // "typeRoots": [], /* List of folders to include type definitions from. */
52
- // "types": [], /* Type declaration files to be included in compilation. */
53
- // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
54
- "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
55
- // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
56
- // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
57
-
58
- /* Source Map Options */
59
- // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
60
- // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
61
- // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
62
- // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
63
-
64
- /* Experimental Options */
65
- "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
66
- "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
67
-
68
- /* Advanced Options */
69
- "skipLibCheck": true /* Skip type checking of declaration files. */,
70
- "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
71
- },
72
- "include": ["src/**/*", "tests/**/*"],
73
- "exclude": ["node_modules"]
74
- }