j-toml icon indicating copy to clipboard operation
j-toml copied to clipboard

Can't Parse a Stringified example.

Open meyverick opened this issue 2 years ago • 1 comments

I try to stringify the readme example and then parse it and it returns an error. Am I doing it wrong?

Script

import TOML from "@ltd/j-toml";

const test = TOML.stringify({
	key: "value",
	dotted: {
		key: "value",
	},
	inlineTable: TOML.inline({ key: "value" }),
	mix: {
		key: "value",
		table: TOML.Section({
			key: "value",
		}),
	},
	table: TOML.Section({
		key: "value",
		table: TOML.Section({
			key: "value",
		}),
	}),
});

const rootTable = TOML.parse(test);

Returns

PS C:\Users\User Name\Files\My Project\app> node helpers/configFile.js
file:///C:/Users/User%20Name/Files/My%20Project/app/node_modules/@ltd/j-toml/index.mjs:2230
    if ( typeof sourcePath!=='string' ) { throw TypeError$1('TOML.parse(source.path)'); }
                                                                            ^

TypeError: TOML.parse(source.path)
    at parse (file:///C:/Users/User%20Name/Files/My%20Project/app/node_modules/@ltd/j-toml/index.mjs:2230:49)
    at Module.parse (file:///C:/Users/User%20Name/Files/My%20Project/app/node_modules/@ltd/j-toml/index.mjs:2293:6)
    at file:///C:/Users/User%20Name/Files/V%20Rising/app/helpers/configFile.js:38:24
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:409:24)
    at async loadESM (node:internal/process/esm_loader:85:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Node.js v17.9.0

meyverick avatar May 26 '22 20:05 meyverick

@meyverick Sorry for replying late. TOML.stringify returns an array of strings, if there is no options.newline, because the lib doesn't know use what to join lines.

See the docs:

  1. https://github.com/LongTengDao/j-toml/tree/master/docs/English#optionsnewline
  2. https://github.com/LongTengDao/j-toml/tree/master/docs/English#return-2

When you pass it into TOML.parse, it throws, because TOML.parse only accept string | Buffer | { path, data } currently.

I may (or may not) add this feature (parsing array of lines) in the future.

LongTengDao avatar Jun 09 '22 18:06 LongTengDao

To help users to notice that TOML.stringify WON'T return string while there is no option newline, currently I don't add the feature that supporting parsing array of lines, and I make it more friendly to notice that since v1.33.0.

When users widely know the options.newline, I will add the feature of parsing array; or I found users will never remember that, then I may remove the feature that TOML.stringify returning array.

LongTengDao avatar Aug 11 '22 07:08 LongTengDao