@scalar/snippetz 0.4.4 → 0.4.6

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.
@@ -1 +1 @@
1
- {"version":3,"file":"objectToString.d.ts","sourceRoot":"","sources":["../../src/utils/objectToString.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,QAAQ;IACA,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;CACjC;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,SAAI,GAAG,MAAM,CAkD3E"}
1
+ {"version":3,"file":"objectToString.d.ts","sourceRoot":"","sources":["../../src/utils/objectToString.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,qBAAa,QAAQ;IACA,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;CACjC;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,SAAI,GAAG,MAAM,CA2D3E"}
@@ -8,6 +8,9 @@ function objectToString(obj, indent = 0) {
8
8
  const parts = [];
9
9
  const indentation = " ".repeat(indent);
10
10
  const innerIndentation = " ".repeat(indent + 2);
11
+ if (Object.keys(obj).length === 0) {
12
+ return "{}";
13
+ }
11
14
  for (const [key, value] of Object.entries(obj)) {
12
15
  const formattedKey = needsQuotes(key) ? `'${key}'` : key;
13
16
  if (value instanceof Unquoted) {
@@ -23,16 +26,24 @@ function objectToString(obj, indent = 0) {
23
26
  }
24
27
  parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`);
25
28
  } else if (Array.isArray(value)) {
26
- const arrayString = value.map((item) => {
29
+ const items = value.map((item) => {
27
30
  if (typeof item === "string") {
28
31
  return `'${item}'`;
29
32
  }
30
33
  if (item && typeof item === "object") {
31
- return objectToString(item, indent + 2);
34
+ return objectToString(item);
32
35
  }
33
- return item;
34
- }).join(`, ${innerIndentation}`);
35
- parts.push(`${innerIndentation}${formattedKey}: [${arrayString}]`);
36
+ return JSON.stringify(item);
37
+ });
38
+ if (items.some((item) => item.includes("\n"))) {
39
+ const arrayString = items.map((item) => indentString(item, indent + 4)).join(`,
40
+ `);
41
+ parts.push(`${innerIndentation}${formattedKey}: [
42
+ ${arrayString}
43
+ ${innerIndentation}]`);
44
+ } else {
45
+ parts.push(`${innerIndentation}${formattedKey}: [${items.join(", ")}]`);
46
+ }
36
47
  } else if (value && typeof value === "object") {
37
48
  parts.push(`${innerIndentation}${formattedKey}: ${objectToString(value, indent + 2)}`);
38
49
  } else if (typeof value === "string") {
@@ -46,6 +57,10 @@ function objectToString(obj, indent = 0) {
46
57
  ${parts.join(",\n")}
47
58
  ${indentation}}`;
48
59
  }
60
+ function indentString(str, indent) {
61
+ const indentation = " ".repeat(indent);
62
+ return str.split("\n").map((line) => `${indentation}${line}`).join("\n");
63
+ }
49
64
  export {
50
65
  Unquoted,
51
66
  objectToString
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/utils/objectToString.ts"],
4
- "sourcesContent": ["import { needsQuotes } from './needsQuotes'\n\n/**\n * Represents a raw code that should not be quoted, e.g. `JSON.stringify(...)`.\n * If consists of multiple lines, they will be indented properly.\n */\nexport class Unquoted {\n constructor(public value: string) {}\n}\n\n/**\n * Converts an object into a string representation with proper formatting and indentation\n *\n * Handles nested objects, arrays, and special string values\n */\nexport function objectToString(obj: Record<string, any>, indent = 0): string {\n const parts = []\n const indentation = ' '.repeat(indent)\n const innerIndentation = ' '.repeat(indent + 2)\n\n for (const [key, value] of Object.entries(obj)) {\n const formattedKey = needsQuotes(key) ? `'${key}'` : key\n\n if (value instanceof Unquoted) {\n const lines = value.value.split('\\n')\n let formattedValue = `${value.value}`\n\n if (lines.length > 1) {\n formattedValue = lines\n .map((line, index) => {\n if (index === 0) {\n return line\n }\n\n return `${innerIndentation}${line}`\n })\n .join('\\n')\n }\n\n parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`)\n } else if (Array.isArray(value)) {\n const arrayString = value\n .map((item) => {\n if (typeof item === 'string') {\n return `'${item}'`\n }\n if (item && typeof item === 'object') {\n return objectToString(item, indent + 2)\n }\n return item\n })\n .join(`, ${innerIndentation}`)\n parts.push(`${innerIndentation}${formattedKey}: [${arrayString}]`)\n } else if (value && typeof value === 'object') {\n parts.push(`${innerIndentation}${formattedKey}: ${objectToString(value, indent + 2)}`)\n } else if (typeof value === 'string') {\n const formattedValue = `'${value}'`\n\n parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`)\n } else {\n parts.push(`${innerIndentation}${formattedKey}: ${value}`)\n }\n }\n\n return `{\\n${parts.join(',\\n')}\\n${indentation}}`\n}\n"],
5
- "mappings": "AAAA,SAAS,mBAAmB;AAMrB,MAAM,SAAS;AAAA,EACpB,YAAmB,OAAe;AAAf;AAAA,EAAgB;AACrC;AAOO,SAAS,eAAe,KAA0B,SAAS,GAAW;AAC3E,QAAM,QAAQ,CAAC;AACf,QAAM,cAAc,IAAI,OAAO,MAAM;AACrC,QAAM,mBAAmB,IAAI,OAAO,SAAS,CAAC;AAE9C,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,GAAG,GAAG;AAC9C,UAAM,eAAe,YAAY,GAAG,IAAI,IAAI,GAAG,MAAM;AAErD,QAAI,iBAAiB,UAAU;AAC7B,YAAM,QAAQ,MAAM,MAAM,MAAM,IAAI;AACpC,UAAI,iBAAiB,GAAG,MAAM,KAAK;AAEnC,UAAI,MAAM,SAAS,GAAG;AACpB,yBAAiB,MACd,IAAI,CAAC,MAAM,UAAU;AACpB,cAAI,UAAU,GAAG;AACf,mBAAO;AAAA,UACT;AAEA,iBAAO,GAAG,gBAAgB,GAAG,IAAI;AAAA,QACnC,CAAC,EACA,KAAK,IAAI;AAAA,MACd;AAEA,YAAM,KAAK,GAAG,gBAAgB,GAAG,YAAY,KAAK,cAAc,EAAE;AAAA,IACpE,WAAW,MAAM,QAAQ,KAAK,GAAG;AAC/B,YAAM,cAAc,MACjB,IAAI,CAAC,SAAS;AACb,YAAI,OAAO,SAAS,UAAU;AAC5B,iBAAO,IAAI,IAAI;AAAA,QACjB;AACA,YAAI,QAAQ,OAAO,SAAS,UAAU;AACpC,iBAAO,eAAe,MAAM,SAAS,CAAC;AAAA,QACxC;AACA,eAAO;AAAA,MACT,CAAC,EACA,KAAK,KAAK,gBAAgB,EAAE;AAC/B,YAAM,KAAK,GAAG,gBAAgB,GAAG,YAAY,MAAM,WAAW,GAAG;AAAA,IACnE,WAAW,SAAS,OAAO,UAAU,UAAU;AAC7C,YAAM,KAAK,GAAG,gBAAgB,GAAG,YAAY,KAAK,eAAe,OAAO,SAAS,CAAC,CAAC,EAAE;AAAA,IACvF,WAAW,OAAO,UAAU,UAAU;AACpC,YAAM,iBAAiB,IAAI,KAAK;AAEhC,YAAM,KAAK,GAAG,gBAAgB,GAAG,YAAY,KAAK,cAAc,EAAE;AAAA,IACpE,OAAO;AACL,YAAM,KAAK,GAAG,gBAAgB,GAAG,YAAY,KAAK,KAAK,EAAE;AAAA,IAC3D;AAAA,EACF;AAEA,SAAO;AAAA,EAAM,MAAM,KAAK,KAAK,CAAC;AAAA,EAAK,WAAW;AAChD;",
4
+ "sourcesContent": ["import { needsQuotes } from './needsQuotes'\n\n/**\n * Represents a raw code that should not be quoted, e.g. `JSON.stringify(...)`.\n * If consists of multiple lines, they will be indented properly.\n */\nexport class Unquoted {\n constructor(public value: string) {}\n}\n\n/**\n * Converts an object into a string representation with proper formatting and indentation\n *\n * Handles nested objects, arrays, and special string values\n */\nexport function objectToString(obj: Record<string, any>, indent = 0): string {\n const parts = []\n const indentation = ' '.repeat(indent)\n const innerIndentation = ' '.repeat(indent + 2)\n\n if (Object.keys(obj).length === 0) {\n return '{}'\n }\n\n for (const [key, value] of Object.entries(obj)) {\n const formattedKey = needsQuotes(key) ? `'${key}'` : key\n\n if (value instanceof Unquoted) {\n const lines = value.value.split('\\n')\n let formattedValue = `${value.value}`\n\n if (lines.length > 1) {\n formattedValue = lines\n .map((line, index) => {\n if (index === 0) {\n return line\n }\n\n return `${innerIndentation}${line}`\n })\n .join('\\n')\n }\n\n parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`)\n } else if (Array.isArray(value)) {\n const items = value.map((item) => {\n if (typeof item === 'string') {\n return `'${item}'`\n }\n if (item && typeof item === 'object') {\n return objectToString(item)\n }\n return JSON.stringify(item)\n })\n\n if (items.some((item) => item.includes('\\n'))) {\n // format vertically if any array element contains a newline\n const arrayString = items.map((item) => indentString(item, indent + 4)).join(`,\\n`)\n parts.push(`${innerIndentation}${formattedKey}: [\\n${arrayString}\\n${innerIndentation}]`)\n } else {\n parts.push(`${innerIndentation}${formattedKey}: [${items.join(', ')}]`)\n }\n } else if (value && typeof value === 'object') {\n parts.push(`${innerIndentation}${formattedKey}: ${objectToString(value, indent + 2)}`)\n } else if (typeof value === 'string') {\n const formattedValue = `'${value}'`\n\n parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`)\n } else {\n parts.push(`${innerIndentation}${formattedKey}: ${value}`)\n }\n }\n\n return `{\\n${parts.join(',\\n')}\\n${indentation}}`\n}\n\nfunction indentString(str: string, indent: number) {\n const indentation = ' '.repeat(indent)\n return str\n .split('\\n')\n .map((line) => `${indentation}${line}`)\n .join('\\n')\n}\n"],
5
+ "mappings": "AAAA,SAAS,mBAAmB;AAMrB,MAAM,SAAS;AAAA,EACpB,YAAmB,OAAe;AAAf;AAAA,EAAgB;AACrC;AAOO,SAAS,eAAe,KAA0B,SAAS,GAAW;AAC3E,QAAM,QAAQ,CAAC;AACf,QAAM,cAAc,IAAI,OAAO,MAAM;AACrC,QAAM,mBAAmB,IAAI,OAAO,SAAS,CAAC;AAE9C,MAAI,OAAO,KAAK,GAAG,EAAE,WAAW,GAAG;AACjC,WAAO;AAAA,EACT;AAEA,aAAW,CAAC,KAAK,KAAK,KA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E;AAAA,IACvF,WAAW,OAAO,UAAU,UAAU;AACpC,YAAM,iBAAiB,IAAI,KAAK;AAEhC,YAAM,KAAK,GAAG,gBAAgB,GAAG,YAAY,KAAK,cAAc,EAAE;AAAA,IACpE,OAAO;AACL,YAAM,KAAK,GAAG,gBAAgB,GAAG,YAAY,KAAK,KAAK,EAAE;AAAA,IAC3D;AAAA,EACF;AAEA,SAAO;AAAA,EAAM,MAAM,KAAK,KAAK,CAAC;AAAA,EAAK,WAAW;AAChD;AAEA,SAAS,aAAa,KAAa,QAAgB;AACjD,QAAM,cAAc,IAAI,OAAO,MAAM;AACrC,SAAO,IACJ,MAAM,IAAI,EACV,IAAI,CAAC,SAAS,GAAG,WAAW,GAAG,IAAI,EAAE,EACrC,KAAK,IAAI;AACd;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "url": "git+https://github.com/scalar/scalar.git",
10
10
  "directory": "packages/snippetz"
11
11
  },
12
- "version": "0.4.4",
12
+ "version": "0.4.6",
13
13
  "engines": {
14
14
  "node": ">=20"
15
15
  },
@@ -219,11 +219,11 @@
219
219
  "module": "./dist/index.js",
220
220
  "dependencies": {
221
221
  "stringify-object": "^5.0.0",
222
- "@scalar/types": "0.2.11"
222
+ "@scalar/types": "0.2.12"
223
223
  },
224
224
  "devDependencies": {
225
225
  "vite": "6.1.6",
226
- "@scalar/build-tooling": "0.2.4"
226
+ "@scalar/build-tooling": "0.2.6"
227
227
  },
228
228
  "scripts": {
229
229
  "build": "scalar-build-esbuild",