[Pkg-javascript-commits] [node-strip-json-comments] 01/03: Imported Upstream version 1.0.4
Julien Puydt
julien.puydt at laposte.net
Wed Aug 12 14:29:10 UTC 2015
This is an automated email from the git hooks/post-receive script.
jpuydt-guest pushed a commit to branch master
in repository node-strip-json-comments.
commit 66e23932c9151d3313d28496665bb5db3aaa6876
Author: Julien Puydt <julien.puydt at laposte.net>
Date: Wed Aug 12 16:17:38 2015 +0200
Imported Upstream version 1.0.4
---
.travis.yml | 3 +++
cli.js | 4 ++--
package.json | 10 ++++------
readme.md | 10 ++++++++--
strip-json-comments.js | 20 +++++++++++++-------
test.js | 25 ++++++++++++++++---------
6 files changed, 46 insertions(+), 26 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 244b7e8..dedfc07 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,3 +1,6 @@
+sudo: false
language: node_js
node_js:
+ - 'iojs'
+ - '0.12'
- '0.10'
diff --git a/cli.js b/cli.js
index ac4da48..aec5aa2 100644
--- a/cli.js
+++ b/cli.js
@@ -20,9 +20,9 @@ function getStdin(cb) {
}
if (process.argv.indexOf('-h') !== -1 || process.argv.indexOf('--help') !== -1) {
- console.log('strip-json-comments <input file> > <output file>');
+ console.log('strip-json-comments input-file > output-file');
console.log('or');
- console.log('cat <input file> | strip-json-comments > <output file>');
+ console.log('strip-json-comments < input-file > output-file');
return;
}
diff --git a/package.json b/package.json
index 0c234b4..20c9c25 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "strip-json-comments",
- "version": "1.0.2",
+ "version": "1.0.4",
"description": "Strip comments from JSON. Lets you use comments in your JSON files!",
"keywords": [
"json",
@@ -25,19 +25,17 @@
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus at gmail.com",
- "url": "http://sindresorhus.com"
+ "url": "sindresorhus.com"
},
"files": [
"cli.js",
"strip-json-comments.js"
],
"main": "strip-json-comments",
- "bin": {
- "strip-json-comments": "cli.js"
- },
+ "bin": "cli.js",
"repository": "sindresorhus/strip-json-comments",
"scripts": {
- "test": "mocha"
+ "test": "mocha --ui tdd"
},
"devDependencies": {
"mocha": "*"
diff --git a/readme.md b/readme.md
index 3365232..63ce165 100644
--- a/readme.md
+++ b/readme.md
@@ -13,10 +13,11 @@ This is now possible:
It will remove single-line comments `//` and multi-line comments `/**/`.
-Also available as a [gulp](https://github.com/sindresorhus/gulp-strip-json-comments)/[grunt](https://github.com/sindresorhus/grunt-strip-json-comments)/[broccoli](https://github.com/sindresorhus/broccoli-strip-json-comments) plugin and a [require hook](https://github.com/uTest/autostrip-json-comments).
+Also available as a [gulp](https://github.com/sindresorhus/gulp-strip-json-comments)/[grunt](https://github.com/sindresorhus/grunt-strip-json-comments)/[broccoli](https://github.com/sindresorhus/broccoli-strip-json-comments) plugin.
+-
-*There's already [json-comments](https://npmjs.org/package/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.*
+*There's also [`json-comments`](https://npmjs.org/package/json-comments), but it's only for Node.js, inefficient, bloated as it also minifies, and comes with a `require` hook, which is :(*
## Install
@@ -69,6 +70,11 @@ strip-json-comments < input-file > output-file
```
+## Related
+
+- [`strip-css-comments`](https://github.com/sindresorhus/strip-css-comments)
+
+
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)
diff --git a/strip-json-comments.js b/strip-json-comments.js
index a47976f..eb77ce7 100644
--- a/strip-json-comments.js
+++ b/strip-json-comments.js
@@ -8,6 +8,9 @@
(function () {
'use strict';
+ var singleComment = 1;
+ var multiComment = 2;
+
function stripJsonComments(str) {
var currentChar;
var nextChar;
@@ -19,8 +22,11 @@
currentChar = str[i];
nextChar = str[i + 1];
- if (!insideComment && str[i - 1] !== '\\' && currentChar === '"') {
- insideString = !insideString;
+ if (!insideComment && currentChar === '"') {
+ var escaped = str[i - 1] === '\\' && str[i - 2] !== '\\';
+ if (!insideComment && !escaped && currentChar === '"') {
+ insideString = !insideString;
+ }
}
if (insideString) {
@@ -29,21 +35,21 @@
}
if (!insideComment && currentChar + nextChar === '//') {
- insideComment = 'single';
+ insideComment = singleComment;
i++;
- } else if (insideComment === 'single' && currentChar + nextChar === '\r\n') {
+ } else if (insideComment === singleComment && currentChar + nextChar === '\r\n') {
insideComment = false;
i++;
ret += currentChar;
ret += nextChar;
continue;
- } else if (insideComment === 'single' && currentChar === '\n') {
+ } else if (insideComment === singleComment && currentChar === '\n') {
insideComment = false;
} else if (!insideComment && currentChar + nextChar === '/*') {
- insideComment = 'multi';
+ insideComment = multiComment;
i++;
continue;
- } else if (insideComment === 'multi' && currentChar + nextChar === '*/') {
+ } else if (insideComment === multiComment && currentChar + nextChar === '*/') {
insideComment = false;
i++;
continue;
diff --git a/test.js b/test.js
index 4a62e0f..eb4906e 100644
--- a/test.js
+++ b/test.js
@@ -2,8 +2,8 @@
var assert = require('assert');
var strip = require('./strip-json-comments');
-describe('Test Cases', function() {
- it('should strip comments', function () {
+suite('Test Cases', function () {
+ test('should strip comments', function () {
assert.strictEqual(strip('//comment\n{"a":"b"}'), '\n{"a":"b"}');
assert.strictEqual(strip('/*//comment*/{"a":"b"}'), '{"a":"b"}');
assert.strictEqual(strip('{"a":"b"//comment\n}'), '{"a":"b"\n}');
@@ -11,30 +11,37 @@ describe('Test Cases', function() {
assert.strictEqual(strip('{"a"/*\n\n\ncomment\r\n*/:"b"}'), '{"a":"b"}');
});
- it('should not strip comments inside strings', function () {
+ test('should not strip comments inside strings', function () {
assert.strictEqual(strip('{"a":"b//c"}'), '{"a":"b//c"}');
assert.strictEqual(strip('{"a":"b/*c*/"}'), '{"a":"b/*c*/"}');
assert.strictEqual(strip('{"/*a":"b"}'), '{"/*a":"b"}');
assert.strictEqual(strip('{"\\"/*a":"b"}'), '{"\\"/*a":"b"}');
});
- describe('Line endings', function() {
- it('no comments', function() {
+ test('should consider escaped slashes when checking for escaped string quote', function () {
+ assert.strictEqual(strip('{"\\\\":"https://foobar.com"}'), '{"\\\\":"https://foobar.com"}');
+ assert.strictEqual(strip('{"foo\\\"":"https://foobar.com"}'), '{"foo\\\"":"https://foobar.com"}');
+ });
+
+ suite('Line endings', function () {
+ test('no comments', function () {
assert.strictEqual(strip('{"a":"b"\n}'), '{"a":"b"\n}');
assert.strictEqual(strip('{"a":"b"\r\n}'), '{"a":"b"\r\n}');
});
- it('single line comment', function() {
+
+ test('single line comment', function () {
assert.strictEqual(strip('{"a":"b"//c\n}'), '{"a":"b"\n}');
assert.strictEqual(strip('{"a":"b"//c\r\n}'), '{"a":"b"\r\n}');
});
- it('single line block comment', function() {
+
+ test('single line block comment', function () {
assert.strictEqual(strip('{"a":"b"/*c*/\n}'), '{"a":"b"\n}');
assert.strictEqual(strip('{"a":"b"/*c*/\r\n}'), '{"a":"b"\r\n}');
});
- it('multi line block comment', function() {
+
+ test('multi line block comment', function () {
assert.strictEqual(strip('{"a":"b",/*c\nc2*/"x":"y"\n}'), '{"a":"b","x":"y"\n}');
assert.strictEqual(strip('{"a":"b",/*c\r\nc2*/"x":"y"\r\n}'), '{"a":"b","x":"y"\r\n}');
});
});
});
-
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-strip-json-comments.git
More information about the Pkg-javascript-commits
mailing list