@bhsd/common 1.0.1 → 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/dist/index.d.ts CHANGED
@@ -1,3 +1,9 @@
1
+ declare interface JsonError {
2
+ message: string;
3
+ line: number | null;
4
+ column: number | null;
5
+ position: number | null;
6
+ }
1
7
  export type RegexGetter<T = string> = (s: T) => RegExp;
2
8
  export declare const wmf = "wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)";
3
9
  /**
@@ -30,5 +36,12 @@ export declare function getRegex<T extends object>(f: RegexGetter<T>): RegexGett
30
36
  /**
31
37
  * 缓存生成的正则表达式
32
38
  * @param f 生成正则表达式的函数
39
+ * @deprecated 需要改为使用`getRegex`
33
40
  */
34
41
  export declare const getObjRegex: typeof getRegex;
42
+ /**
43
+ * 诊断JSON字符串中的语法错误
44
+ * @param str JSON字符串
45
+ */
46
+ export declare const lintJSON: (str: string) => JsonError[];
47
+ export {};
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getObjRegex = exports.sanitizeInlineStyle = exports.splitColors = exports.numToHex = exports.rawurldecode = exports.wmf = void 0;
3
+ exports.lintJSON = exports.getObjRegex = exports.sanitizeInlineStyle = exports.splitColors = exports.numToHex = exports.rawurldecode = exports.wmf = void 0;
4
4
  exports.getRegex = getRegex;
5
5
  exports.wmf = 'wiktionary|wiki(?:pedia|books|news|quote|source|versity|voyage)';
6
6
  /**
@@ -53,7 +53,6 @@ exports.splitColors = splitColors;
53
53
  const sanitizeInlineStyle = (style) => style.replace(/[{}]/gu, p => p === '{' ? '{' : '}');
54
54
  exports.sanitizeInlineStyle = sanitizeInlineStyle;
55
55
  function getRegex(f) {
56
- /* eslint-enable jsdoc/require-jsdoc */
57
56
  const map = new Map(), weakMap = new WeakMap();
58
57
  return s => {
59
58
  const regexp = typeof s === 'string' ? map : weakMap;
@@ -70,5 +69,26 @@ function getRegex(f) {
70
69
  /**
71
70
  * 缓存生成的正则表达式
72
71
  * @param f 生成正则表达式的函数
72
+ * @deprecated 需要改为使用`getRegex`
73
73
  */
74
74
  exports.getObjRegex = getRegex;
75
+ const mt2num = (mt) => mt && Number(mt[1]);
76
+ /**
77
+ * 诊断JSON字符串中的语法错误
78
+ * @param str JSON字符串
79
+ */
80
+ const lintJSON = (str) => {
81
+ try {
82
+ if (str.trim()) {
83
+ JSON.parse(str);
84
+ }
85
+ }
86
+ catch (e) {
87
+ if (e instanceof SyntaxError) {
88
+ const { message } = e, line = mt2num(/\bline (\d+)/u.exec(message)), column = mt2num(/\bcolumn (\d+)/u.exec(message)), position = mt2num(/\bposition (\d+)/u.exec(message));
89
+ return [{ message, line, column, position }];
90
+ }
91
+ }
92
+ return [];
93
+ };
94
+ exports.lintJSON = lintJSON;
package/dist/index.mjs CHANGED
@@ -42,9 +42,24 @@ function getRegex(f) {
42
42
  };
43
43
  }
44
44
  const getObjRegex = getRegex;
45
+ const mt2num = (mt) => mt && Number(mt[1]);
46
+ const lintJSON = (str) => {
47
+ try {
48
+ if (str.trim()) {
49
+ JSON.parse(str);
50
+ }
51
+ } catch (e) {
52
+ if (e instanceof SyntaxError) {
53
+ const { message } = e, line = mt2num(/\bline (\d+)/u.exec(message)), column = mt2num(/\bcolumn (\d+)/u.exec(message)), position = mt2num(/\bposition (\d+)/u.exec(message));
54
+ return [{ message, line, column, position }];
55
+ }
56
+ }
57
+ return [];
58
+ };
45
59
  export {
46
60
  getObjRegex,
47
61
  getRegex,
62
+ lintJSON,
48
63
  numToHex,
49
64
  rawurldecode,
50
65
  sanitizeInlineStyle,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhsd/common",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "homepage": "https://github.com/bhsd-harry/common#readme",
5
5
  "bugs": {
6
6
  "url": "https://github.com/bhsd-harry/common/issues"
@@ -26,21 +26,20 @@
26
26
  "lint": "npm run lint:ts"
27
27
  },
28
28
  "devDependencies": {
29
- "@bhsd/code-standard": "^1.0.0",
30
- "@stylistic/eslint-plugin": "^3.1.0",
31
- "@types/node": "^24.0.10",
32
- "@typescript-eslint/eslint-plugin": "^8.35.1",
33
- "@typescript-eslint/parser": "^8.35.1",
34
- "esbuild": "^0.25.5",
35
- "eslint": "^8.57.1",
36
- "eslint-plugin-es-x": "^8.7.0",
29
+ "@bhsd/code-standard": "^1.3.2",
30
+ "@stylistic/eslint-plugin": "^5.5.0",
31
+ "@typescript-eslint/eslint-plugin": "^8.46.4",
32
+ "@typescript-eslint/parser": "^8.46.4",
33
+ "esbuild": "^0.25.12",
34
+ "eslint": "^9.39.1",
35
+ "eslint-plugin-es-x": "^9.1.2",
37
36
  "eslint-plugin-eslint-comments": "^3.2.0",
38
- "eslint-plugin-jsdoc": "^52.0.2",
39
- "eslint-plugin-json-es": "^1.6.0",
37
+ "eslint-plugin-jsdoc": "^61.2.1",
38
+ "eslint-plugin-jsonc": "^2.21.0",
40
39
  "eslint-plugin-promise": "^7.2.1",
41
- "eslint-plugin-regexp": "^2.9.0",
42
- "eslint-plugin-unicorn": "^56.0.1",
43
- "typescript": "^5.8.2"
40
+ "eslint-plugin-regexp": "^2.10.0",
41
+ "eslint-plugin-unicorn": "^62.0.0",
42
+ "typescript": "^5.9.3"
44
43
  },
45
44
  "engines": {
46
45
  "node": ">=18.17.0"