smol-toml 1.4.2 → 1.5.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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  [![TOML 1.0.0](https://img.shields.io/badge/TOML-1.0.0-9c4221?style=flat-square)](https://toml.io/en/v1.0.0)
3
3
  [![License](https://img.shields.io/github/license/squirrelchat/smol-toml.svg?style=flat-square)](https://github.com/squirrelchat/smol-toml/blob/mistress/LICENSE)
4
4
  [![npm](https://img.shields.io/npm/v/smol-toml?style=flat-square)](https://npm.im/smol-toml)
5
- [![Build](https://img.shields.io/github/actions/workflow/status/squirrelchat/smol-toml/build.yml?style=flat-square&logo=github)](https://github.com/squirrelchat/smol-toml/actions/workflows/build.yml)
5
+ [![Build](https://img.shields.io/github/actions/workflow/status/squirrelchat/smol-toml/build.yaml?style=flat-square&logo=github)](https://github.com/squirrelchat/smol-toml/actions/workflows/build.yaml)
6
6
 
7
7
  [![GitHub Sponsors](https://img.shields.io/badge/GitHub%20Sponsors-support%20me-EA4AAA?style=flat-square)](https://github.com/sponsors/cyyynthia)
8
8
  [![Weekly downloads](https://img.shields.io/npm/dw/smol-toml?style=flat-square)](https://npm.im/smol-toml)
package/dist/index.cjs CHANGED
@@ -820,14 +820,13 @@ function stringifyArrayTable(array, key, depth, numberAsFloat) {
820
820
  }
821
821
  let res = "";
822
822
  for (let i = 0; i < array.length; i++) {
823
- res += `[[${key}]]
823
+ res += `${res && "\n"}[[${key}]]
824
824
  `;
825
- res += stringifyTable(array[i], key, depth, numberAsFloat);
826
- res += "\n\n";
825
+ res += stringifyTable(0, array[i], key, depth, numberAsFloat);
827
826
  }
828
827
  return res;
829
828
  }
830
- function stringifyTable(obj, prefix, depth, numberAsFloat) {
829
+ function stringifyTable(tableKey, obj, prefix, depth, numberAsFloat) {
831
830
  if (depth === 0) {
832
831
  throw new Error("Could not stringify the object: maximum object depth exceeded");
833
832
  }
@@ -846,10 +845,8 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
846
845
  tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1, numberAsFloat);
847
846
  } else if (type === "object") {
848
847
  let tblKey = prefix ? `${prefix}.${key}` : key;
849
- tables += `[${tblKey}]
850
- `;
851
- tables += stringifyTable(obj[k], tblKey, depth - 1, numberAsFloat);
852
- tables += "\n\n";
848
+ let table = stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat);
849
+ tables += (tables && "\n") + table;
853
850
  } else {
854
851
  preamble += key;
855
852
  preamble += " = ";
@@ -858,14 +855,20 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
858
855
  }
859
856
  }
860
857
  }
861
- return `${preamble}
862
- ${tables}`.trim();
858
+ if (tableKey && (preamble || !tables))
859
+ preamble = preamble ? `[${tableKey}]
860
+ ${preamble}` : `[${tableKey}]`;
861
+ return preamble && tables ? `${preamble}
862
+ ${tables}` : preamble || tables;
863
863
  }
864
864
  function stringify(obj, { maxDepth = 1e3, numbersAsFloat = false } = {}) {
865
865
  if (extendedTypeOf(obj) !== "object") {
866
866
  throw new TypeError("stringify can only be called with an object");
867
867
  }
868
- return stringifyTable(obj, "", maxDepth, numbersAsFloat);
868
+ let str = stringifyTable(0, obj, "", maxDepth, numbersAsFloat);
869
+ if (str[str.length - 1] !== "\n")
870
+ return str + "\n";
871
+ return str;
869
872
  }
870
873
 
871
874
  // dist/index.js
package/dist/stringify.js CHANGED
@@ -115,13 +115,12 @@ function stringifyArrayTable(array, key, depth, numberAsFloat) {
115
115
  }
116
116
  let res = '';
117
117
  for (let i = 0; i < array.length; i++) {
118
- res += `[[${key}]]\n`;
119
- res += stringifyTable(array[i], key, depth, numberAsFloat);
120
- res += '\n\n';
118
+ res += `${res && '\n'}[[${key}]]\n`;
119
+ res += stringifyTable(0, array[i], key, depth, numberAsFloat);
121
120
  }
122
121
  return res;
123
122
  }
124
- function stringifyTable(obj, prefix, depth, numberAsFloat) {
123
+ function stringifyTable(tableKey, obj, prefix, depth, numberAsFloat) {
125
124
  if (depth === 0) {
126
125
  throw new Error('Could not stringify the object: maximum object depth exceeded');
127
126
  }
@@ -141,9 +140,8 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
141
140
  }
142
141
  else if (type === 'object') {
143
142
  let tblKey = prefix ? `${prefix}.${key}` : key;
144
- tables += `[${tblKey}]\n`;
145
- tables += stringifyTable(obj[k], tblKey, depth - 1, numberAsFloat);
146
- tables += '\n\n';
143
+ let table = stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat);
144
+ tables += (tables && '\n') + table;
147
145
  }
148
146
  else {
149
147
  preamble += key;
@@ -153,11 +151,18 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
153
151
  }
154
152
  }
155
153
  }
156
- return `${preamble}\n${tables}`.trim();
154
+ if (tableKey && (preamble || !tables)) // Create table only if necessary
155
+ preamble = preamble ? `[${tableKey}]\n${preamble}` : `[${tableKey}]`;
156
+ return preamble && tables
157
+ ? `${preamble}\n${tables}`
158
+ : preamble || tables;
157
159
  }
158
160
  export function stringify(obj, { maxDepth = 1000, numbersAsFloat = false } = {}) {
159
161
  if (extendedTypeOf(obj) !== 'object') {
160
162
  throw new TypeError('stringify can only be called with an object');
161
163
  }
162
- return stringifyTable(obj, '', maxDepth, numbersAsFloat);
164
+ let str = stringifyTable(0, obj, '', maxDepth, numbersAsFloat);
165
+ if (str[str.length - 1] !== '\n')
166
+ return str + '\n';
167
+ return str;
163
168
  }
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.1",
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",