@guanghechen/string 1.0.1 → 2.0.2

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/CHANGELOG.md ADDED
@@ -0,0 +1,35 @@
1
+ # Change Log
2
+
3
+ ## 2.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - chore: vitest config auto-load aliases and coverage thresholds; style/doc formatting updates
8
+
9
+ All notable changes to this project will be documented in this file. See
10
+ [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
11
+
12
+ ## 2.0.1 (2025-02-07)
13
+
14
+ ### Improvements
15
+
16
+ - Enhance type safety and test coverage
17
+
18
+ ### Documentation
19
+
20
+ - Update README.md
21
+
22
+ ### Miscellaneous
23
+
24
+ - Add LICENSE file
25
+ - Migrate from lerna to changesets
26
+
27
+ ## 2.0.0 (2025-01-15)
28
+
29
+ ### Features
30
+
31
+ - Initial stable release: String utilities extracted from @guanghechen/std
32
+
33
+ ### Improvements
34
+
35
+ - Switch to recommended ESLint stack
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-present guanghechen (https://github.com/guanghechen)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <header>
2
2
  <h1 align="center">
3
- <a href="https://github.com/guanghechen/sora/tree/@guanghechen/string@1.0.1/packages/string#readme">@guanghechen/string</a>
3
+ <a href="https://github.com/guanghechen/sora/tree/@guanghechen/string@2.0.0/packages/string#readme">@guanghechen/string</a>
4
4
  </h1>
5
5
  <div align="center">
6
6
  <a href="https://www.npmjs.com/package/@guanghechen/string">
@@ -21,6 +21,12 @@
21
21
  src="https://img.shields.io/npm/l/@guanghechen/string.svg"
22
22
  />
23
23
  </a>
24
+ <a href="#install">
25
+ <img
26
+ alt="Module Formats: cjs, esm"
27
+ src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
28
+ />
29
+ </a>
24
30
  <a href="https://github.com/nodejs/node">
25
31
  <img
26
32
  alt="Node.js Version"
@@ -43,58 +49,114 @@
43
49
  </header>
44
50
  <br/>
45
51
 
46
- Utilities for processing strings or stringify other type data.
52
+ String utilities including case transformers, bytes parsers, and number/interval collectors.
47
53
 
48
54
  ## Install
49
55
 
50
- * npm
56
+ - npm
51
57
 
52
58
  ```bash
53
- npm install --save-dev @guanghechen/string
59
+ npm install --save @guanghechen/string
54
60
  ```
55
61
 
56
- * yarn
62
+ - yarn
57
63
 
58
64
  ```bash
59
- yarn add --dev @guanghechen/string
65
+ yarn add @guanghechen/string
60
66
  ```
61
67
 
62
68
  ## Usage
63
69
 
64
- * `transformer` utilities
65
-
66
- Name | Description
67
- :--------------------:|:---------------------------------------
68
- `toCamelCase` | `'test string' => 'testString'`
69
- `toCapitalCase` | `'test string' => 'Test String'`
70
- `toConstantCase` | `'test string' => 'TEST_STRING'`
71
- `toDotCase` | `'test string' => 'test.string'`
72
- `toKebabCase` | `'test string' => 'test-string'`
73
- `toLowerCase` | `'TEST STRING' => 'test string'`
74
- `toPascalCase` | `'test string' => 'TestString'`
75
- `toPathCase` | `'test string' => 'test/string'`
76
- `toSentenceCase` | `'testString' => 'Test string'`
77
- `toSnakeCase` | `'test string' => 'test_string'`
78
- `toTitleCase` | `'a simple test' => 'A Simple Test'`
79
- `toUpperCase` | `'test string' => 'TEST STRING'`
80
-
81
- - `composeTextTransformers`: Compose multiple ITextTransformer into one.
82
-
83
- ```typescript
84
- import {
85
- composeTextTransformers,
86
- toKebabCase,
87
- toTrim,
88
- } from '@guanghechen/string'
89
-
90
- // function composeTextTransformers (
91
- // ...transformers: ReadonlyArray<ITextTransformer>
92
- // ): ITextTransformer
93
-
94
- const transform = composeTextTransformers(toTrim, toKebabCase)
95
- const text: string = transform(' TeSt_StrinG ')
96
- // => 'test-string'
97
- ```
98
-
99
-
100
- [homepage]: https://github.com/guanghechen/sora/tree/@guanghechen/string@1.0.1/packages/string#readme
70
+ ### Case Transformers
71
+
72
+ ```typescript
73
+ import {
74
+ toCamelCase,
75
+ toKebabCase,
76
+ toSnakeCase,
77
+ toPascalCase,
78
+ toConstantCase,
79
+ toCapitalCase,
80
+ toSentenceCase,
81
+ toTitleCase,
82
+ toDotCase,
83
+ toPathCase,
84
+ composeTextTransformers,
85
+ toTrim,
86
+ } from '@guanghechen/string'
87
+
88
+ toCamelCase('test string') // 'testString'
89
+ toKebabCase('test string') // 'test-string'
90
+ toSnakeCase('test string') // 'test_string'
91
+ toPascalCase('test string') // 'TestString'
92
+ toConstantCase('test string') // 'TEST_STRING'
93
+ toCapitalCase('test string') // 'Test String'
94
+ toSentenceCase('testString') // 'Test string'
95
+ toTitleCase('a simple test') // 'A Simple Test'
96
+ toDotCase('test string') // 'test.string'
97
+ toPathCase('test string') // 'test/string'
98
+
99
+ // Compose multiple transformers
100
+ const transform = composeTextTransformers(toTrim, toKebabCase)
101
+ transform(' TeSt_StrinG ') // 'test-string'
102
+ ```
103
+
104
+ ### Bytes Parser
105
+
106
+ ```typescript
107
+ import { parseBytesString } from '@guanghechen/string'
108
+
109
+ parseBytesString('1K') // 1024
110
+ parseBytesString('1KB') // 1024
111
+ parseBytesString('1M') // 1048576
112
+ parseBytesString('1MB') // 1048576
113
+ parseBytesString('1.5G') // 1610612736
114
+ parseBytesString('100') // 100
115
+ parseBytesString('2T') // 2199023255552
116
+ ```
117
+
118
+ ### Number/Interval Collectors
119
+
120
+ Parse comma/space-separated number ranges:
121
+
122
+ ```typescript
123
+ import { collectNumbers, collectIntervals } from '@guanghechen/string'
124
+
125
+ // Collect individual numbers from ranges
126
+ collectNumbers('1-3') // [1, 2, 3]
127
+ collectNumbers('3,1-2,2,2') // [1, 2, 3]
128
+ collectNumbers('2-4,1-3,5-9') // [1, 2, 3, 4, 5, 6, 7, 8, 9]
129
+
130
+ // Collect merged intervals
131
+ collectIntervals('1-3') // [[1, 3]]
132
+ collectIntervals('3,7-5,2,2') // [[2, 3], [5, 7]]
133
+ collectIntervals('2-4,1-3,6-9') // [[1, 4], [6, 9]]
134
+
135
+ // Custom separator
136
+ collectNumbers('1;2;3-5', /;/) // [1, 2, 3, 4, 5]
137
+ ```
138
+
139
+ ### Transformer Reference
140
+
141
+ | Transformer | Input | Output |
142
+ | :---------------: | :-------------: | :-------------: |
143
+ | `toCamelCase` | 'test string' | 'testString' |
144
+ | `toCapitalCase` | 'test string' | 'Test String' |
145
+ | `toConstantCase` | 'test string' | 'TEST_STRING' |
146
+ | `toDotCase` | 'test string' | 'test.string' |
147
+ | `toKebabCase` | 'test string' | 'test-string' |
148
+ | `toLowerCase` | 'TEST STRING' | 'test string' |
149
+ | `toPascalCase` | 'test string' | 'TestString' |
150
+ | `toPathCase` | 'test string' | 'test/string' |
151
+ | `toSentenceCase` | 'testString' | 'Test string' |
152
+ | `toSnakeCase` | 'test string' | 'test_string' |
153
+ | `toTitleCase` | 'a simple test' | 'A Simple Test' |
154
+ | `toUpperCase` | 'test string' | 'TEST STRING' |
155
+ | `toTrim` | ' test ' | 'test' |
156
+
157
+ ## Reference
158
+
159
+ - [homepage][homepage]
160
+
161
+ [homepage]:
162
+ https://github.com/guanghechen/sora/tree/@guanghechen/string@2.0.0/packages/string#readme
package/lib/cjs/index.cjs CHANGED
@@ -1,5 +1,73 @@
1
1
  'use strict';
2
2
 
3
+ const bytesUnit = {
4
+ K: 1024,
5
+ M: 1024 ** 2,
6
+ G: 1024 ** 3,
7
+ T: 1024 ** 4,
8
+ P: 1024 ** 5,
9
+ };
10
+ const unitKeys = Object.keys(bytesUnit).join('');
11
+ const bytesRegex = new RegExp(`^\\s*(\\d+(?:\\.\\d+)?)([${unitKeys}]B?)?\\s*$`);
12
+ function parseBytesString(text) {
13
+ const match = bytesRegex.exec(text);
14
+ if (match) {
15
+ const [, num, unit] = match;
16
+ const bytes = Number(num) * (bytesUnit[unit] ?? 1);
17
+ return Math.max(0, Math.floor(bytes));
18
+ }
19
+ return 0;
20
+ }
21
+
22
+ const intervalRegex = /^(\d+)(?:-(\d+))?$/;
23
+ const defaultSeparator = /[,\s]+/;
24
+ function collectNumbers(text, separator = defaultSeparator) {
25
+ const intervals = collectIntervals(text, separator);
26
+ const result = [];
27
+ for (const interval of intervals) {
28
+ const [x, y] = interval;
29
+ for (let i = x; i <= y; ++i)
30
+ result.push(i);
31
+ }
32
+ return result;
33
+ }
34
+ function collectIntervals(text, separator = defaultSeparator) {
35
+ const intervals = [];
36
+ const tokens = text.split(new RegExp(separator, 'g'));
37
+ for (const token of tokens) {
38
+ const match = intervalRegex.exec(token);
39
+ if (!match)
40
+ continue;
41
+ const [, lft, rht] = match;
42
+ const x = Number(lft);
43
+ if (typeof rht !== 'string') {
44
+ const x = Number(lft);
45
+ intervals.push([x, x]);
46
+ continue;
47
+ }
48
+ const y = Number(rht);
49
+ intervals.push(x < y ? [x, y] : [y, x]);
50
+ }
51
+ intervals.sort((x, y) => {
52
+ const d = x[0] - y[0];
53
+ return d === 0 ? x[1] - y[1] : d;
54
+ });
55
+ if (intervals.length <= 1)
56
+ return intervals;
57
+ const result = [intervals[0]];
58
+ for (let i = 1; i < intervals.length; ++i) {
59
+ const interval = intervals[i];
60
+ const top = result[result.length - 1];
61
+ if (top[1] + 1 >= interval[0]) {
62
+ if (top[1] < interval[1])
63
+ top[1] = interval[1];
64
+ continue;
65
+ }
66
+ result.push(interval);
67
+ }
68
+ return result;
69
+ }
70
+
3
71
  const SPLIT_LOWER_UPPER_RE = /([\p{Ll}\d])(\p{Lu})/gu;
4
72
  const SPLIT_UPPER_UPPER_RE = /(\p{Lu})([\p{Lu}][\p{Ll}])/gu;
5
73
  const SPLIT_NUMBER_LOWER_RE = /(\d)(\p{Ll})/gu;
@@ -185,7 +253,10 @@ const toTitleCase = text => titleCase(text);
185
253
  const toTrim = text => text.trim();
186
254
  const toUpperCase = text => text.toLocaleUpperCase();
187
255
 
256
+ exports.collectIntervals = collectIntervals;
257
+ exports.collectNumbers = collectNumbers;
188
258
  exports.composeTextTransformers = composeTextTransformers;
259
+ exports.parseBytesString = parseBytesString;
189
260
  exports.toCamelCase = toCamelCase;
190
261
  exports.toCapitalCase = toCapitalCase;
191
262
  exports.toConstantCase = toConstantCase;
@@ -199,4 +270,3 @@ exports.toSnakeCase = toSnakeCase;
199
270
  exports.toTitleCase = toTitleCase;
200
271
  exports.toTrim = toTrim;
201
272
  exports.toUpperCase = toUpperCase;
202
- //# sourceMappingURL=index.cjs.map
package/lib/esm/index.mjs CHANGED
@@ -1,3 +1,71 @@
1
+ const bytesUnit = {
2
+ K: 1024,
3
+ M: 1024 ** 2,
4
+ G: 1024 ** 3,
5
+ T: 1024 ** 4,
6
+ P: 1024 ** 5,
7
+ };
8
+ const unitKeys = Object.keys(bytesUnit).join('');
9
+ const bytesRegex = new RegExp(`^\\s*(\\d+(?:\\.\\d+)?)([${unitKeys}]B?)?\\s*$`);
10
+ function parseBytesString(text) {
11
+ const match = bytesRegex.exec(text);
12
+ if (match) {
13
+ const [, num, unit] = match;
14
+ const bytes = Number(num) * (bytesUnit[unit] ?? 1);
15
+ return Math.max(0, Math.floor(bytes));
16
+ }
17
+ return 0;
18
+ }
19
+
20
+ const intervalRegex = /^(\d+)(?:-(\d+))?$/;
21
+ const defaultSeparator = /[,\s]+/;
22
+ function collectNumbers(text, separator = defaultSeparator) {
23
+ const intervals = collectIntervals(text, separator);
24
+ const result = [];
25
+ for (const interval of intervals) {
26
+ const [x, y] = interval;
27
+ for (let i = x; i <= y; ++i)
28
+ result.push(i);
29
+ }
30
+ return result;
31
+ }
32
+ function collectIntervals(text, separator = defaultSeparator) {
33
+ const intervals = [];
34
+ const tokens = text.split(new RegExp(separator, 'g'));
35
+ for (const token of tokens) {
36
+ const match = intervalRegex.exec(token);
37
+ if (!match)
38
+ continue;
39
+ const [, lft, rht] = match;
40
+ const x = Number(lft);
41
+ if (typeof rht !== 'string') {
42
+ const x = Number(lft);
43
+ intervals.push([x, x]);
44
+ continue;
45
+ }
46
+ const y = Number(rht);
47
+ intervals.push(x < y ? [x, y] : [y, x]);
48
+ }
49
+ intervals.sort((x, y) => {
50
+ const d = x[0] - y[0];
51
+ return d === 0 ? x[1] - y[1] : d;
52
+ });
53
+ if (intervals.length <= 1)
54
+ return intervals;
55
+ const result = [intervals[0]];
56
+ for (let i = 1; i < intervals.length; ++i) {
57
+ const interval = intervals[i];
58
+ const top = result[result.length - 1];
59
+ if (top[1] + 1 >= interval[0]) {
60
+ if (top[1] < interval[1])
61
+ top[1] = interval[1];
62
+ continue;
63
+ }
64
+ result.push(interval);
65
+ }
66
+ return result;
67
+ }
68
+
1
69
  const SPLIT_LOWER_UPPER_RE = /([\p{Ll}\d])(\p{Lu})/gu;
2
70
  const SPLIT_UPPER_UPPER_RE = /(\p{Lu})([\p{Lu}][\p{Ll}])/gu;
3
71
  const SPLIT_NUMBER_LOWER_RE = /(\d)(\p{Ll})/gu;
@@ -183,5 +251,4 @@ const toTitleCase = text => titleCase(text);
183
251
  const toTrim = text => text.trim();
184
252
  const toUpperCase = text => text.toLocaleUpperCase();
185
253
 
186
- export { composeTextTransformers, toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toLowerCase, toPascalCase, toPathCase, toSentenceCase, toSnakeCase, toTitleCase, toTrim, toUpperCase };
187
- //# sourceMappingURL=index.mjs.map
254
+ export { collectIntervals, collectNumbers, composeTextTransformers, parseBytesString, toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toLowerCase, toPascalCase, toPathCase, toSentenceCase, toSnakeCase, toTitleCase, toTrim, toUpperCase };
@@ -1,3 +1,40 @@
1
+ /**
2
+ * Parse texts like 1M, 1Mb, 1G to the number of bytes.
3
+ *
4
+ * @param text
5
+ */
6
+ declare function parseBytesString(text: string): number;
7
+
8
+ /**
9
+ * - '' => []
10
+ * - '1' => [1]
11
+ * - '1-3' => [1, 2, 3]
12
+ * - '3,1-2,2,2' => [1, 2, 3]
13
+ * - '3,7-5,2,2' => [2, 3, 5, 6, 7]
14
+ * - '2,1-3' => [1, 2, 3]
15
+ * - '4,1-3' => [1, 2, 3, 4]
16
+ * - '2-4,1-3,5-9' => [1, 2, 3, 4, 5, 6, 7, 8, 9]
17
+ * - '2-4,1-3,6-9' => [1, 2, 3, 4, 6, 7, 8, 9]
18
+ *
19
+ * @param text
20
+ */
21
+ declare function collectNumbers(text: string, separator?: RegExp): number[];
22
+ /**
23
+ * - '' => []
24
+ * - '1' => [[1, 1]]
25
+ * - '1-3' => [[1, 3]]
26
+ * - '3,1-2,2,2' => [[1, 3]]
27
+ * - '3,7-5,2,2' => [[2, 3], [5, 7]]
28
+ * - '2,1-3' => [[1, 3]]
29
+ * - '4,1-3' => [[1, 4]]
30
+ * - '2-4,1-3,5-9' => [[1, 9]]
31
+ * - '2-4,1-3,6-9' => [[1, 4], [6, 9]]
32
+ *
33
+ * @param text
34
+ * @returns
35
+ */
36
+ declare function collectIntervals(text: string, separator?: RegExp): Array<[number, number]>;
37
+
1
38
  /**
2
39
  * Text transformer.
3
40
  */
@@ -128,5 +165,5 @@ declare const toTrim: ITextTransformer;
128
165
  */
129
166
  declare const toUpperCase: ITextTransformer;
130
167
 
131
- export { type ITextTransformer, composeTextTransformers, toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toLowerCase, toPascalCase, toPathCase, toSentenceCase, toSnakeCase, toTitleCase, toTrim, toUpperCase };
132
- //# sourceMappingURL=index.d.ts.map
168
+ export { collectIntervals, collectNumbers, composeTextTransformers, parseBytesString, toCamelCase, toCapitalCase, toConstantCase, toDotCase, toKebabCase, toLowerCase, toPascalCase, toPathCase, toSentenceCase, toSnakeCase, toTitleCase, toTrim, toUpperCase };
169
+ export type { ITextTransformer };
package/package.json CHANGED
@@ -1,29 +1,32 @@
1
1
  {
2
2
  "name": "@guanghechen/string",
3
- "version": "1.0.1",
4
- "description": "Utilities for processing strings or stringify other type data.",
3
+ "version": "2.0.2",
4
+ "description": "String utilities including case transformers and parsers.",
5
5
  "author": {
6
6
  "name": "guanghechen",
7
7
  "url": "https://github.com/guanghechen/"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/guanghechen/sora/tree/@guanghechen/string@1.0.0",
11
+ "url": "https://github.com/guanghechen/sora/tree/@guanghechen/string@2.0.0",
12
12
  "directory": "packages/string"
13
13
  },
14
- "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/string@1.0.0/packages/string#readme",
14
+ "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/string@2.0.0/packages/string#readme",
15
15
  "keywords": [
16
16
  "string",
17
- "string transformer",
18
- "string utils"
17
+ "case",
18
+ "camel-case",
19
+ "kebab-case",
20
+ "snake-case",
21
+ "pascal-case"
19
22
  ],
20
23
  "type": "module",
21
24
  "exports": {
22
25
  ".": {
26
+ "types": "./lib/types/index.d.ts",
23
27
  "source": "./src/index.ts",
24
28
  "import": "./lib/esm/index.mjs",
25
- "require": "./lib/cjs/index.cjs",
26
- "types": "./lib/types/index.d.ts"
29
+ "require": "./lib/cjs/index.cjs"
27
30
  }
28
31
  },
29
32
  "source": "./src/index.ts",
@@ -31,9 +34,6 @@
31
34
  "module": "./lib/esm/index.mjs",
32
35
  "types": "./lib/types/index.d.ts",
33
36
  "license": "MIT",
34
- "engines": {
35
- "node": ">= 18.0.0"
36
- },
37
37
  "files": [
38
38
  "lib/",
39
39
  "!lib/**/*.map",
@@ -42,5 +42,11 @@
42
42
  "LICENSE",
43
43
  "README.md"
44
44
  ],
45
- "gitHead": "2723aaa31e8ac85afed5b25760875db429d88563"
46
- }
45
+ "scripts": {
46
+ "build": "rollup -c ../../rollup.config.mjs",
47
+ "clean": "rimraf lib",
48
+ "test": "vitest run --config ../../vitest.config.ts",
49
+ "test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
50
+ "test:update": "vitest run --config ../../vitest.config.ts -u"
51
+ }
52
+ }