LongTengDao / j-toml

A Node.js implementation of TOML written by LongTengDao. Belong to "Plan J"./龙腾道为汤小明语写的 Node.js 实现。从属于“简计划”。
https://npmjs.com/package/@ltd/j-toml
GNU Lesser General Public License v3.0
55 stars 6 forks source link

Can't Parse a Stringified example. #21

Closed meyverick closed 2 years ago

meyverick commented 2 years ago

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
LongTengDao commented 2 years ago

@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 commented 2 years ago

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.