[Pkg-javascript-commits] [node-strip-json-comments] 01/11: Imported Upstream version 2.0.0

Julien Puydt julien.puydt at laposte.net
Sat May 14 07:12:00 UTC 2016


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 44ae1dfe6d252f31ed0fa1eb07ad19de88dbb09a
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Fri Jan 8 15:05:51 2016 +0100

    Imported Upstream version 2.0.0
---
 .editorconfig          |   3 +-
 .gitignore             |   1 -
 .jshintrc              |  20 ---------
 .travis.yml            |   2 +-
 bower.json             |  29 -------------
 cli.js                 |  41 -------------------
 component.json         |  26 ------------
 index.js               |  70 ++++++++++++++++++++++++++++++++
 package.json           |  44 +++++++++-----------
 readme.md              |  40 ++++++------------
 strip-json-comments.js |  73 ---------------------------------
 test.js                | 108 ++++++++++++++++++++++++++++---------------------
 12 files changed, 166 insertions(+), 291 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index 8311fe1..8f9d77e 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -1,4 +1,3 @@
-# editorconfig.org
 root = true
 
 [*]
@@ -8,7 +7,7 @@ charset = utf-8
 trim_trailing_whitespace = true
 insert_final_newline = true
 
-[package.json]
+[{package.json,*.yml}]
 indent_style = space
 indent_size = 2
 
diff --git a/.gitignore b/.gitignore
index a088b6f..3c3629e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1 @@
 node_modules
-bower_components
diff --git a/.jshintrc b/.jshintrc
deleted file mode 100644
index ac35c13..0000000
--- a/.jshintrc
+++ /dev/null
@@ -1,20 +0,0 @@
-{
-	"node": true,
-	"esnext": true,
-	"bitwise": true,
-	"camelcase": true,
-	"curly": true,
-	"eqeqeq": true,
-	"immed": true,
-	"indent": 4,
-	"latedef": true,
-	"newcap": true,
-	"noarg": true,
-	"quotmark": "single",
-	"regexp": true,
-	"undef": true,
-	"unused": true,
-	"strict": true,
-	"trailing": true,
-	"smarttabs": true
-}
diff --git a/.travis.yml b/.travis.yml
index dedfc07..52ba159 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,6 @@
 sudo: false
 language: node_js
 node_js:
-  - 'iojs'
+  - 'stable'
   - '0.12'
   - '0.10'
diff --git a/bower.json b/bower.json
deleted file mode 100644
index d805b03..0000000
--- a/bower.json
+++ /dev/null
@@ -1,29 +0,0 @@
-{
-  "name": "strip-json-comments",
-  "description": "Strip comments from JSON. Lets you use comments in your JSON files!",
-  "main": "strip-json-comments.js",
-  "keywords": [
-    "json",
-    "strip",
-    "remove",
-    "delete",
-    "trim",
-    "comments",
-    "multiline",
-    "parse",
-    "config",
-    "configuration",
-    "conf",
-    "settings",
-    "util",
-    "env",
-    "environment"
-  ],
-  "ignore": [
-    ".*",
-    "test.js",
-    "cli.js",
-    "component.json",
-    "package.json"
-  ]
-}
diff --git a/cli.js b/cli.js
deleted file mode 100644
index aec5aa2..0000000
--- a/cli.js
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/usr/bin/env node
-'use strict';
-var fs = require('fs');
-var strip = require('./strip-json-comments');
-var input = process.argv[2];
-
-
-function getStdin(cb) {
-	var ret = '';
-
-	process.stdin.setEncoding('utf8');
-
-	process.stdin.on('data', function (data) {
-		ret += data;
-	});
-
-	process.stdin.on('end', function () {
-		cb(ret);
-	});
-}
-
-if (process.argv.indexOf('-h') !== -1 || process.argv.indexOf('--help') !== -1) {
-	console.log('strip-json-comments input-file > output-file');
-	console.log('or');
-	console.log('strip-json-comments < input-file > output-file');
-	return;
-}
-
-if (process.argv.indexOf('-v') !== -1 || process.argv.indexOf('--version') !== -1) {
-	console.log(require('./package').version);
-	return;
-}
-
-if (input) {
-	process.stdout.write(strip(fs.readFileSync(input, 'utf8')));
-	return;
-}
-
-getStdin(function (data) {
-	process.stdout.write(strip(data));
-});
diff --git a/component.json b/component.json
deleted file mode 100644
index 34009d7..0000000
--- a/component.json
+++ /dev/null
@@ -1,26 +0,0 @@
-{
-  "name": "strip-json-comments",
-  "version": "1.0.0",
-  "description": "Strip comments from JSON. Lets you use comments in your JSON files!",
-  "repository": "sindresorhus/strip-json-comments",
-  "keywords": [
-    "json",
-    "strip",
-    "remove",
-    "delete",
-    "trim",
-    "comments",
-    "multiline",
-    "parse",
-    "config",
-    "configuration",
-    "conf",
-    "settings",
-    "util",
-    "env",
-    "environment"
-  ],
-  "main": "strip-json-comments.js",
-  "scripts": ["strip-json-comments.js"],
-  "license": "MIT"
-}
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..1837ca3
--- /dev/null
+++ b/index.js
@@ -0,0 +1,70 @@
+'use strict';
+var singleComment = 1;
+var multiComment = 2;
+
+function stripWithoutWhitespace() {
+	return '';
+}
+
+function stripWithWhitespace(str, start, end) {
+	return str.slice(start, end).replace(/\S/g, ' ');
+}
+
+module.exports = function (str, opts) {
+	opts = opts || {};
+
+	var currentChar;
+	var nextChar;
+	var insideString = false;
+	var insideComment = false;
+	var offset = 0;
+	var ret = '';
+	var strip = opts.whitespace === false ? stripWithoutWhitespace : stripWithWhitespace;
+
+	for (var i = 0; i < str.length; i++) {
+		currentChar = str[i];
+		nextChar = str[i + 1];
+
+		if (!insideComment && currentChar === '"') {
+			var escaped = str[i - 1] === '\\' && str[i - 2] !== '\\';
+			if (!escaped) {
+				insideString = !insideString;
+			}
+		}
+
+		if (insideString) {
+			continue;
+		}
+
+		if (!insideComment && currentChar + nextChar === '//') {
+			ret += str.slice(offset, i);
+			offset = i;
+			insideComment = singleComment;
+			i++;
+		} else if (insideComment === singleComment && currentChar + nextChar === '\r\n') {
+			i++;
+			insideComment = false;
+			ret += strip(str, offset, i);
+			offset = i;
+			continue;
+		} else if (insideComment === singleComment && currentChar === '\n') {
+			insideComment = false;
+			ret += strip(str, offset, i);
+			offset = i;
+		} else if (!insideComment && currentChar + nextChar === '/*') {
+			ret += str.slice(offset, i);
+			offset = i;
+			insideComment = multiComment;
+			i++;
+			continue;
+		} else if (insideComment === multiComment && currentChar + nextChar === '*/') {
+			i++;
+			insideComment = false;
+			ret += strip(str, offset, i + 1);
+			offset = i + 1;
+			continue;
+		}
+	}
+
+	return ret + str.substr(offset);
+};
diff --git a/package.json b/package.json
index 20c9c25..abec6d8 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,23 @@
 {
   "name": "strip-json-comments",
-  "version": "1.0.4",
+  "version": "2.0.0",
   "description": "Strip comments from JSON. Lets you use comments in your JSON files!",
+  "license": "MIT",
+  "repository": "sindresorhus/strip-json-comments",
+  "author": {
+    "name": "Sindre Sorhus",
+    "email": "sindresorhus at gmail.com",
+    "url": "sindresorhus.com"
+  },
+  "engines": {
+    "node": ">=0.10.0"
+  },
+  "scripts": {
+    "test": "xo && ava"
+  },
+  "files": [
+    "index.js"
+  ],
   "keywords": [
     "json",
     "strip",
@@ -17,30 +33,10 @@
     "settings",
     "util",
     "env",
-    "environment",
-    "cli",
-    "bin"
+    "environment"
   ],
-  "license": "MIT",
-  "author": {
-    "name": "Sindre Sorhus",
-    "email": "sindresorhus at gmail.com",
-    "url": "sindresorhus.com"
-  },
-  "files": [
-    "cli.js",
-    "strip-json-comments.js"
-  ],
-  "main": "strip-json-comments",
-  "bin": "cli.js",
-  "repository": "sindresorhus/strip-json-comments",
-  "scripts": {
-    "test": "mocha --ui tdd"
-  },
   "devDependencies": {
-    "mocha": "*"
-  },
-  "engines": {
-    "node": ">=0.8.0"
+    "ava": "*",
+    "xo": "*"
   }
 }
diff --git a/readme.md b/readme.md
index 63ce165..0ee58df 100644
--- a/readme.md
+++ b/readme.md
@@ -11,34 +11,23 @@ This is now possible:
 }
 ```
 
-It will remove single-line comments `//` and multi-line comments `/**/`.
+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](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 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
 
-```sh
-$ npm install --save strip-json-comments
-```
-
-```sh
-$ bower install --save strip-json-comments
 ```
-
-```sh
-$ component install sindresorhus/strip-json-comments
+$ npm install --save strip-json-comments
 ```
 
 
 ## Usage
 
 ```js
-var json = '{/*rainbows*/"unicorn":"cake"}';
+const json = '{/*rainbows*/"unicorn":"cake"}';
+
 JSON.parse(stripJsonComments(json));
 //=> {unicorn: 'cake'}
 ```
@@ -46,7 +35,7 @@ JSON.parse(stripJsonComments(json));
 
 ## API
 
-### stripJsonComments(input)
+### stripJsonComments(input, [options])
 
 #### input
 
@@ -54,25 +43,20 @@ Type: `string`
 
 Accepts a string with JSON and returns a string without comments.
 
+#### options
 
-## CLI
+##### whitespace
 
-```sh
-$ npm install --global strip-json-comments
-```
+Type: `boolean`  
+Default: `true`
 
-```sh
-$ strip-json-comments --help
-
-strip-json-comments input-file > output-file
-# or
-strip-json-comments < input-file > output-file
-```
+Replace comments with whitespace instead of stripping them entirely.
 
 
 ## Related
 
-- [`strip-css-comments`](https://github.com/sindresorhus/strip-css-comments)
+- [strip-json-comments-cli](https://github.com/sindresorhus/strip-json-comments-cli) - CLI for this module
+- [strip-css-comments](https://github.com/sindresorhus/strip-css-comments) - Strip comments from CSS
 
 
 ## License
diff --git a/strip-json-comments.js b/strip-json-comments.js
deleted file mode 100644
index eb77ce7..0000000
--- a/strip-json-comments.js
+++ /dev/null
@@ -1,73 +0,0 @@
-/*!
-	strip-json-comments
-	Strip comments from JSON. Lets you use comments in your JSON files!
-	https://github.com/sindresorhus/strip-json-comments
-	by Sindre Sorhus
-	MIT License
-*/
-(function () {
-	'use strict';
-
-	var singleComment = 1;
-	var multiComment = 2;
-
-	function stripJsonComments(str) {
-		var currentChar;
-		var nextChar;
-		var insideString = false;
-		var insideComment = false;
-		var ret = '';
-
-		for (var i = 0; i < str.length; i++) {
-			currentChar = str[i];
-			nextChar = str[i + 1];
-
-			if (!insideComment && currentChar === '"') {
-				var escaped = str[i - 1] === '\\' && str[i - 2] !== '\\';
-				if (!insideComment && !escaped && currentChar === '"') {
-					insideString = !insideString;
-				}
-			}
-
-			if (insideString) {
-				ret += currentChar;
-				continue;
-			}
-
-			if (!insideComment && currentChar + nextChar === '//') {
-				insideComment = singleComment;
-				i++;
-			} else if (insideComment === singleComment && currentChar + nextChar === '\r\n') {
-				insideComment = false;
-				i++;
-				ret += currentChar;
-				ret += nextChar;
-				continue;
-			} else if (insideComment === singleComment && currentChar === '\n') {
-				insideComment = false;
-			} else if (!insideComment && currentChar + nextChar === '/*') {
-				insideComment = multiComment;
-				i++;
-				continue;
-			} else if (insideComment === multiComment && currentChar + nextChar === '*/') {
-				insideComment = false;
-				i++;
-				continue;
-			}
-
-			if (insideComment) {
-				continue;
-			}
-
-			ret += currentChar;
-		}
-
-		return ret;
-	}
-
-	if (typeof module !== 'undefined' && module.exports) {
-		module.exports = stripJsonComments;
-	} else {
-		window.stripJsonComments = stripJsonComments;
-	}
-})();
diff --git a/test.js b/test.js
index eb4906e..de39040 100644
--- a/test.js
+++ b/test.js
@@ -1,47 +1,63 @@
-'use strict';
-var assert = require('assert');
-var strip = require('./strip-json-comments');
-
-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}');
-		assert.strictEqual(strip('{"a":"b"/*comment*/}'), '{"a":"b"}');
-		assert.strictEqual(strip('{"a"/*\n\n\ncomment\r\n*/:"b"}'), '{"a":"b"}');
-	});
-
-	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"}');
-	});
-
-	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}');
-		});
-
-		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}');
-		});
-
-		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}');
-		});
-
-		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}');
-		});
-	});
+import test from 'ava';
+import fn from './';
+
+test('replace comments with whitespace', t => {
+	t.is(fn('//comment\n{"a":"b"}'), '         \n{"a":"b"}');
+	t.is(fn('/*//comment*/{"a":"b"}'), '             {"a":"b"}');
+	t.is(fn('{"a":"b"//comment\n}'), '{"a":"b"         \n}');
+	t.is(fn('{"a":"b"/*comment*/}'), '{"a":"b"           }');
+	t.is(fn('{"a"/*\n\n\ncomment\r\n*/:"b"}'), '{"a"  \n\n\n       \r\n  :"b"}');
+	t.is(fn('/*!\n * comment\n */\n{"a":"b"}'), '   \n          \n   \n{"a":"b"}');
+	t.is(fn('{/*comment*/"a":"b"}'), '{           "a":"b"}');
+	t.end();
+});
+
+test('remove comments', t => {
+	const opts = {whitespace: false};
+	t.is(fn('//comment\n{"a":"b"}', opts), '\n{"a":"b"}');
+	t.is(fn('/*//comment*/{"a":"b"}', opts), '{"a":"b"}');
+	t.is(fn('{"a":"b"//comment\n}', opts), '{"a":"b"\n}');
+	t.is(fn('{"a":"b"/*comment*/}', opts), '{"a":"b"}');
+	t.is(fn('{"a"/*\n\n\ncomment\r\n*/:"b"}', opts), '{"a":"b"}');
+	t.is(fn('/*!\n * comment\n */\n{"a":"b"}', opts), '\n{"a":"b"}');
+	t.is(fn('{/*comment*/"a":"b"}', opts), '{"a":"b"}');
+	t.end();
+});
+
+test('doesn\'t strip comments inside strings', t => {
+	t.is(fn('{"a":"b//c"}'), '{"a":"b//c"}');
+	t.is(fn('{"a":"b/*c*/"}'), '{"a":"b/*c*/"}');
+	t.is(fn('{"/*a":"b"}'), '{"/*a":"b"}');
+	t.is(fn('{"\\"/*a":"b"}'), '{"\\"/*a":"b"}');
+	t.end();
+});
+
+test('consider escaped slashes when checking for escaped string quote', t => {
+	t.is(fn('{"\\\\":"https://foobar.com"}'), '{"\\\\":"https://foobar.com"}');
+	t.is(fn('{"foo\\\"":"https://foobar.com"}'), '{"foo\\\"":"https://foobar.com"}');
+	t.end();
+});
+
+test('line endings - no comments', t => {
+	t.is(fn('{"a":"b"\n}'), '{"a":"b"\n}');
+	t.is(fn('{"a":"b"\r\n}'), '{"a":"b"\r\n}');
+	t.end();
+});
+
+test('line endings - single line comment', t => {
+	t.is(fn('{"a":"b"//c\n}'), '{"a":"b"   \n}');
+	t.is(fn('{"a":"b"//c\r\n}'), '{"a":"b"   \r\n}');
+	t.end();
+});
+
+test('line endings - single line block comment', t => {
+	t.is(fn('{"a":"b"/*c*/\n}'), '{"a":"b"     \n}');
+	t.is(fn('{"a":"b"/*c*/\r\n}'), '{"a":"b"     \r\n}');
+	t.end();
+});
+
+test('line endings - multi line block comment', t => {
+	t.is(fn('{"a":"b",/*c\nc2*/"x":"y"\n}'), '{"a":"b",   \n    "x":"y"\n}');
+	t.is(fn('{"a":"b",/*c\r\nc2*/"x":"y"\r\n}'), '{"a":"b",   \r\n    "x":"y"\r\n}');
+	t.end();
 });

-- 
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