@digitalwalletcorp/utils 0.1.0 → 0.4.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.
Files changed (104) hide show
  1. package/README.md +13 -12
  2. package/lib/crypto-util-DFJqfmuw.js +46 -0
  3. package/lib/crypto-util-DFJqfmuw.js.map +1 -0
  4. package/lib/crypto-util-DeZSGetI.cjs +2 -0
  5. package/lib/crypto-util-DeZSGetI.cjs.map +1 -0
  6. package/lib/crypto-util.cjs +2 -0
  7. package/lib/crypto-util.cjs.map +1 -0
  8. package/lib/crypto-util.d.cts +28 -0
  9. package/lib/crypto-util.d.ts +28 -0
  10. package/lib/crypto-util.js +7 -0
  11. package/lib/crypto-util.js.map +1 -0
  12. package/lib/date-util-CG7L8nSP.js +184 -0
  13. package/lib/date-util-CG7L8nSP.js.map +1 -0
  14. package/lib/date-util-DopNKy3u.cjs +2 -0
  15. package/lib/date-util-DopNKy3u.cjs.map +1 -0
  16. package/lib/date-util.cjs +2 -0
  17. package/lib/date-util.cjs.map +1 -0
  18. package/lib/date-util.d.cts +231 -0
  19. package/lib/date-util.d.ts +231 -0
  20. package/lib/date-util.js +23 -0
  21. package/lib/date-util.js.map +1 -0
  22. package/lib/decimal.cjs +2 -0
  23. package/lib/decimal.cjs.map +1 -0
  24. package/lib/decimal.d.cts +197 -0
  25. package/lib/decimal.d.ts +197 -0
  26. package/lib/decimal.js +227 -0
  27. package/lib/decimal.js.map +1 -0
  28. package/lib/file-util-D09BvDYY.cjs +2 -0
  29. package/lib/file-util-D09BvDYY.cjs.map +1 -0
  30. package/lib/file-util-H-c_eQcd.js +140 -0
  31. package/lib/file-util-H-c_eQcd.js.map +1 -0
  32. package/lib/file-util.cjs +2 -0
  33. package/lib/file-util.cjs.map +1 -0
  34. package/lib/file-util.d.cts +119 -0
  35. package/lib/file-util.d.ts +119 -0
  36. package/lib/file-util.js +18 -0
  37. package/lib/file-util.js.map +1 -0
  38. package/lib/html-util-Bxn5YDSK.cjs +2 -0
  39. package/lib/html-util-Bxn5YDSK.cjs.map +1 -0
  40. package/lib/html-util-CgMIuOsf.js +49 -0
  41. package/lib/html-util-CgMIuOsf.js.map +1 -0
  42. package/lib/html-util.cjs +2 -0
  43. package/lib/html-util.cjs.map +1 -0
  44. package/lib/html-util.d.cts +71 -0
  45. package/lib/html-util.d.ts +71 -0
  46. package/lib/html-util.js +8 -0
  47. package/lib/html-util.js.map +1 -0
  48. package/lib/index.cjs +2 -0
  49. package/lib/index.cjs.map +1 -0
  50. package/lib/index.d.cts +9 -0
  51. package/lib/index.d.ts +8 -0
  52. package/lib/index.js +21 -38
  53. package/lib/index.js.map +1 -1
  54. package/lib/json-util-BvrFYh5E.cjs +3 -0
  55. package/lib/json-util-BvrFYh5E.cjs.map +1 -0
  56. package/lib/json-util-YX7xSF6R.js +136 -0
  57. package/lib/json-util-YX7xSF6R.js.map +1 -0
  58. package/lib/json-util.cjs +2 -0
  59. package/lib/json-util.cjs.map +1 -0
  60. package/lib/json-util.d.cts +58 -0
  61. package/lib/json-util.d.ts +58 -0
  62. package/lib/json-util.js +8 -0
  63. package/lib/json-util.js.map +1 -0
  64. package/lib/number-util-BA4KVFyF.js +198 -0
  65. package/lib/number-util-BA4KVFyF.js.map +1 -0
  66. package/lib/number-util-BaSf1wWH.cjs +2 -0
  67. package/lib/number-util-BaSf1wWH.cjs.map +1 -0
  68. package/lib/number-util.cjs +2 -0
  69. package/lib/number-util.cjs.map +1 -0
  70. package/lib/number-util.d.cts +237 -0
  71. package/lib/number-util.d.ts +65 -51
  72. package/lib/number-util.js +23 -495
  73. package/lib/number-util.js.map +1 -1
  74. package/lib/object-util-DhGi-PmA.js +204 -0
  75. package/lib/object-util-DhGi-PmA.js.map +1 -0
  76. package/lib/object-util-Z0Ah5J8A.cjs +2 -0
  77. package/lib/object-util-Z0Ah5J8A.cjs.map +1 -0
  78. package/lib/object-util.cjs +2 -0
  79. package/lib/object-util.cjs.map +1 -0
  80. package/lib/object-util.d.cts +157 -0
  81. package/lib/object-util.d.ts +157 -0
  82. package/lib/object-util.js +16 -0
  83. package/lib/object-util.js.map +1 -0
  84. package/lib/string-util-Cw0VgbIH.js +52 -0
  85. package/lib/string-util-Cw0VgbIH.js.map +1 -0
  86. package/lib/string-util-w2I5EYuY.cjs +2 -0
  87. package/lib/string-util-w2I5EYuY.cjs.map +1 -0
  88. package/lib/string-util.cjs +2 -0
  89. package/lib/string-util.cjs.map +1 -0
  90. package/lib/string-util.d.cts +51 -0
  91. package/lib/string-util.d.ts +51 -0
  92. package/lib/string-util.js +8 -0
  93. package/lib/string-util.js.map +1 -0
  94. package/package.json +59 -16
  95. package/src/crypto-util.ts +84 -0
  96. package/src/date-util.ts +448 -0
  97. package/src/decimal.ts +294 -0
  98. package/src/file-util.ts +281 -0
  99. package/src/html-util.ts +196 -0
  100. package/src/index.ts +8 -0
  101. package/src/json-util.ts +271 -0
  102. package/src/number-util.ts +141 -194
  103. package/src/object-util.ts +482 -0
  104. package/src/string-util.ts +115 -0
@@ -0,0 +1,71 @@
1
+ /**
2
+ * class属性に指定する値
3
+ * 配列は要素を空白区切りで連結し、Recordは値がtrueのキーだけを連結する
4
+ */
5
+ export type HtmlClassValue = string | string[] | Record<string, boolean>;
6
+ /**
7
+ * style属性に指定する値
8
+ * Recordのキーはキャメルケースをケバブケースに変換する。数値には単位を付けない
9
+ */
10
+ export type HtmlStyleValue = string | Record<string, string | number>;
11
+ /**
12
+ * 生成するタグに付与する属性
13
+ */
14
+ export interface HtmlTagOptions {
15
+ class?: HtmlClassValue;
16
+ style?: HtmlStyleValue;
17
+ }
18
+ /**
19
+ * 文字列をWEB表示用にフォーマットする
20
+ * オプションのhtmlにより、入力文字列がHTMLなのかプレーンテキストなのかを指定可能
21
+ *
22
+ * @param {string | null | undefined} str
23
+ * @param {*} [options]
24
+ * ├ html boolean trueを指定した場合、入力された文字列をHTMLとして扱う
25
+ * @returns {string}
26
+ */
27
+ export declare function formatStringForWeb(str: string | null | undefined, options?: {
28
+ html?: boolean;
29
+ }): string;
30
+ /**
31
+ * 文字列の中で一致したワードをspanタグで囲んだHTML文字列を返す
32
+ * オプションのhtmlにより、入力文字列がHTMLなのかプレーンテキストなのかを指定可能
33
+ *
34
+ * 例)
35
+ * HtmlUtil.highlightWord('This is a test message', 'test', { class: 'highlight' });
36
+ * → 'This is a <span class="highlight">test</span> message'
37
+ *
38
+ * @param {string | null | undefined} value
39
+ * @param {string | null | undefined} word
40
+ * @param {*} [options] spanタグに付与するclass, styleのほか、以下を指定できる
41
+ * ├ class string | string[] | Record<string, boolean>
42
+ * ├ style string | Record<string, string | number>
43
+ * ├ caseInsensitive boolean trueを指定した場合、大文字小文字を区別しない
44
+ * ├ html boolean trueを指定した場合、入力された文字列をHTMLとして扱う
45
+ * @returns {string}
46
+ */
47
+ export declare function highlightWord(value: string | null | undefined, word: string | null | undefined, options?: HtmlTagOptions & {
48
+ caseInsensitive?: boolean;
49
+ html?: boolean;
50
+ }): string;
51
+ /**
52
+ * 文字列を1文字ずつ比較し、差分をspanタグで囲んだHTML文字列を返す
53
+ * 追加・削除・一致の部分ごとに、spanタグに付与するclass, styleを指定できる
54
+ * 入力文字列はプレーンなテキストであるとみなす
55
+ * 出力される文字列は、diffで埋め込まれるHTMLタグ以外はHTMLエスケープされ、HTMLセーフな文字列となる
56
+ *
57
+ * @param {string | null | undefined} text1 比較元の文字列
58
+ * @param {string | null | undefined} text2 比較される文字列(通常、入力値はこちらに渡す)
59
+ * @param {*} [options]
60
+ * ├ ignoreCase boolean trueを指定した場合、大文字小文字を区別しない
61
+ * ├ added HtmlTagOptions text2で追加された部分のspanタグに付与する属性
62
+ * ├ removed HtmlTagOptions text2で削除された部分のspanタグに付与する属性
63
+ * ├ same HtmlTagOptions 一致した部分のspanタグに付与する属性
64
+ * @returns {string}
65
+ */
66
+ export declare function diffForWeb(text1: string | null | undefined, text2: string | null | undefined, options?: {
67
+ ignoreCase?: boolean;
68
+ added?: HtmlTagOptions;
69
+ removed?: HtmlTagOptions;
70
+ same?: HtmlTagOptions;
71
+ }): string;
@@ -0,0 +1,71 @@
1
+ /**
2
+ * class属性に指定する値
3
+ * 配列は要素を空白区切りで連結し、Recordは値がtrueのキーだけを連結する
4
+ */
5
+ export type HtmlClassValue = string | string[] | Record<string, boolean>;
6
+ /**
7
+ * style属性に指定する値
8
+ * Recordのキーはキャメルケースをケバブケースに変換する。数値には単位を付けない
9
+ */
10
+ export type HtmlStyleValue = string | Record<string, string | number>;
11
+ /**
12
+ * 生成するタグに付与する属性
13
+ */
14
+ export interface HtmlTagOptions {
15
+ class?: HtmlClassValue;
16
+ style?: HtmlStyleValue;
17
+ }
18
+ /**
19
+ * 文字列をWEB表示用にフォーマットする
20
+ * オプションのhtmlにより、入力文字列がHTMLなのかプレーンテキストなのかを指定可能
21
+ *
22
+ * @param {string | null | undefined} str
23
+ * @param {*} [options]
24
+ * ├ html boolean trueを指定した場合、入力された文字列をHTMLとして扱う
25
+ * @returns {string}
26
+ */
27
+ export declare function formatStringForWeb(str: string | null | undefined, options?: {
28
+ html?: boolean;
29
+ }): string;
30
+ /**
31
+ * 文字列の中で一致したワードをspanタグで囲んだHTML文字列を返す
32
+ * オプションのhtmlにより、入力文字列がHTMLなのかプレーンテキストなのかを指定可能
33
+ *
34
+ * 例)
35
+ * HtmlUtil.highlightWord('This is a test message', 'test', { class: 'highlight' });
36
+ * → 'This is a <span class="highlight">test</span> message'
37
+ *
38
+ * @param {string | null | undefined} value
39
+ * @param {string | null | undefined} word
40
+ * @param {*} [options] spanタグに付与するclass, styleのほか、以下を指定できる
41
+ * ├ class string | string[] | Record<string, boolean>
42
+ * ├ style string | Record<string, string | number>
43
+ * ├ caseInsensitive boolean trueを指定した場合、大文字小文字を区別しない
44
+ * ├ html boolean trueを指定した場合、入力された文字列をHTMLとして扱う
45
+ * @returns {string}
46
+ */
47
+ export declare function highlightWord(value: string | null | undefined, word: string | null | undefined, options?: HtmlTagOptions & {
48
+ caseInsensitive?: boolean;
49
+ html?: boolean;
50
+ }): string;
51
+ /**
52
+ * 文字列を1文字ずつ比較し、差分をspanタグで囲んだHTML文字列を返す
53
+ * 追加・削除・一致の部分ごとに、spanタグに付与するclass, styleを指定できる
54
+ * 入力文字列はプレーンなテキストであるとみなす
55
+ * 出力される文字列は、diffで埋め込まれるHTMLタグ以外はHTMLエスケープされ、HTMLセーフな文字列となる
56
+ *
57
+ * @param {string | null | undefined} text1 比較元の文字列
58
+ * @param {string | null | undefined} text2 比較される文字列(通常、入力値はこちらに渡す)
59
+ * @param {*} [options]
60
+ * ├ ignoreCase boolean trueを指定した場合、大文字小文字を区別しない
61
+ * ├ added HtmlTagOptions text2で追加された部分のspanタグに付与する属性
62
+ * ├ removed HtmlTagOptions text2で削除された部分のspanタグに付与する属性
63
+ * ├ same HtmlTagOptions 一致した部分のspanタグに付与する属性
64
+ * @returns {string}
65
+ */
66
+ export declare function diffForWeb(text1: string | null | undefined, text2: string | null | undefined, options?: {
67
+ ignoreCase?: boolean;
68
+ added?: HtmlTagOptions;
69
+ removed?: HtmlTagOptions;
70
+ same?: HtmlTagOptions;
71
+ }): string;
@@ -0,0 +1,8 @@
1
+ import "diff";
2
+ import { d as f, f as i, a as t } from "./html-util-CgMIuOsf.js";
3
+ export {
4
+ f as diffForWeb,
5
+ i as formatStringForWeb,
6
+ t as highlightWord
7
+ };
8
+ //# sourceMappingURL=html-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html-util.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
package/lib/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./crypto-util-DeZSGetI.cjs"),i=require("./date-util-DopNKy3u.cjs"),e=require("./decimal.cjs"),l=require("./file-util-D09BvDYY.cjs"),r=require("./html-util-Bxn5YDSK.cjs"),U=require("./json-util-BvrFYh5E.cjs"),o=require("./number-util-BaSf1wWH.cjs"),c=require("./object-util-Z0Ah5J8A.cjs"),n=require("./string-util-w2I5EYuY.cjs");exports.CryptoUtil=t.cryptoUtil;exports.DateUtil=i.dateUtil;exports.Decimal=e.Decimal;exports.FileUtil=l.fileUtil;exports.HtmlUtil=r.htmlUtil;exports.JsonUtil=U.jsonUtil;exports.NumberUtil=o.numberUtil;exports.ObjectUtil=c.objectUtil;exports.StringUtil=n.stringUtil;
2
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ export * as CryptoUtil from './crypto-util.cjs';
2
+ export * as DateUtil from './date-util.cjs';
3
+ export { Decimal } from './decimal.cjs';
4
+ export * as FileUtil from './file-util.cjs';
5
+ export * as HtmlUtil from './html-util.cjs';
6
+ export * as JsonUtil from './json-util.cjs';
7
+ export * as NumberUtil from './number-util.cjs';
8
+ export * as ObjectUtil from './object-util.cjs';
9
+ export * as StringUtil from './string-util.cjs';
package/lib/index.d.ts CHANGED
@@ -1 +1,9 @@
1
+ export * as CryptoUtil from './crypto-util';
2
+ export * as DateUtil from './date-util';
3
+ export { Decimal } from './decimal';
4
+ export * as FileUtil from './file-util';
5
+ export * as HtmlUtil from './html-util';
6
+ export * as JsonUtil from './json-util';
1
7
  export * as NumberUtil from './number-util';
8
+ export * as ObjectUtil from './object-util';
9
+ export * as StringUtil from './string-util';
package/lib/index.js CHANGED
@@ -1,38 +1,21 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.NumberUtil = void 0;
37
- exports.NumberUtil = __importStar(require("./number-util"));
38
- //# sourceMappingURL=index.js.map
1
+ import { c as r } from "./crypto-util-DFJqfmuw.js";
2
+ import { d as m } from "./date-util-CG7L8nSP.js";
3
+ import { Decimal as l } from "./decimal.js";
4
+ import { f } from "./file-util-H-c_eQcd.js";
5
+ import { h as s } from "./html-util-CgMIuOsf.js";
6
+ import { j as U } from "./json-util-YX7xSF6R.js";
7
+ import { n } from "./number-util-BA4KVFyF.js";
8
+ import { o as j } from "./object-util-DhGi-PmA.js";
9
+ import { s as d } from "./string-util-Cw0VgbIH.js";
10
+ export {
11
+ r as CryptoUtil,
12
+ m as DateUtil,
13
+ l as Decimal,
14
+ f as FileUtil,
15
+ s as HtmlUtil,
16
+ U as JsonUtil,
17
+ n as NumberUtil,
18
+ j as ObjectUtil,
19
+ d as StringUtil
20
+ };
21
+ //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4DAA4C"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
@@ -0,0 +1,3 @@
1
+ "use strict";const b=/^-?\d+$/;function u(r){return JSON.parse(r,(n,e,t)=>{const i=t?.source;return typeof e=="number"&&!Number.isSafeInteger(e)&&i&&b.test(i)?i:e})}function l(r,n,e,t){const i=[];return JSON.stringify(r,function(f,o){let s=n?n.call(this,f,o):o;if(typeof s=="bigint"&&(s=s.toString()),t?.circular!=="replace"||typeof s!="object"||s===null)return s;for(;i.length&&i[i.length-1].replaced!==this;)i.pop();return i.some(a=>a.original===o||a.replaced===s)?"[Circular]":(i.push({original:o,replaced:s}),s)},e)}const S=/^(\{|\[)/;function m(r){if(S.test(r))try{const n=u(r);return typeof n=="object"&&n!==null?n:void 0}catch{return}}function d(r,n){if(r.length<=n.maxStringLength)return r;const e=Array.from(r);return e.length<=n.maxStringLength?r:`${e.slice(0,n.maxStringLength).join("")}...`}function g(r,n,e,t){switch(typeof r){case"string":{const i=m(r);return i===void 0?JSON.stringify(d(r,t)):g(i,n,e,t)}case"number":case"boolean":return JSON.stringify(r);case"bigint":return JSON.stringify(r.toString());case"object":{if(r===null)return"null";if(typeof r.toFixed=="function")return JSON.stringify(r.toFixed());if(typeof r.toJSON=="function")return g(r.toJSON(e),n,e,t);if(t.ancestors.includes(r))return'"[Circular]"';const i=Array.isArray(r);if(t.maxDepth<n)return i?'"[Array]"':'"[Object]"';t.ancestors.push(r);try{return O(r,i,n,t)}finally{t.ancestors.pop()}}default:return}}function O(r,n,e,t){const i=t.spaces===0?":":": ",f=[];if(n)for(let c=0;c<r.length;c++)f.push(g(r[c],e+1,String(c),t)??"null");else{const c=Object.keys(r);t.sort&&c.sort();for(const p of c){const y=g(r[p],e+1,p,t);y!==void 0&&f.push(`${JSON.stringify(p)}${i}${y}`)}}const[o,s]=n?["[","]"]:["{","}"];if(f.length===0)return`${o}${s}`;if(t.spaces===0)return`${o}${f.join(",")}${s}`;const a=" ".repeat(t.spaces*(e+1)),$=" ".repeat(t.spaces*e);return`${o}${t.newline}${a}${f.join(`,${t.newline}${a}`)}${t.newline}${$}${s}`}function h(r,n){if(r==null)return r;const e={spaces:n?.spaces??2,newline:n?.newline??`
2
+ `,maxDepth:n?.maxDepth??Number.POSITIVE_INFINITY,maxStringLength:n?.maxStringLength??Number.POSITIVE_INFINITY,sort:n?.sort??!1,ancestors:[]};switch(typeof r){case"string":{const t=m(r);return t===void 0?r:g(t,0,"",e)}case"object":return g(r,0,"",e)??r;default:return r}}function N(r){if(!r||typeof r!="string"||!S.test(r))return!1;try{return typeof u(r)=="object"}catch{return!1}}const I=Object.freeze(Object.defineProperty({__proto__:null,formatJsonString:h,isJsonString:N,parse:u,stringify:l},Symbol.toStringTag,{value:"Module"}));exports.formatJsonString=h;exports.isJsonString=N;exports.jsonUtil=I;exports.parse=u;exports.stringify=l;
3
+ //# sourceMappingURL=json-util-BvrFYh5E.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-util-BvrFYh5E.cjs","sources":["../src/json-util.ts"],"sourcesContent":["/**\n * JSON.parseのreviverが第3引数で受け取る情報\n */\ninterface JsonReviverContext {\n /** プリミティブ値のJSON上の表記 */\n source?: string;\n}\n\nconst INTEGER_LITERAL = /^-?\\d+$/;\n\n/**\n * JSON文字列をパースする\n * Number.MIN_SAFE_INTEGER〜Number.MAX_SAFE_INTEGERの範囲外の整数はnumberに変換すると桁落ちするため、JSON上の表記のまま文字列で返す\n * そのため、同じ項目でも値の大きさによってnumberとstringが混在する\n *\n * @param {string} source\n * @returns {T}\n */\nexport function parse<T = any>(source: string): T {\n return JSON.parse(source, (_key: string, value: any, context?: JsonReviverContext) => {\n const literal = context?.source;\n if (typeof value === 'number' && !Number.isSafeInteger(value) && literal && INTEGER_LITERAL.test(literal)) {\n return literal;\n }\n return value;\n }) as T;\n}\n\n/**\n * 値をJSON文字列に変換する\n * bigintは文字列として出力する\n *\n * @param {any} value\n * @param {(this: any, key: string, value: any) => any} [replacer]\n * @param {string | number} [space]\n * @param {*} [options]\n * ├ circular 'replace' 循環参照を'[Circular]'に置き換える。未指定の場合、循環参照があればTypeErrorをスローする\n * @returns {string}\n */\nexport function stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number, options?: {\n circular?: 'replace';\n}): string {\n // 祖先の「元の値」と「replacerを適用した後の値」の組。JSON.stringifyが走査するのは後者だが、循環参照の判定は前者で行う。\n // replacerが新しいオブジェクトを返す場合でも、元の値が同じなら循環参照として検出できるようにするため\n const ancestors: { original: any; replaced: object; }[] = [];\n return JSON.stringify(value, function(this: any, key: string, current: any) {\n let result = replacer ? replacer.call(this, key, current) : current;\n if (typeof result === 'bigint') {\n result = result.toString();\n }\n if (options?.circular !== 'replace' || typeof result !== 'object' || result === null) {\n return result;\n }\n // thisは処理中の値を持つ親オブジェクトである。親より後に積まれた要素は、走査を終えた兄弟の子孫なので取り除く。\n // 残った要素は処理中の値の祖先だけになる。祖先に含まれる値だけを循環参照とみなすため、複数箇所から参照されているだけのオブジェクトは置き換えない。\n while (ancestors.length && ancestors[ancestors.length - 1].replaced !== this) {\n ancestors.pop();\n }\n if (ancestors.some(ancestor => ancestor.original === current || ancestor.replaced === result)) {\n return '[Circular]';\n }\n ancestors.push({ original: current, replaced: result });\n return result;\n }, space);\n}\n\n/**\n * formatJsonStringのオプション\n */\nexport interface FormatJsonStringOptions {\n /** インデントのスペース数。0を指定すると改行・インデントなしの1行にする デフォルト 2 */\n spaces?: number;\n /** 改行コード デフォルト '\\n' */\n newline?: string;\n /** 展開するネストの深さ。ルートを0とし、これを超える深さのオブジェクト・配列は \"[Object]\" / \"[Array]\" に置き換える。未指定の場合は制限なし */\n maxDepth?: number;\n /** 文字列値1項目あたりの最大文字数。超える部分を切り捨てて末尾に '...' を付ける。未指定の場合は制限なし */\n maxStringLength?: number;\n /** trueの場合、オブジェクトのキーをコード単位の昇順に並べる デフォルト false */\n sort?: boolean;\n}\n\ninterface FormatContext {\n spaces: number;\n newline: string;\n maxDepth: number;\n maxStringLength: number;\n sort: boolean;\n /** 処理中の値の祖先。循環参照の判定に使う */\n ancestors: object[];\n}\n\nconst JSON_LIKE_PATTERN = /^(\\{|\\[)/;\n\n/**\n * オブジェクトまたは配列を表すJSON文字列をパースする\n * それ以外の文字列、パースできない文字列はundefinedを返す\n *\n * @param {string} value\n * @returns {object | undefined}\n */\nfunction parseJsonLike(value: string): object | undefined {\n if (!JSON_LIKE_PATTERN.test(value)) {\n return undefined;\n }\n try {\n const parsed = parse(value);\n return typeof parsed === 'object' && parsed !== null ? parsed : undefined;\n } catch (error) {\n return undefined;\n }\n}\n\n/**\n * 文字列をmaxStringLengthに収まるよう切り詰める\n * サロゲートペアを分断しないようコードポイント単位で数える\n *\n * @param {string} value\n * @param {FormatContext} context\n * @returns {string}\n */\nfunction truncate(value: string, context: FormatContext): string {\n if (value.length <= context.maxStringLength) {\n return value;\n }\n const chars = Array.from(value);\n return chars.length <= context.maxStringLength ? value : `${chars.slice(0, context.maxStringLength).join('')}...`;\n}\n\n/**\n * 値をJSON文字列の断片に変換する\n * JSON.stringifyが出力しない値(undefined・関数・symbol)はundefinedを返す\n *\n * @param {any} value\n * @param {number} depth ルートを0とするネストの深さ\n * @param {string} key 親オブジェクトでのキー。toJSONに渡す\n * @param {FormatContext} context\n * @returns {string | undefined}\n */\nfunction formatValue(value: any, depth: number, key: string, context: FormatContext): string | undefined {\n switch (typeof value) {\n case 'string': {\n const parsed = parseJsonLike(value);\n return parsed === undefined ? JSON.stringify(truncate(value, context)) : formatValue(parsed, depth, key, context);\n }\n case 'number':\n case 'boolean':\n return JSON.stringify(value);\n case 'bigint':\n return JSON.stringify(value.toString());\n case 'object': {\n if (value === null) {\n return 'null';\n }\n if (typeof value.toFixed === 'function') {\n return JSON.stringify(value.toFixed());\n }\n if (typeof value.toJSON === 'function') {\n return formatValue(value.toJSON(key), depth, key, context);\n }\n if (context.ancestors.includes(value)) {\n return '\"[Circular]\"';\n }\n const isArray = Array.isArray(value);\n if (context.maxDepth < depth) {\n return isArray ? '\"[Array]\"' : '\"[Object]\"';\n }\n context.ancestors.push(value);\n try {\n return formatContainer(value, isArray, depth, context);\n } finally {\n context.ancestors.pop();\n }\n }\n default:\n return undefined;\n }\n}\n\n/**\n * オブジェクトまたは配列の要素を整形して括弧で囲む\n *\n * @param {any} value\n * @param {boolean} isArray\n * @param {number} depth\n * @param {FormatContext} context\n * @returns {string}\n */\nfunction formatContainer(value: any, isArray: boolean, depth: number, context: FormatContext): string {\n const separator = context.spaces === 0 ? ':' : ': ';\n const entries: string[] = [];\n if (isArray) {\n for (let i = 0; i < value.length; i++) {\n entries.push(formatValue(value[i], depth + 1, String(i), context) ?? 'null');\n }\n } else {\n const keys = Object.keys(value);\n if (context.sort) {\n keys.sort();\n }\n for (const key of keys) {\n const formatted = formatValue(value[key], depth + 1, key, context);\n if (formatted !== undefined) {\n entries.push(`${JSON.stringify(key)}${separator}${formatted}`);\n }\n }\n }\n const [open, close] = isArray ? ['[', ']'] : ['{', '}'];\n if (entries.length === 0) {\n return `${open}${close}`;\n }\n if (context.spaces === 0) {\n return `${open}${entries.join(',')}${close}`;\n }\n const indent = ' '.repeat(context.spaces * (depth + 1));\n const closeIndent = ' '.repeat(context.spaces * depth);\n return `${open}${context.newline}${indent}${entries.join(`,${context.newline}${indent}`)}${context.newline}${closeIndent}${close}`;\n}\n\n/**\n * 値を整形したJSON文字列に変換する\n * JSON文字列を値に持つ項目は、その文字列もオブジェクトとして展開して整形する\n * 循環参照は \"[Circular]\" に置き換える\n * オブジェクト・配列・それらを表すJSON文字列以外の値はそのまま返す\n *\n * @param {any} value\n * @param {FormatJsonStringOptions} [options]\n * @returns {string}\n * @see FormatJsonStringOptions\n */\nexport function formatJsonString(value: any, options?: FormatJsonStringOptions): string {\n if (value == null) {\n return value;\n }\n const context: FormatContext = {\n spaces: options?.spaces ?? 2,\n newline: options?.newline ?? '\\n',\n maxDepth: options?.maxDepth ?? Number.POSITIVE_INFINITY,\n maxStringLength: options?.maxStringLength ?? Number.POSITIVE_INFINITY,\n sort: options?.sort ?? false,\n ancestors: []\n };\n switch (typeof value) {\n case 'string': {\n const parsed = parseJsonLike(value);\n return parsed === undefined ? value : formatValue(parsed, 0, '', context) as string;\n }\n case 'object':\n return formatValue(value, 0, '', context) ?? value;\n default:\n return value;\n }\n}\n\n/**\n * 値がJSON文字列か判定する\n * オブジェクトまたは配列を表すJSON文字列の場合だけtrueを返す\n *\n * @param {any} value\n * @returns {boolean}\n */\nexport function isJsonString(value: any): boolean {\n if (!value || typeof value !== 'string' || !JSON_LIKE_PATTERN.test(value)) {\n return false;\n }\n try {\n return typeof parse(value) === 'object';\n } catch (error) {\n return false;\n }\n}\n"],"names":["INTEGER_LITERAL","parse","source","_key","value","context","literal","stringify","replacer","space","options","ancestors","key","current","result","ancestor","JSON_LIKE_PATTERN","parseJsonLike","parsed","truncate","chars","formatValue","depth","isArray","formatContainer","separator","entries","i","keys","formatted","open","close","indent","closeIndent","formatJsonString","isJsonString"],"mappings":"aAQA,MAAMA,EAAkB,UAUjB,SAASC,EAAeC,EAAmB,CAChD,OAAO,KAAK,MAAMA,EAAQ,CAACC,EAAcC,EAAYC,IAAiC,CACpF,MAAMC,EAAUD,GAAS,OACzB,OAAI,OAAOD,GAAU,UAAY,CAAC,OAAO,cAAcA,CAAK,GAAKE,GAAWN,EAAgB,KAAKM,CAAO,EAC/FA,EAEFF,CACT,CAAC,CACH,CAaO,SAASG,EAAUH,EAAYI,EAAwDC,EAAyBC,EAE5G,CAGT,MAAMC,EAAoD,CAAA,EAC1D,OAAO,KAAK,UAAUP,EAAO,SAAoBQ,EAAaC,EAAc,CAC1E,IAAIC,EAASN,EAAWA,EAAS,KAAK,KAAMI,EAAKC,CAAO,EAAIA,EAI5D,GAHI,OAAOC,GAAW,WACpBA,EAASA,EAAO,SAAA,GAEdJ,GAAS,WAAa,WAAa,OAAOI,GAAW,UAAYA,IAAW,KAC9E,OAAOA,EAIT,KAAOH,EAAU,QAAU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AQ,EAAE,EAChDW,EAAc,IAAI,OAAO5B,EAAQ,OAASiB,CAAK,EACrD,MAAO,GAAGQ,CAAI,GAAGzB,EAAQ,OAAO,GAAG2B,CAAM,GAAGN,EAAQ,KAAK,IAAIrB,EAAQ,OAAO,GAAG2B,CAAM,EAAE,CAAC,GAAG3B,EAAQ,OAAO,GAAG4B,CAAW,GAAGF,CAAK,EAClI,CAaO,SAASG,EAAiB9B,EAAYM,EAA2C,CACtF,GAAIN,GAAS,KACX,OAAOA,EAET,MAAMC,EAAyB,CAC7B,OAAQK,GAAS,QAAU,EAC3B,QAASA,GAAS,SAAW;AAAA,EAC7B,SAAUA,GAAS,UAAY,OAAO,kBACtC,gBAAiBA,GAAS,iBAAmB,OAAO,kBACpD,KAAMA,GAAS,MAAQ,GACvB,UAAW,CAAA,CAAC,EAEd,OAAQ,OAAON,EAAA,CACb,IAAK,SAAU,CACb,MAAMc,EAASD,EAAcb,CAAK,EAClC,OAAOc,IAAW,OAAYd,EAAQiB,EAAYH,EAAQ,EAAG,GAAIb,CAAO,CAC1E,CACA,IAAK,SACH,OAAOgB,EAAYjB,EAAO,EAAG,GAAIC,CAAO,GAAKD,EAC/C,QACE,OAAOA,CAAA,CAEb,CASO,SAAS+B,EAAa/B,EAAqB,CAChD,GAAI,CAACA,GAAS,OAAOA,GAAU,UAAY,CAACY,EAAkB,KAAKZ,CAAK,EACtE,MAAO,GAET,GAAI,CACF,OAAO,OAAOH,EAAMG,CAAK,GAAM,QACjC,MAAgB,CACd,MAAO,EACT,CACF"}
@@ -0,0 +1,136 @@
1
+ const m = /^-?\d+$/;
2
+ function p(r) {
3
+ return JSON.parse(r, (n, e, t) => {
4
+ const s = t?.source;
5
+ return typeof e == "number" && !Number.isSafeInteger(e) && s && m.test(s) ? s : e;
6
+ });
7
+ }
8
+ function N(r, n, e, t) {
9
+ const s = [];
10
+ return JSON.stringify(r, function(f, o) {
11
+ let i = n ? n.call(this, f, o) : o;
12
+ if (typeof i == "bigint" && (i = i.toString()), t?.circular !== "replace" || typeof i != "object" || i === null)
13
+ return i;
14
+ for (; s.length && s[s.length - 1].replaced !== this; )
15
+ s.pop();
16
+ return s.some((g) => g.original === o || g.replaced === i) ? "[Circular]" : (s.push({ original: o, replaced: i }), i);
17
+ }, e);
18
+ }
19
+ const l = /^(\{|\[)/;
20
+ function S(r) {
21
+ if (l.test(r))
22
+ try {
23
+ const n = p(r);
24
+ return typeof n == "object" && n !== null ? n : void 0;
25
+ } catch {
26
+ return;
27
+ }
28
+ }
29
+ function $(r, n) {
30
+ if (r.length <= n.maxStringLength)
31
+ return r;
32
+ const e = Array.from(r);
33
+ return e.length <= n.maxStringLength ? r : `${e.slice(0, n.maxStringLength).join("")}...`;
34
+ }
35
+ function a(r, n, e, t) {
36
+ switch (typeof r) {
37
+ case "string": {
38
+ const s = S(r);
39
+ return s === void 0 ? JSON.stringify($(r, t)) : a(s, n, e, t);
40
+ }
41
+ case "number":
42
+ case "boolean":
43
+ return JSON.stringify(r);
44
+ case "bigint":
45
+ return JSON.stringify(r.toString());
46
+ case "object": {
47
+ if (r === null)
48
+ return "null";
49
+ if (typeof r.toFixed == "function")
50
+ return JSON.stringify(r.toFixed());
51
+ if (typeof r.toJSON == "function")
52
+ return a(r.toJSON(e), n, e, t);
53
+ if (t.ancestors.includes(r))
54
+ return '"[Circular]"';
55
+ const s = Array.isArray(r);
56
+ if (t.maxDepth < n)
57
+ return s ? '"[Array]"' : '"[Object]"';
58
+ t.ancestors.push(r);
59
+ try {
60
+ return b(r, s, n, t);
61
+ } finally {
62
+ t.ancestors.pop();
63
+ }
64
+ }
65
+ default:
66
+ return;
67
+ }
68
+ }
69
+ function b(r, n, e, t) {
70
+ const s = t.spaces === 0 ? ":" : ": ", f = [];
71
+ if (n)
72
+ for (let c = 0; c < r.length; c++)
73
+ f.push(a(r[c], e + 1, String(c), t) ?? "null");
74
+ else {
75
+ const c = Object.keys(r);
76
+ t.sort && c.sort();
77
+ for (const u of c) {
78
+ const y = a(r[u], e + 1, u, t);
79
+ y !== void 0 && f.push(`${JSON.stringify(u)}${s}${y}`);
80
+ }
81
+ }
82
+ const [o, i] = n ? ["[", "]"] : ["{", "}"];
83
+ if (f.length === 0)
84
+ return `${o}${i}`;
85
+ if (t.spaces === 0)
86
+ return `${o}${f.join(",")}${i}`;
87
+ const g = " ".repeat(t.spaces * (e + 1)), h = " ".repeat(t.spaces * e);
88
+ return `${o}${t.newline}${g}${f.join(`,${t.newline}${g}`)}${t.newline}${h}${i}`;
89
+ }
90
+ function d(r, n) {
91
+ if (r == null)
92
+ return r;
93
+ const e = {
94
+ spaces: n?.spaces ?? 2,
95
+ newline: n?.newline ?? `
96
+ `,
97
+ maxDepth: n?.maxDepth ?? Number.POSITIVE_INFINITY,
98
+ maxStringLength: n?.maxStringLength ?? Number.POSITIVE_INFINITY,
99
+ sort: n?.sort ?? !1,
100
+ ancestors: []
101
+ };
102
+ switch (typeof r) {
103
+ case "string": {
104
+ const t = S(r);
105
+ return t === void 0 ? r : a(t, 0, "", e);
106
+ }
107
+ case "object":
108
+ return a(r, 0, "", e) ?? r;
109
+ default:
110
+ return r;
111
+ }
112
+ }
113
+ function O(r) {
114
+ if (!r || typeof r != "string" || !l.test(r))
115
+ return !1;
116
+ try {
117
+ return typeof p(r) == "object";
118
+ } catch {
119
+ return !1;
120
+ }
121
+ }
122
+ const I = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
123
+ __proto__: null,
124
+ formatJsonString: d,
125
+ isJsonString: O,
126
+ parse: p,
127
+ stringify: N
128
+ }, Symbol.toStringTag, { value: "Module" }));
129
+ export {
130
+ d as f,
131
+ O as i,
132
+ I as j,
133
+ p,
134
+ N as s
135
+ };
136
+ //# sourceMappingURL=json-util-YX7xSF6R.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-util-YX7xSF6R.js","sources":["../src/json-util.ts"],"sourcesContent":["/**\n * JSON.parseのreviverが第3引数で受け取る情報\n */\ninterface JsonReviverContext {\n /** プリミティブ値のJSON上の表記 */\n source?: string;\n}\n\nconst INTEGER_LITERAL = /^-?\\d+$/;\n\n/**\n * JSON文字列をパースする\n * Number.MIN_SAFE_INTEGER〜Number.MAX_SAFE_INTEGERの範囲外の整数はnumberに変換すると桁落ちするため、JSON上の表記のまま文字列で返す\n * そのため、同じ項目でも値の大きさによってnumberとstringが混在する\n *\n * @param {string} source\n * @returns {T}\n */\nexport function parse<T = any>(source: string): T {\n return JSON.parse(source, (_key: string, value: any, context?: JsonReviverContext) => {\n const literal = context?.source;\n if (typeof value === 'number' && !Number.isSafeInteger(value) && literal && INTEGER_LITERAL.test(literal)) {\n return literal;\n }\n return value;\n }) as T;\n}\n\n/**\n * 値をJSON文字列に変換する\n * bigintは文字列として出力する\n *\n * @param {any} value\n * @param {(this: any, key: string, value: any) => any} [replacer]\n * @param {string | number} [space]\n * @param {*} [options]\n * ├ circular 'replace' 循環参照を'[Circular]'に置き換える。未指定の場合、循環参照があればTypeErrorをスローする\n * @returns {string}\n */\nexport function stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number, options?: {\n circular?: 'replace';\n}): string {\n // 祖先の「元の値」と「replacerを適用した後の値」の組。JSON.stringifyが走査するのは後者だが、循環参照の判定は前者で行う。\n // replacerが新しいオブジェクトを返す場合でも、元の値が同じなら循環参照として検出できるようにするため\n const ancestors: { original: any; replaced: object; }[] = [];\n return JSON.stringify(value, function(this: any, key: string, current: any) {\n let result = replacer ? replacer.call(this, key, current) : current;\n if (typeof result === 'bigint') {\n result = result.toString();\n }\n if (options?.circular !== 'replace' || typeof result !== 'object' || result === null) {\n return result;\n }\n // thisは処理中の値を持つ親オブジェクトである。親より後に積まれた要素は、走査を終えた兄弟の子孫なので取り除く。\n // 残った要素は処理中の値の祖先だけになる。祖先に含まれる値だけを循環参照とみなすため、複数箇所から参照されているだけのオブジェクトは置き換えない。\n while (ancestors.length && ancestors[ancestors.length - 1].replaced !== this) {\n ancestors.pop();\n }\n if (ancestors.some(ancestor => ancestor.original === current || ancestor.replaced === result)) {\n return '[Circular]';\n }\n ancestors.push({ original: current, replaced: result });\n return result;\n }, space);\n}\n\n/**\n * formatJsonStringのオプション\n */\nexport interface FormatJsonStringOptions {\n /** インデントのスペース数。0を指定すると改行・インデントなしの1行にする デフォルト 2 */\n spaces?: number;\n /** 改行コード デフォルト '\\n' */\n newline?: string;\n /** 展開するネストの深さ。ルートを0とし、これを超える深さのオブジェクト・配列は \"[Object]\" / \"[Array]\" に置き換える。未指定の場合は制限なし */\n maxDepth?: number;\n /** 文字列値1項目あたりの最大文字数。超える部分を切り捨てて末尾に '...' を付ける。未指定の場合は制限なし */\n maxStringLength?: number;\n /** trueの場合、オブジェクトのキーをコード単位の昇順に並べる デフォルト false */\n sort?: boolean;\n}\n\ninterface FormatContext {\n spaces: number;\n newline: string;\n maxDepth: number;\n maxStringLength: number;\n sort: boolean;\n /** 処理中の値の祖先。循環参照の判定に使う */\n ancestors: object[];\n}\n\nconst JSON_LIKE_PATTERN = /^(\\{|\\[)/;\n\n/**\n * オブジェクトまたは配列を表すJSON文字列をパースする\n * それ以外の文字列、パースできない文字列はundefinedを返す\n *\n * @param {string} value\n * @returns {object | undefined}\n */\nfunction parseJsonLike(value: string): object | undefined {\n if (!JSON_LIKE_PATTERN.test(value)) {\n return undefined;\n }\n try {\n const parsed = parse(value);\n return typeof parsed === 'object' && parsed !== null ? parsed : undefined;\n } catch (error) {\n return undefined;\n }\n}\n\n/**\n * 文字列をmaxStringLengthに収まるよう切り詰める\n * サロゲートペアを分断しないようコードポイント単位で数える\n *\n * @param {string} value\n * @param {FormatContext} context\n * @returns {string}\n */\nfunction truncate(value: string, context: FormatContext): string {\n if (value.length <= context.maxStringLength) {\n return value;\n }\n const chars = Array.from(value);\n return chars.length <= context.maxStringLength ? value : `${chars.slice(0, context.maxStringLength).join('')}...`;\n}\n\n/**\n * 値をJSON文字列の断片に変換する\n * JSON.stringifyが出力しない値(undefined・関数・symbol)はundefinedを返す\n *\n * @param {any} value\n * @param {number} depth ルートを0とするネストの深さ\n * @param {string} key 親オブジェクトでのキー。toJSONに渡す\n * @param {FormatContext} context\n * @returns {string | undefined}\n */\nfunction formatValue(value: any, depth: number, key: string, context: FormatContext): string | undefined {\n switch (typeof value) {\n case 'string': {\n const parsed = parseJsonLike(value);\n return parsed === undefined ? JSON.stringify(truncate(value, context)) : formatValue(parsed, depth, key, context);\n }\n case 'number':\n case 'boolean':\n return JSON.stringify(value);\n case 'bigint':\n return JSON.stringify(value.toString());\n case 'object': {\n if (value === null) {\n return 'null';\n }\n if (typeof value.toFixed === 'function') {\n return JSON.stringify(value.toFixed());\n }\n if (typeof value.toJSON === 'function') {\n return formatValue(value.toJSON(key), depth, key, context);\n }\n if (context.ancestors.includes(value)) {\n return '\"[Circular]\"';\n }\n const isArray = Array.isArray(value);\n if (context.maxDepth < depth) {\n return isArray ? '\"[Array]\"' : '\"[Object]\"';\n }\n context.ancestors.push(value);\n try {\n return formatContainer(value, isArray, depth, context);\n } finally {\n context.ancestors.pop();\n }\n }\n default:\n return undefined;\n }\n}\n\n/**\n * オブジェクトまたは配列の要素を整形して括弧で囲む\n *\n * @param {any} value\n * @param {boolean} isArray\n * @param {number} depth\n * @param {FormatContext} context\n * @returns {string}\n */\nfunction formatContainer(value: any, isArray: boolean, depth: number, context: FormatContext): string {\n const separator = context.spaces === 0 ? ':' : ': ';\n const entries: string[] = [];\n if (isArray) {\n for (let i = 0; i < value.length; i++) {\n entries.push(formatValue(value[i], depth + 1, String(i), context) ?? 'null');\n }\n } else {\n const keys = Object.keys(value);\n if (context.sort) {\n keys.sort();\n }\n for (const key of keys) {\n const formatted = formatValue(value[key], depth + 1, key, context);\n if (formatted !== undefined) {\n entries.push(`${JSON.stringify(key)}${separator}${formatted}`);\n }\n }\n }\n const [open, close] = isArray ? ['[', ']'] : ['{', '}'];\n if (entries.length === 0) {\n return `${open}${close}`;\n }\n if (context.spaces === 0) {\n return `${open}${entries.join(',')}${close}`;\n }\n const indent = ' '.repeat(context.spaces * (depth + 1));\n const closeIndent = ' '.repeat(context.spaces * depth);\n return `${open}${context.newline}${indent}${entries.join(`,${context.newline}${indent}`)}${context.newline}${closeIndent}${close}`;\n}\n\n/**\n * 値を整形したJSON文字列に変換する\n * JSON文字列を値に持つ項目は、その文字列もオブジェクトとして展開して整形する\n * 循環参照は \"[Circular]\" に置き換える\n * オブジェクト・配列・それらを表すJSON文字列以外の値はそのまま返す\n *\n * @param {any} value\n * @param {FormatJsonStringOptions} [options]\n * @returns {string}\n * @see FormatJsonStringOptions\n */\nexport function formatJsonString(value: any, options?: FormatJsonStringOptions): string {\n if (value == null) {\n return value;\n }\n const context: FormatContext = {\n spaces: options?.spaces ?? 2,\n newline: options?.newline ?? '\\n',\n maxDepth: options?.maxDepth ?? Number.POSITIVE_INFINITY,\n maxStringLength: options?.maxStringLength ?? Number.POSITIVE_INFINITY,\n sort: options?.sort ?? false,\n ancestors: []\n };\n switch (typeof value) {\n case 'string': {\n const parsed = parseJsonLike(value);\n return parsed === undefined ? value : formatValue(parsed, 0, '', context) as string;\n }\n case 'object':\n return formatValue(value, 0, '', context) ?? value;\n default:\n return value;\n }\n}\n\n/**\n * 値がJSON文字列か判定する\n * オブジェクトまたは配列を表すJSON文字列の場合だけtrueを返す\n *\n * @param {any} value\n * @returns {boolean}\n */\nexport function isJsonString(value: any): boolean {\n if (!value || typeof value !== 'string' || !JSON_LIKE_PATTERN.test(value)) {\n return false;\n }\n try {\n return typeof parse(value) === 'object';\n } catch (error) {\n return false;\n }\n}\n"],"names":["INTEGER_LITERAL","parse","source","_key","value","context","literal","stringify","replacer","space","options","ancestors","key","current","result","ancestor","JSON_LIKE_PATTERN","parseJsonLike","parsed","truncate","chars","formatValue","depth","isArray","formatContainer","separator","entries","i","keys","formatted","open","close","indent","closeIndent","formatJsonString","isJsonString"],"mappings":"AAQA,MAAMA,IAAkB;AAUjB,SAASC,EAAeC,GAAmB;AAChD,SAAO,KAAK,MAAMA,GAAQ,CAACC,GAA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AAA;AAAA,IACb,KAAK,UAAU;AACb,YAAMc,IAASD,EAAcb,CAAK;AAClC,aAAOc,MAAW,SAAYd,IAAQiB,EAAYH,GAAQ,GAAG,IAAIb,CAAO;AAAA,IAC1E;AAAA,IACA,KAAK;AACH,aAAOgB,EAAYjB,GAAO,GAAG,IAAIC,CAAO,KAAKD;AAAA,IAC/C;AACE,aAAOA;AAAA,EAAA;AAEb;AASO,SAAS+B,EAAa/B,GAAqB;AAChD,MAAI,CAACA,KAAS,OAAOA,KAAU,YAAY,CAACY,EAAkB,KAAKZ,CAAK;AACtE,WAAO;AAET,MAAI;AACF,WAAO,OAAOH,EAAMG,CAAK,KAAM;AAAA,EACjC,QAAgB;AACd,WAAO;AAAA,EACT;AACF;;;;;;;;"}
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./json-util-BvrFYh5E.cjs");exports.formatJsonString=r.formatJsonString;exports.isJsonString=r.isJsonString;exports.parse=r.parse;exports.stringify=r.stringify;
2
+ //# sourceMappingURL=json-util.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-util.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,58 @@
1
+ /**
2
+ * JSON文字列をパースする
3
+ * Number.MIN_SAFE_INTEGER〜Number.MAX_SAFE_INTEGERの範囲外の整数はnumberに変換すると桁落ちするため、JSON上の表記のまま文字列で返す
4
+ * そのため、同じ項目でも値の大きさによってnumberとstringが混在する
5
+ *
6
+ * @param {string} source
7
+ * @returns {T}
8
+ */
9
+ export declare function parse<T = any>(source: string): T;
10
+ /**
11
+ * 値をJSON文字列に変換する
12
+ * bigintは文字列として出力する
13
+ *
14
+ * @param {any} value
15
+ * @param {(this: any, key: string, value: any) => any} [replacer]
16
+ * @param {string | number} [space]
17
+ * @param {*} [options]
18
+ * ├ circular 'replace' 循環参照を'[Circular]'に置き換える。未指定の場合、循環参照があればTypeErrorをスローする
19
+ * @returns {string}
20
+ */
21
+ export declare function stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number, options?: {
22
+ circular?: 'replace';
23
+ }): string;
24
+ /**
25
+ * formatJsonStringのオプション
26
+ */
27
+ export interface FormatJsonStringOptions {
28
+ /** インデントのスペース数。0を指定すると改行・インデントなしの1行にする デフォルト 2 */
29
+ spaces?: number;
30
+ /** 改行コード デフォルト '\n' */
31
+ newline?: string;
32
+ /** 展開するネストの深さ。ルートを0とし、これを超える深さのオブジェクト・配列は "[Object]" / "[Array]" に置き換える。未指定の場合は制限なし */
33
+ maxDepth?: number;
34
+ /** 文字列値1項目あたりの最大文字数。超える部分を切り捨てて末尾に '...' を付ける。未指定の場合は制限なし */
35
+ maxStringLength?: number;
36
+ /** trueの場合、オブジェクトのキーをコード単位の昇順に並べる デフォルト false */
37
+ sort?: boolean;
38
+ }
39
+ /**
40
+ * 値を整形したJSON文字列に変換する
41
+ * JSON文字列を値に持つ項目は、その文字列もオブジェクトとして展開して整形する
42
+ * 循環参照は "[Circular]" に置き換える
43
+ * オブジェクト・配列・それらを表すJSON文字列以外の値はそのまま返す
44
+ *
45
+ * @param {any} value
46
+ * @param {FormatJsonStringOptions} [options]
47
+ * @returns {string}
48
+ * @see FormatJsonStringOptions
49
+ */
50
+ export declare function formatJsonString(value: any, options?: FormatJsonStringOptions): string;
51
+ /**
52
+ * 値がJSON文字列か判定する
53
+ * オブジェクトまたは配列を表すJSON文字列の場合だけtrueを返す
54
+ *
55
+ * @param {any} value
56
+ * @returns {boolean}
57
+ */
58
+ export declare function isJsonString(value: any): boolean;
@@ -0,0 +1,58 @@
1
+ /**
2
+ * JSON文字列をパースする
3
+ * Number.MIN_SAFE_INTEGER〜Number.MAX_SAFE_INTEGERの範囲外の整数はnumberに変換すると桁落ちするため、JSON上の表記のまま文字列で返す
4
+ * そのため、同じ項目でも値の大きさによってnumberとstringが混在する
5
+ *
6
+ * @param {string} source
7
+ * @returns {T}
8
+ */
9
+ export declare function parse<T = any>(source: string): T;
10
+ /**
11
+ * 値をJSON文字列に変換する
12
+ * bigintは文字列として出力する
13
+ *
14
+ * @param {any} value
15
+ * @param {(this: any, key: string, value: any) => any} [replacer]
16
+ * @param {string | number} [space]
17
+ * @param {*} [options]
18
+ * ├ circular 'replace' 循環参照を'[Circular]'に置き換える。未指定の場合、循環参照があればTypeErrorをスローする
19
+ * @returns {string}
20
+ */
21
+ export declare function stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number, options?: {
22
+ circular?: 'replace';
23
+ }): string;
24
+ /**
25
+ * formatJsonStringのオプション
26
+ */
27
+ export interface FormatJsonStringOptions {
28
+ /** インデントのスペース数。0を指定すると改行・インデントなしの1行にする デフォルト 2 */
29
+ spaces?: number;
30
+ /** 改行コード デフォルト '\n' */
31
+ newline?: string;
32
+ /** 展開するネストの深さ。ルートを0とし、これを超える深さのオブジェクト・配列は "[Object]" / "[Array]" に置き換える。未指定の場合は制限なし */
33
+ maxDepth?: number;
34
+ /** 文字列値1項目あたりの最大文字数。超える部分を切り捨てて末尾に '...' を付ける。未指定の場合は制限なし */
35
+ maxStringLength?: number;
36
+ /** trueの場合、オブジェクトのキーをコード単位の昇順に並べる デフォルト false */
37
+ sort?: boolean;
38
+ }
39
+ /**
40
+ * 値を整形したJSON文字列に変換する
41
+ * JSON文字列を値に持つ項目は、その文字列もオブジェクトとして展開して整形する
42
+ * 循環参照は "[Circular]" に置き換える
43
+ * オブジェクト・配列・それらを表すJSON文字列以外の値はそのまま返す
44
+ *
45
+ * @param {any} value
46
+ * @param {FormatJsonStringOptions} [options]
47
+ * @returns {string}
48
+ * @see FormatJsonStringOptions
49
+ */
50
+ export declare function formatJsonString(value: any, options?: FormatJsonStringOptions): string;
51
+ /**
52
+ * 値がJSON文字列か判定する
53
+ * オブジェクトまたは配列を表すJSON文字列の場合だけtrueを返す
54
+ *
55
+ * @param {any} value
56
+ * @returns {boolean}
57
+ */
58
+ export declare function isJsonString(value: any): boolean;
@@ -0,0 +1,8 @@
1
+ import { f as a, i, p as n, s as o } from "./json-util-YX7xSF6R.js";
2
+ export {
3
+ a as formatJsonString,
4
+ i as isJsonString,
5
+ n as parse,
6
+ o as stringify
7
+ };
8
+ //# sourceMappingURL=json-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-util.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}