@stryke/json 0.3.0 → 0.4.1

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.
@@ -10,7 +10,7 @@ var _buffer = require("buffer/");
10
10
  var _jsoncParser = require("jsonc-parser");
11
11
  var _superjson = _interopRequireDefault(require("superjson"));
12
12
  var _parseError = require("./utils/parse-error.cjs");
13
- var _stringifyJson = require("./utils/stringify-json.cjs");
13
+ var _stringify = require("./utils/stringify.cjs");
14
14
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
15
  class StormJSON extends _superjson.default {
16
16
  static #e;
@@ -26,12 +26,12 @@ class StormJSON extends _superjson.default {
26
26
  static parse(e) {
27
27
  return StormJSON.instance.parse(e);
28
28
  }
29
- static stringifyJson(e, r) {
29
+ static stringify(e, r) {
30
30
  const s = StormJSON.instance.customTransformerRegistry.findApplicable(e);
31
31
  let t = e;
32
- return s && (t = s.serialize(t)), (0, _stringifyJson.stringifyJson)(t?.json ? t?.json : t, r?.spaces);
32
+ return s && (t = s.serialize(t)), (0, _stringify.stringify)(t?.json ? t?.json : t, r?.spaces);
33
33
  }
34
- static stringify(e) {
34
+ static stringifyBase(e) {
35
35
  return StormJSON.instance.stringify(e);
36
36
  }
37
37
  static parseJson(e, r) {
@@ -30,18 +30,18 @@ export declare class StormJSON extends SuperJSON {
30
30
  * Serializes the given data to a JSON string.
31
31
  * By default the JSON string is formatted with a 2 space indentation to be easy readable.
32
32
  *
33
- * @param json - Object which should be serialized to JSON
33
+ * @param value - Object which should be serialized to JSON
34
34
  * @param options - JSON serialize options
35
35
  * @returns the formatted JSON representation of the object
36
36
  */
37
- static stringifyJson<TJson>(json: TJson, options?: JsonSerializeOptions): string;
37
+ static stringify<T>(value: T, options?: JsonSerializeOptions): string;
38
38
  /**
39
39
  * Stringify the given value with superjson
40
40
  *
41
41
  * @param obj - The object to stringify
42
42
  * @returns The stringified object
43
43
  */
44
- static stringify(obj: any): string;
44
+ static stringifyBase(obj: any): string;
45
45
  /**
46
46
  * Parses the given JSON string and returns the object the JSON content represents.
47
47
  * By default javascript-style comments and trailing commas are allowed.
@@ -1 +1 @@
1
- import{isObject as o}from"@stryke/types/type-checks/is-object";import{isString as l}from"@stryke/types/type-checks/is-string";import{Buffer as a}from"buffer/";import{parse as c}from"jsonc-parser";import u from"superjson";import{formatParseError as p}from"./utils/parse-error";import{stringifyJson as f}from"./utils/stringify-json";export class StormJSON extends u{static#e;static get instance(){return StormJSON.#e||(StormJSON.#e=new StormJSON),StormJSON.#e}static deserialize(e){return StormJSON.instance.deserialize(e)}static serialize(e){return StormJSON.instance.serialize(e)}static parse(e){return StormJSON.instance.parse(e)}static stringifyJson(e,r){const s=StormJSON.instance.customTransformerRegistry.findApplicable(e);let t=e;return s&&(t=s.serialize(t)),f(t?.json?t?.json:t,r?.spaces)}static stringify(e){return StormJSON.instance.stringify(e)}static parseJson(e,r){try{if(r?.expectComments===!1)return StormJSON.instance.parse(e)}catch{}const s=[],t={allowTrailingComma:!0,...r},n=c(e,s,t);if(s.length>0&&s[0])throw new Error(p(e,s[0]));return n}static register(e,r,s,t){StormJSON.instance.registerCustom({isApplicable:t,serialize:r,deserialize:s},e)}static registerClass(e,r){StormJSON.instance.registerClass(e,{identifier:l(r)?r:r?.identifier||e.name,allowProps:r&&o(r)&&r?.allowProps&&Array.isArray(r.allowProps)?r.allowProps:["__typename"]})}constructor(){super({dedupe:!0})}}StormJSON.instance.registerCustom({isApplicable:i=>a.isBuffer(i),serialize:i=>i.toString("base64"),deserialize:i=>a.from(i,"base64")},"Bytes");
1
+ import{isObject as o}from"@stryke/types/type-checks/is-object";import{isString as l}from"@stryke/types/type-checks/is-string";import{Buffer as a}from"buffer/";import{parse as c}from"jsonc-parser";import u from"superjson";import{formatParseError as p}from"./utils/parse-error";import{stringify as f}from"./utils/stringify";export class StormJSON extends u{static#e;static get instance(){return StormJSON.#e||(StormJSON.#e=new StormJSON),StormJSON.#e}static deserialize(e){return StormJSON.instance.deserialize(e)}static serialize(e){return StormJSON.instance.serialize(e)}static parse(e){return StormJSON.instance.parse(e)}static stringify(e,r){const s=StormJSON.instance.customTransformerRegistry.findApplicable(e);let t=e;return s&&(t=s.serialize(t)),f(t?.json?t?.json:t,r?.spaces)}static stringifyBase(e){return StormJSON.instance.stringify(e)}static parseJson(e,r){try{if(r?.expectComments===!1)return StormJSON.instance.parse(e)}catch{}const s=[],t={allowTrailingComma:!0,...r},n=c(e,s,t);if(s.length>0&&s[0])throw new Error(p(e,s[0]));return n}static register(e,r,s,t){StormJSON.instance.registerCustom({isApplicable:t,serialize:r,deserialize:s},e)}static registerClass(e,r){StormJSON.instance.registerClass(e,{identifier:l(r)?r:r?.identifier||e.name,allowProps:r&&o(r)&&r?.allowProps&&Array.isArray(r.allowProps)?r.allowProps:["__typename"]})}constructor(){super({dedupe:!0})}}StormJSON.instance.registerCustom({isApplicable:i=>a.isBuffer(i),serialize:i=>i.toString("base64"),deserialize:i=>a.from(i,"base64")},"Bytes");
@@ -25,14 +25,14 @@ Object.keys(_parseError).forEach(function (key) {
25
25
  }
26
26
  });
27
27
  });
28
- var _stringifyJson = require("./stringify-json.cjs");
29
- Object.keys(_stringifyJson).forEach(function (key) {
28
+ var _stringify = require("./stringify.cjs");
29
+ Object.keys(_stringify).forEach(function (key) {
30
30
  if (key === "default" || key === "__esModule") return;
31
- if (key in exports && exports[key] === _stringifyJson[key]) return;
31
+ if (key in exports && exports[key] === _stringify[key]) return;
32
32
  Object.defineProperty(exports, key, {
33
33
  enumerable: true,
34
34
  get: function () {
35
- return _stringifyJson[key];
35
+ return _stringify[key];
36
36
  }
37
37
  });
38
38
  });
@@ -1,4 +1,4 @@
1
1
  export * from "./code-frames";
2
2
  export * from "./parse-error";
3
- export * from "./stringify-json";
3
+ export * from "./stringify";
4
4
  export * from "./strip-comments";
@@ -1 +1 @@
1
- export*from"./code-frames";export*from"./parse-error";export*from"./stringify-json";export*from"./strip-comments";
1
+ export*from"./code-frames";export*from"./parse-error";export*from"./stringify";export*from"./strip-comments";
@@ -3,9 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.stringifyJson = void 0;
6
+ exports.stringify = void 0;
7
7
  var _isNumber = require("@stryke/types/type-checks/is-number");
8
- const stringifyJson = (r, t = 2) => {
8
+ const stringify = (r, t = 2) => {
9
9
  const n = (0, _isNumber.isNumber)(t) ? " ".repeat(t) : t;
10
10
  switch (r) {
11
11
  case null:
@@ -17,7 +17,7 @@ const stringifyJson = (r, t = 2) => {
17
17
  case !1:
18
18
  return "false";
19
19
  }
20
- if (Array.isArray(r)) return `[${n}${r.map(e => stringifyJson(e, n)).join(`,${n}`)}${n}]`;
20
+ if (Array.isArray(r)) return `[${n}${r.map(e => stringify(e, n)).join(`,${n}`)}${n}]`;
21
21
  if (r instanceof Uint8Array) return r.toString();
22
22
  switch (typeof r) {
23
23
  case "number":
@@ -27,10 +27,10 @@ const stringifyJson = (r, t = 2) => {
27
27
  case "object":
28
28
  {
29
29
  const e = Object.keys(r);
30
- return `{${n}${e.map(s => `${s}${n}=${n}${stringifyJson(r[s], n)}`).join(`,${n}`)}${n}}`;
30
+ return `{${n}${e.map(s => `${s}: ${n}${stringify(r[s], n)}`).join(`,${n}`)}${n}}`;
31
31
  }
32
32
  default:
33
33
  return "null";
34
34
  }
35
35
  };
36
- exports.stringifyJson = stringifyJson;
36
+ exports.stringify = stringify;
@@ -5,4 +5,4 @@
5
5
  * @param spacing - The spacing to use for the stringification
6
6
  * @returns The stringified value
7
7
  */
8
- export declare const stringifyJson: (value: unknown, spacing?: string | number) => string;
8
+ export declare const stringify: (value: unknown, spacing?: string | number) => string;
@@ -0,0 +1 @@
1
+ import{isNumber as i}from"@stryke/types/type-checks/is-number";export const stringify=(r,t=2)=>{const n=i(t)?" ".repeat(t):t;switch(r){case null:return"null";case void 0:return"undefined";case!0:return"true";case!1:return"false"}if(Array.isArray(r))return`[${n}${r.map(e=>stringify(e,n)).join(`,${n}`)}${n}]`;if(r instanceof Uint8Array)return r.toString();switch(typeof r){case"number":return`${r}`;case"string":return JSON.stringify(r);case"object":{const e=Object.keys(r);return`{${n}${e.map(s=>`${s}: ${n}${stringify(r[s],n)}`).join(`,${n}`)}${n}}`}default:return"null"}};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/json",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "description": "A package containing JSON parsing/stringify utilities used by Storm Software.",
6
6
  "repository": {
@@ -106,18 +106,18 @@
106
106
  "default": "./dist/utils/strip-comments.mjs"
107
107
  }
108
108
  },
109
- "./utils/stringify-json": {
109
+ "./utils/stringify": {
110
110
  "import": {
111
- "types": "./dist/utils/stringify-json.d.ts",
112
- "default": "./dist/utils/stringify-json.mjs"
111
+ "types": "./dist/utils/stringify.d.ts",
112
+ "default": "./dist/utils/stringify.mjs"
113
113
  },
114
114
  "require": {
115
- "types": "./dist/utils/stringify-json.d.ts",
116
- "default": "./dist/utils/stringify-json.cjs"
115
+ "types": "./dist/utils/stringify.d.ts",
116
+ "default": "./dist/utils/stringify.cjs"
117
117
  },
118
118
  "default": {
119
- "types": "./dist/utils/stringify-json.d.ts",
120
- "default": "./dist/utils/stringify-json.mjs"
119
+ "types": "./dist/utils/stringify.d.ts",
120
+ "default": "./dist/utils/stringify.mjs"
121
121
  }
122
122
  },
123
123
  "./utils/parse-error": {
@@ -1 +0,0 @@
1
- import{isNumber as i}from"@stryke/types/type-checks/is-number";export const stringifyJson=(r,t=2)=>{const n=i(t)?" ".repeat(t):t;switch(r){case null:return"null";case void 0:return"undefined";case!0:return"true";case!1:return"false"}if(Array.isArray(r))return`[${n}${r.map(e=>stringifyJson(e,n)).join(`,${n}`)}${n}]`;if(r instanceof Uint8Array)return r.toString();switch(typeof r){case"number":return`${r}`;case"string":return JSON.stringify(r);case"object":{const e=Object.keys(r);return`{${n}${e.map(s=>`${s}${n}=${n}${stringifyJson(r[s],n)}`).join(`,${n}`)}${n}}`}default:return"null"}};