Coder Social home page Coder Social logo

strip-json-comments's Introduction

strip-json-comments

Strip comments from JSON. Lets you use comments in your JSON files!

This is now possible:

{
	// Rainbows
	"unicorn": /* ❤ */ "cake"
}

It will replace single-line comments // and multi-line comments /**/ with whitespace. This allows JSON error positions to remain as close as possible to the original source.

Also available as a Gulp/Grunt/Broccoli plugin.

Install

npm install strip-json-comments

Usage

import stripJsonComments from 'strip-json-comments';

const json = `{
	// Rainbows
	"unicorn": /* ❤ */ "cake"
}`;

JSON.parse(stripJsonComments(json));
//=> {unicorn: 'cake'}

API

stripJsonComments(jsonString, options?)

jsonString

Type: string

Accepts a string with JSON and returns a string without comments.

options

Type: object

trailingCommas

Type: boolean
Default: false

Strip trailing commas in addition to comments.

whitespace

Type: boolean
Default: true

Replace comments and trailing commas with whitespace instead of stripping them entirely.

Benchmark

npm run bench

Related

strip-json-comments's People

Contributors

azerum avatar bendingbender avatar blakeembrey avatar c0ldf0x avatar chrisregnier avatar dtinth avatar edwardbetts avatar joshuajaco avatar kevva avatar lydell avatar novacrazy avatar papb avatar richienb avatar sebmck avatar sindresorhus avatar slauyama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

strip-json-comments's Issues

ERR_REQUIRE_ESM

updating to node v16 where ES6 modules are supported, I got the following error caused by strip-json-comments

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/strip-json-comments/index.js from jest.config.ts not supported.

Instead change the require of index.js in jest.config.ts to a dynamic import() which is available in all CommonJS modules.

    at readConfigFileAndSetRootDir (node_modules/jest-config/build/readConfigFileAndSetRootDir.js:118:13)
    at async readConfig (node_modules/jest-config/build/index.js:233:18)
    at async readConfigs (node_modules/jest-config/build/index.js:420:26)
    at async runCLI (node_modules/@jest/core/build/cli/index.js:132:59)
    at async Object.run (node_modules/jest-cli/build/cli/index.js:155:37)

jest.config.ts

import stripJsonComments from 'strip-json-comments';

Include LICENSE file

I'm in a corporate environment and would love to use this package, but cannot due to the lack of a LICENSE file. Please consider adding one containing the license text and your copyright?

Replace comments with whitespace

Just a thought I got from TypeScript actually, but what about replacing the comments with whitespace so that tooling will report the correct error positions?

You can try this

{  
  "connect_timeout": 30,
    //"keepalive": 120,
    //"ftp_passive_mode": true,
    //"ftp_obey_passive_host": false,
    //"ssh_key_file": "~/.ssh/id_rsa",
    //"sftp_flags": ["-F", "/path/to/ssh_config"],

    //"preserve_modification_times": false,
    //"remote_time_offset_in_hours": 0,
    "remote_encoding": "GBK",
    //"remote_locale": "C",
    //"allow_config_upload": false,
}

result

{
    "connect_timeout": 30,








    "remote_encoding": "GBK",


}

at last have a comma。after"GBK"

keep specific comments

This gulp-plugin is awesome, it solves the problem that my sass needs to remove personal comments, but can I ask what should I do if I want to keep specific annotations,thanks!

REST API

Not everyone likes CLI or can install it. Create a simple REST endpoint for stripping JSON files.

require no longer works with strip-json-comments

I have had this line in my project for at least the last 4 years:

var stripJsonComments = require('strip-json-comments');

your module from npm now triggers an error where it didn't before. I cannot use an import statement to amend this.

If you consider this module production ready, you should fix this code regression problem

Thanks

the package is not compatible with ES6

require() of ES Module node_modules/strip-json-comments/index.js not supported.
Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules

minimal repo

version: 4.0.0
tested on node v14 & v16 LTS

Error on install

On install show this error:
npm WARN tar ENOENT: no such file or directory, open '/home/ionic-app/node_modules/.staging/ajv-0fc645a4/dist/ajv.min.js.map'
npm ERR! path /home/ionic-app/node_modules/.staging/fined-2cea2c09/node_modules/tar
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename
npm ERR! enoent ENOENT: no such file or directory, rename '/home/ionic-app/node_modules/.staging/fined-2cea2c09/node_modules/tar' -> '/home/ionic-app/node_modules/.staging/tar-1bf39489'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /home/.npm/_logs/2018-05-27T23_03_51_754Z-debug.log

Breaks Regular Expressions

Examples of valid RegExp that gets broken:

// thinks it is a text opening:
/'/
/"/
/\`/
// thinks it is a single-line comment opening:
/\//
// thinks it is a multi-line comment opening:
/\/*/

How to Strip out this type of response?

Basically it is done with status code embedded outside of the JSON, so how to remove 200 or whatever statuscode before processing JSON:

200

{
"records": [
{
"number": "CHG0XXXXX",
"ticket_url": "https://myurl.com/nav_to.do?uri=change_request.do?sysparm_query=number%3DCHGXXXX",
"u_change_type": "Routine",
},
{
"number": "CHG0XXXXX",
"ticket_url": "https://myurl.com/nav_to.do?uri=change_request.do?sysparm_query=number%3DCHGXXXX",
"u_change_type": "Routine",
}]
}

Broken test case file

i could see some unit test cases in test.js but there is one object "it" which has no reference at all?
Can you please update the unit case cases?

update the document

as the author of this package doesn't want to make it hybrid, the document should be updated to indicate the correct version that should be used based on the module type.

if your package is a commonjs module, you should use strip-json-comments < v4.0.0 or use jsonminify
if you want to upgrade to >v4.0.0 you should swich your project to esm

support commonjs modules

not all modulaes that use strip-json-comments are migrated to esm, so you will need to support commonjs modules

this supporting is very easy
in your package.json

export:{
  import: './index.mjs',
  require: './index.cjs'
}

also, you will need to mention this breaking change in your docs, and confirm at witch vision of strip-json-comments you migrated to esm, so (as a temporary workaround) other packages can downgrade to the older version of strip-json-comments

Stripping whitespace

Whitespace is not removed around the removed comment. For example, when starting with this JSON

/* first comment */
{
  // this is comment
  "a": "b" // inline comment
  // this has comments on
  // two lines
  "c": "d"
  // another comment
  "e": "f"   // <- extra spaces in front
  /* last comment*/
}

Applying this code with whitespace: false shows this result (spaces were replaced with a middot & carriage returns indicated by ):


{
··↵
··"a":·"b"·↵
··↵
··↵
··"c":·"d"
··↵
··"e":·"f"···↵
··↵
}

It should throw an error if the argument passed to the function is not a string

Hi,
thanks for your tiny and useful module. I'd recommend throwing an error if the argument passed to the exported function is not a string. For example, considering the following code:

const fileContent = await readFile(jsonFilePath)
console.log(JSON.parse(stripComments(fileContent)))

and assuming that fileContent is a JSON file with some comments inside it, the output will be the original file content when you omit to pass the encoding "utf8" as the second argument of the function, as stated in the docs. The result is that the function is parsing a raw buffer.

Readme info on json-comments module out of date

There's already json-comments, but it's only for Node.js and uses a naive regex to strip comments which fails on simple cases like {"a":"//"}. This module however parses out the comments.

This does not seem to be true since version 0.2.0 of that module.

Stripping trailing part of "s/foo//" regex inside doubly-escaped script value breaks existential ssh command

This is that issue you've been dreading, I'm afraid. Apologies in advance.

So, I have this terrible script command in my package.json file. It runs tail via ssh, and pipes it through sed to do some higgerty-piggerty shit to prettify the output. The sed command has a wonky regex to strip off the ISO time prefix because, hey, time is just an artificial construct that devalues our all-too-ephemeral existence in this sad, sad, world.

It's awful, I know. Don't judge me.

The problem? ESLint no longer works because ESLint is trying to be clever and read my package.json even though there's nothing relevant to ESLint there but, hey, thanks for being clever. It barfs because SyntaxError: Cannot read config file: .../package.json.

Yay! 😹

Anyhow, the problem boils down to to this bit o' code, wherein my perfectly valid package.json file gets garbage-ified before being handed to JSON.parse.

Here's the SSCCE ...

package.json:

{
  "scripts": {
    "tail": "ssh some_host \"tail -f some_file | sed -e \\\"s/^.\\\\{46\\\\}T//\\\" -e \\\"s/#033/\\\\x1b/g\\\"\""
  }
}

Then...

node -e "console.log(require('strip-json-comments')(require('fs').readFileSync('package.json', 'utf8')))"

Output:

{
  "scripts": {
    "tail": "ssh some_host \"tail -f some_file | sed -e \\\"s/^.\\\\{46\\\\}T                                     
  }
}

Inclusion of MIT license in release

Hi

strip-json-comments does not include MIT license in its tar(release), can you please include it? It would be really useful in terms of packaging.

[v4] SyntaxError: Unexpected token 'export'

After installing strip-json-comments v4 I started to get

 export default function stripJsonComments(jsonString, {whitespace = true} = {}) {
    ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import stripComments from 'strip-json-comments';
        |                                                 ^

See the pipeline: https://github.com/pauldcomanici/oia/runs/6077266927?check_suite_focus=true

This issue seems to be caused by the fact that the code is not transpiled to work inside NodeJs environment.

It is possible to add a transpiller step for this package?

Feature: strip trailing commas

tsconfig.json supports comments but also trailing commas. For example:

{
  "exclude": [], // trailing comma
}

It would be nice to have a flag that enables stripping of these trailing commas.

I bet we can do this by tracking the location of the most recently encountered comma, and detecting when we hit a } or ]. Both of those characters signal that the comma was trailing and can be either removed or replaced with a space.

This may add a tiny bit of complexity to the result += statements. Instead of result +=, we might want to do accumulator += and then flush the accumulator to the result as soon as we have determined that the most recently-encountered comma is or is not trailing. That way, accumulator will start where the comma is, and we'll have a good way to strip that comma if necessary.

I can probably attempt an implementation in the near-term. My use-case requires CJS support, so I may do this in a fork, but I can at the very least share the patch here.

StripJsonComments in IE

Dear developer,
My application with strip-json-comments stopped working in IE - Syntax error in vendor.js on application loading. However, in Chrome it works good. I tried version 3.0.1 (released a year ago) - same result, not working in IE. Angular 8, IE -11.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.