@bhsd/nodejs 1.0.0 → 1.1.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/README.md +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/replace.d.ts +21 -0
- package/dist/replace.js +46 -0
- package/package.json +5 -15
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
# @bhsd/nodejs
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@bhsd/nodejs)
|
|
4
|
-
[](https://github.com/bhsd-harry/nodejs/actions/workflows/codeql.yml)
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -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
|
+
}
|
package/dist/replace.js
ADDED
|
@@ -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.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"homepage": "https://github.com/bhsd-harry/nodejs#readme",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/bhsd-harry/nodejs/issues"
|
|
@@ -14,26 +14,16 @@
|
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"scripts": {
|
|
17
|
+
"ls": "npm i --package-lock-only && npm ls --package-lock-only --all --omit=dev",
|
|
17
18
|
"prepublishOnly": "npm run build",
|
|
18
19
|
"build": "tsc",
|
|
19
20
|
"lint:ts": "tsc --noEmit && eslint --cache .",
|
|
20
21
|
"lint": "npm run lint:ts"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
|
-
"@bhsd/code-standard": "^
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"@typescript-eslint/eslint-plugin": "^8.57.0",
|
|
27
|
-
"@typescript-eslint/parser": "^8.57.0",
|
|
28
|
-
"eslint": "^9.39.4",
|
|
29
|
-
"eslint-plugin-eslint-comments": "^3.2.0",
|
|
30
|
-
"eslint-plugin-jsdoc": "^62.7.1",
|
|
31
|
-
"eslint-plugin-jsonc": "^3.1.1",
|
|
32
|
-
"eslint-plugin-n": "^17.24.0",
|
|
33
|
-
"eslint-plugin-promise": "^7.2.1",
|
|
34
|
-
"eslint-plugin-regexp": "^3.1.0",
|
|
35
|
-
"eslint-plugin-unicorn": "^63.0.0",
|
|
36
|
-
"typescript": "^5.9.3"
|
|
24
|
+
"@bhsd/code-standard": "^3.0.0",
|
|
25
|
+
"eslint": "^10.5.0",
|
|
26
|
+
"typescript": "^6.0.3"
|
|
37
27
|
},
|
|
38
28
|
"engines": {
|
|
39
29
|
"node": "^20.19.0 || ^22.13.0 || >=24.11.0"
|