hjson-js icon indicating copy to clipboard operation
hjson-js copied to clipboard

delete object property

Open MartinMuzatko opened this issue 7 years ago • 1 comments

I would like to be able to delete an object property via the delete keyword:

image

When I do that with hjson, the property is set to undefined.

See example

const path = require('path')
const promisify = require('promisify-node')
const fs = promisify('fs')
const hjson = require('hjson')

const CONFIGFILE = path.resolve(__dirname, '../serverconfig.hjson')

async function get() {
    const file = await fs.readFile(CONFIGFILE)
    return hjson.rt.parse(file.toString())
}

async function set(config) {
    const json = hjson.rt.stringify(config, {
        quotes: 'strings',
        space: 4, // can also use \t for tabs
        bracesSameLine: true,
    })
    await fs.writeFile(CONFIGFILE, json)
}


(async () => {

    var x = await get()
    delete x.example
    await set(x)
    // hjson file still contains `example` property
})()

MartinMuzatko avatar Nov 24 '17 16:11 MartinMuzatko

I don't have time to look at it now but I guess it's a bug with round-tripping the comments. If you don't need that feature you can use hjson.stringify().

laktak avatar Dec 12 '17 21:12 laktak