ezze / node-perfect-json

Utility function to beautify JSON string...like JSON.stringify() but better
MIT License
0 stars 0 forks source link

Various bugs #1

Open theBowja opened 8 months ago

theBowja commented 8 months ago
  1. Properties with undefined values should be removed from the output.
  2. Strings with \n newline characters aren't handled properly.
  3. Support tab indent?
const perfectJson = require('perfect-json');
const data = {
  name: 'Dmitriy',
  surname: 'Pushkov',
  asdf: undefined,
  skills: ['Java\nScript', 'Node.js', 'ES6'],
  env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
};

console.log(perfectJson(data, {
  singleLine: ({ key }) => {
    return ['skills', 'env'].includes(key);
  }
}));

Actual (improper json):

{
  "name": "Dmitriy",
  "surname": "Pushkov",
  "asdf": undefined,
  "skills": ["Java
Script", "Node.js", "ES6"],
  "env": { "node": "14.0.0", "eslint": true, "babel": true, "typescript": false }
}

Expected:

{
  "name": "Dmitriy",
  "surname": "Pushkov",
  "skills": ["Java\nScript", "Node.js", "ES6"],
  "env": { "node": "14.0.0", "eslint": true, "babel": true, "typescript": false }
}
theBowja commented 8 months ago

fix for newline and undefined: https://gist.github.com/theBowja/5d8f0fc80da350945a4703527745dcf8/revisions

theBowja commented 8 months ago

also need to take into account Date and BigInt

    if (item instanceof Date) {
      return JSON.stringify(item);
    }
    if (typeof item === 'bigint') {
      return JSON.stringify(item);
    }