smol-toml 1.4.2 → 1.5.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.cjs CHANGED
@@ -822,12 +822,12 @@ function stringifyArrayTable(array, key, depth, numberAsFloat) {
822
822
  for (let i = 0; i < array.length; i++) {
823
823
  res += `[[${key}]]
824
824
  `;
825
- res += stringifyTable(array[i], key, depth, numberAsFloat);
826
- res += "\n\n";
825
+ res += stringifyTable(0, array[i], key, depth, numberAsFloat);
826
+ res += "\n";
827
827
  }
828
828
  return res;
829
829
  }
830
- function stringifyTable(obj, prefix, depth, numberAsFloat) {
830
+ function stringifyTable(tableKey, obj, prefix, depth, numberAsFloat) {
831
831
  if (depth === 0) {
832
832
  throw new Error("Could not stringify the object: maximum object depth exceeded");
833
833
  }
@@ -846,10 +846,9 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
846
846
  tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1, numberAsFloat);
847
847
  } else if (type === "object") {
848
848
  let tblKey = prefix ? `${prefix}.${key}` : key;
849
- tables += `[${tblKey}]
850
- `;
851
- tables += stringifyTable(obj[k], tblKey, depth - 1, numberAsFloat);
852
- tables += "\n\n";
849
+ let table = stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat);
850
+ if (table)
851
+ tables += table + "\n";
853
852
  } else {
854
853
  preamble += key;
855
854
  preamble += " = ";
@@ -858,14 +857,17 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
858
857
  }
859
858
  }
860
859
  }
861
- return `${preamble}
862
- ${tables}`.trim();
860
+ if (tableKey && (preamble || !tables))
861
+ preamble = preamble ? `[${tableKey}]
862
+ ${preamble}` : `[${tableKey}]`;
863
+ return preamble && tables ? `${preamble}
864
+ ${tables}` : preamble || tables;
863
865
  }
864
866
  function stringify(obj, { maxDepth = 1e3, numbersAsFloat = false } = {}) {
865
867
  if (extendedTypeOf(obj) !== "object") {
866
868
  throw new TypeError("stringify can only be called with an object");
867
869
  }
868
- return stringifyTable(obj, "", maxDepth, numbersAsFloat);
870
+ return stringifyTable(0, obj, "", maxDepth, numbersAsFloat);
869
871
  }
870
872
 
871
873
  // dist/index.js
package/dist/stringify.js CHANGED
@@ -116,12 +116,12 @@ function stringifyArrayTable(array, key, depth, numberAsFloat) {
116
116
  let res = '';
117
117
  for (let i = 0; i < array.length; i++) {
118
118
  res += `[[${key}]]\n`;
119
- res += stringifyTable(array[i], key, depth, numberAsFloat);
120
- res += '\n\n';
119
+ res += stringifyTable(0, array[i], key, depth, numberAsFloat);
120
+ res += '\n';
121
121
  }
122
122
  return res;
123
123
  }
124
- function stringifyTable(obj, prefix, depth, numberAsFloat) {
124
+ function stringifyTable(tableKey, obj, prefix, depth, numberAsFloat) {
125
125
  if (depth === 0) {
126
126
  throw new Error('Could not stringify the object: maximum object depth exceeded');
127
127
  }
@@ -141,9 +141,9 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
141
141
  }
142
142
  else if (type === 'object') {
143
143
  let tblKey = prefix ? `${prefix}.${key}` : key;
144
- tables += `[${tblKey}]\n`;
145
- tables += stringifyTable(obj[k], tblKey, depth - 1, numberAsFloat);
146
- tables += '\n\n';
144
+ let table = stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat);
145
+ if (table)
146
+ tables += table + '\n';
147
147
  }
148
148
  else {
149
149
  preamble += key;
@@ -153,11 +153,15 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
153
153
  }
154
154
  }
155
155
  }
156
- return `${preamble}\n${tables}`.trim();
156
+ if (tableKey && (preamble || !tables)) // Create table only if necessary
157
+ preamble = preamble ? `[${tableKey}]\n${preamble}` : `[${tableKey}]`;
158
+ return preamble && tables
159
+ ? `${preamble}\n${tables}`
160
+ : preamble || tables;
157
161
  }
158
162
  export function stringify(obj, { maxDepth = 1000, numbersAsFloat = false } = {}) {
159
163
  if (extendedTypeOf(obj) !== 'object') {
160
164
  throw new TypeError('stringify can only be called with an object');
161
165
  }
162
- return stringifyTable(obj, '', maxDepth, numbersAsFloat);
166
+ return stringifyTable(0, obj, '', maxDepth, numbersAsFloat);
163
167
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "smol-toml",
3
3
  "license": "BSD-3-Clause",
4
- "version": "1.4.2",
4
+ "version": "1.5.0",
5
5
  "description": "A small, fast, and correct TOML parser/serializer",
6
6
  "author": "Cynthia <cyyynthia@borkenware.com>",
7
7
  "repository": "github:squirrelchat/smol-toml",
@@ -19,15 +19,15 @@
19
19
  "devDependencies": {
20
20
  "@iarna/toml": "3.0.0",
21
21
  "@ltd/j-toml": "^1.38.0",
22
- "@tsconfig/node-lts": "^22.0.2",
23
- "@tsconfig/strictest": "^2.0.5",
24
- "@types/node": "^24.0.7",
25
- "@vitest/ui": "^3.2.4",
26
- "esbuild": "^0.25.5",
22
+ "@tsconfig/node-lts": "^22.0.3",
23
+ "@tsconfig/strictest": "^2.0.8",
24
+ "@types/node": "^24.10.1",
25
+ "@vitest/ui": "^4.0.8",
26
+ "esbuild": "^0.27.0",
27
27
  "fast-toml": "^0.5.4",
28
28
  "pin-github-action": "^3.4.0",
29
- "typescript": "^5.8.3",
30
- "vitest": "^3.2.4"
29
+ "typescript": "^5.9.3",
30
+ "vitest": "^4.0.8"
31
31
  },
32
32
  "main": "./dist/index.cjs",
33
33
  "module": "./dist/index.js",