smol-toml 1.4.1 → 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
@@ -69,6 +69,12 @@ ${codeblock}`, options);
69
69
  };
70
70
 
71
71
  // dist/util.js
72
+ function isEscaped(str, ptr) {
73
+ let i = 0;
74
+ while (str[ptr - ++i] === "\\")
75
+ ;
76
+ return --i && i % 2;
77
+ }
72
78
  function indexOfNewline(str, start = 0, end = str.length) {
73
79
  let idx = str.indexOf("\n", start);
74
80
  if (str[idx - 1] === "\r")
@@ -123,7 +129,7 @@ function getStringEnd(str, seek) {
123
129
  seek += target.length - 1;
124
130
  do
125
131
  seek = str.indexOf(target, ++seek);
126
- while (seek > -1 && first !== "'" && str[seek - 1] === "\\" && (str[seek - 2] !== "\\" || str[seek - 3] === "\\"));
132
+ while (seek > -1 && first !== "'" && isEscaped(str, seek));
127
133
  if (seek > -1) {
128
134
  seek += target.length;
129
135
  if (target.length > 1) {
@@ -816,12 +822,12 @@ function stringifyArrayTable(array, key, depth, numberAsFloat) {
816
822
  for (let i = 0; i < array.length; i++) {
817
823
  res += `[[${key}]]
818
824
  `;
819
- res += stringifyTable(array[i], key, depth, numberAsFloat);
820
- res += "\n\n";
825
+ res += stringifyTable(0, array[i], key, depth, numberAsFloat);
826
+ res += "\n";
821
827
  }
822
828
  return res;
823
829
  }
824
- function stringifyTable(obj, prefix, depth, numberAsFloat) {
830
+ function stringifyTable(tableKey, obj, prefix, depth, numberAsFloat) {
825
831
  if (depth === 0) {
826
832
  throw new Error("Could not stringify the object: maximum object depth exceeded");
827
833
  }
@@ -840,10 +846,9 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
840
846
  tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1, numberAsFloat);
841
847
  } else if (type === "object") {
842
848
  let tblKey = prefix ? `${prefix}.${key}` : key;
843
- tables += `[${tblKey}]
844
- `;
845
- tables += stringifyTable(obj[k], tblKey, depth - 1, numberAsFloat);
846
- tables += "\n\n";
849
+ let table = stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat);
850
+ if (table)
851
+ tables += table + "\n";
847
852
  } else {
848
853
  preamble += key;
849
854
  preamble += " = ";
@@ -852,14 +857,17 @@ function stringifyTable(obj, prefix, depth, numberAsFloat) {
852
857
  }
853
858
  }
854
859
  }
855
- return `${preamble}
856
- ${tables}`.trim();
860
+ if (tableKey && (preamble || !tables))
861
+ preamble = preamble ? `[${tableKey}]
862
+ ${preamble}` : `[${tableKey}]`;
863
+ return preamble && tables ? `${preamble}
864
+ ${tables}` : preamble || tables;
857
865
  }
858
866
  function stringify(obj, { maxDepth = 1e3, numbersAsFloat = false } = {}) {
859
867
  if (extendedTypeOf(obj) !== "object") {
860
868
  throw new TypeError("stringify can only be called with an object");
861
869
  }
862
- return stringifyTable(obj, "", maxDepth, numbersAsFloat);
870
+ return stringifyTable(0, obj, "", maxDepth, numbersAsFloat);
863
871
  }
864
872
 
865
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/dist/util.js CHANGED
@@ -26,6 +26,12 @@
26
26
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
  import { TomlError } from './error.js';
29
+ function isEscaped(str, ptr) {
30
+ let i = 0;
31
+ while (str[ptr - ++i] === '\\')
32
+ ;
33
+ return --i && (i % 2);
34
+ }
29
35
  export function indexOfNewline(str, start = 0, end = str.length) {
30
36
  let idx = str.indexOf('\n', start);
31
37
  if (str[idx - 1] === '\r')
@@ -86,7 +92,7 @@ export function getStringEnd(str, seek) {
86
92
  seek += target.length - 1;
87
93
  do
88
94
  seek = str.indexOf(target, ++seek);
89
- while (seek > -1 && first !== "'" && str[seek - 1] === '\\' && (str[seek - 2] !== '\\' || str[seek - 3] === '\\'));
95
+ while (seek > -1 && first !== "'" && isEscaped(str, seek));
90
96
  if (seek > -1) {
91
97
  seek += target.length;
92
98
  if (target.length > 1) {
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.1",
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",