@bhsd/nodejs 1.0.1 → 1.2.0

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/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './replace.js';
1
2
  /**
2
3
  * 进行性能分析,生成prof.json和prof-summary.json文件
3
4
  * @param callback 要分析的函数
@@ -29,3 +30,8 @@ export declare const red: (str: string) => string;
29
30
  * @param str 要显示的字符串
30
31
  */
31
32
  export declare const blue: (str: string) => string;
33
+ /**
34
+ * 将字符串以灰色显示
35
+ * @param str 要显示的字符串
36
+ */
37
+ export declare const gray: (str: string) => string;
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ import * as fs from 'fs';
2
2
  import * as path from 'path';
3
3
  import { Session } from 'inspector/promises';
4
4
  import { styleText } from 'util';
5
+ export * from './replace.js';
5
6
  /**
6
7
  * Adds the ticks to the myTicks object.
7
8
  * @param myTicks ticks记录对象
@@ -75,3 +76,8 @@ export const red = (str) => styleText('red', str);
75
76
  * @param str 要显示的字符串
76
77
  */
77
78
  export const blue = (str) => styleText('blue', str);
79
+ /**
80
+ * 将字符串以灰色显示
81
+ * @param str 要显示的字符串
82
+ */
83
+ export const gray = (str) => styleText('gray', str);
@@ -0,0 +1,21 @@
1
+ export declare class ReplacableString {
2
+ input: string;
3
+ /** @param input 要替换的字符串 */
4
+ constructor(input: string);
5
+ /**
6
+ * 替换并确认
7
+ * @param searchValue 查找的字符串或正则表达式
8
+ * @param replaceValue 替换的字符串
9
+ * @param g 是否全局正则表达式
10
+ * @throws `RangeError` 查找全局正则表达式
11
+ */
12
+ replace(searchValue: string | RegExp, replaceValue: string, g?: boolean): this;
13
+ /**
14
+ * 替换全部并检查替换次数
15
+ * @param searchValue 查找的字符串或正则表达式
16
+ * @param replaceValue 替换的字符串
17
+ * @param count 预期替换的次数
18
+ * @throws `RangeError` 查找的不是字符串或全局正则表达式
19
+ */
20
+ replaceAll(searchValue: string | RegExp, replaceValue: string, count: number): this;
21
+ }
@@ -0,0 +1,46 @@
1
+ import * as assert from 'assert';
2
+ export class ReplacableString {
3
+ input;
4
+ /** @param input 要替换的字符串 */
5
+ constructor(input) {
6
+ this.input = input;
7
+ }
8
+ /**
9
+ * 替换并确认
10
+ * @param searchValue 查找的字符串或正则表达式
11
+ * @param replaceValue 替换的字符串
12
+ * @param g 是否全局正则表达式
13
+ * @throws `RangeError` 查找全局正则表达式
14
+ */
15
+ replace(searchValue, replaceValue, g) {
16
+ if (!g && typeof searchValue === 'object' && searchValue.global) {
17
+ throw new RangeError('search value must not be a global RegExp', { cause: searchValue });
18
+ }
19
+ const { input } = this;
20
+ this.input = input.replace(searchValue, replaceValue);
21
+ assert.notStrictEqual(this.input, input, `replace failed: ${searchValue}`);
22
+ return this;
23
+ }
24
+ /**
25
+ * 替换全部并检查替换次数
26
+ * @param searchValue 查找的字符串或正则表达式
27
+ * @param replaceValue 替换的字符串
28
+ * @param count 预期替换的次数
29
+ * @throws `RangeError` 查找的不是字符串或全局正则表达式
30
+ */
31
+ replaceAll(searchValue, replaceValue, count) {
32
+ const { input } = this, msg = `replaceAll failed: ${searchValue}`;
33
+ if (typeof searchValue === 'string') {
34
+ assert.strictEqual(input.split(searchValue).length - 1, count, msg);
35
+ }
36
+ else if (searchValue.global) {
37
+ // eslint-disable-next-line regexp/prefer-regexp-exec
38
+ assert.strictEqual(input.match(searchValue)?.length, count, msg);
39
+ }
40
+ else {
41
+ throw new RangeError('search value must be a string or a global RegExp', { cause: searchValue });
42
+ }
43
+ this.input = input.replaceAll(searchValue, replaceValue);
44
+ return this;
45
+ }
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/nodejs",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "homepage": "https://github.com/bhsd-harry/nodejs#readme",
5
5
  "bugs": {
6
6
  "url": "https://github.com/bhsd-harry/nodejs/issues"
@@ -21,10 +21,8 @@
21
21
  "lint": "npm run lint:ts"
22
22
  },
23
23
  "devDependencies": {
24
- "@bhsd/code-standard": "^2.5.0",
25
- "@types/node": "^25.9.0",
26
- "@typescript-eslint/parser": "^8.59.4",
27
- "eslint": "^10.4.0",
24
+ "@bhsd/code-standard": "^3.1.0",
25
+ "eslint": "^10.6.0",
28
26
  "typescript": "^6.0.3"
29
27
  },
30
28
  "engines": {