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

Julien Puydt julien.puydt at laposte.net
Tue Jun 23 14:33:39 UTC 2015


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to annotated tag upstream/1.0.2
in repository node-strip-json-comments.

commit 7117c0416453f08735ca5a2337b8c99e98c8eef3
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Tue Jun 23 15:07:20 2015 +0200

    Imported Upstream version 1.0.2
---
 .editorconfig          | 16 +++++++++++
 .gitattributes         |  1 +
 .gitignore             |  2 ++
 .jshintrc              | 20 ++++++++++++++
 .travis.yml            |  3 ++
 bower.json             | 29 ++++++++++++++++++++
 cli.js                 | 41 ++++++++++++++++++++++++++++
 component.json         | 26 ++++++++++++++++++
 license                | 21 ++++++++++++++
 package.json           | 48 ++++++++++++++++++++++++++++++++
 readme.md              | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++
 strip-json-comments.js | 67 +++++++++++++++++++++++++++++++++++++++++++++
 test.js                | 40 +++++++++++++++++++++++++++
 13 files changed, 388 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..8311fe1
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,16 @@
+# editorconfig.org
+root = true
+
+[*]
+indent_style = tab
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[package.json]
+indent_style = space
+indent_size = 2
+
+[*.md]
+trim_trailing_whitespace = false
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a088b6f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+bower_components
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..ac35c13
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,20 @@
+{
+	"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
new file mode 100644
index 0000000..244b7e8
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+  - '0.10'
diff --git a/bower.json b/bower.json
new file mode 100644
index 0000000..d805b03
--- /dev/null
+++ b/bower.json
@@ -0,0 +1,29 @@
+{
+  "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
new file mode 100644
index 0000000..ac4da48
--- /dev/null
+++ b/cli.js
@@ -0,0 +1,41 @@
+#!/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('cat <input file> | strip-json-comments > <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
new file mode 100644
index 0000000..34009d7
--- /dev/null
+++ b/component.json
@@ -0,0 +1,26 @@
+{
+  "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/license b/license
new file mode 100644
index 0000000..654d0bf
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) Sindre Sorhus <sindresorhus at gmail.com> (sindresorhus.com)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..0c234b4
--- /dev/null
+++ b/package.json
@@ -0,0 +1,48 @@
+{
+  "name": "strip-json-comments",
+  "version": "1.0.2",
+  "description": "Strip comments from JSON. Lets you use comments in your JSON files!",
+  "keywords": [
+    "json",
+    "strip",
+    "remove",
+    "delete",
+    "trim",
+    "comments",
+    "multiline",
+    "parse",
+    "config",
+    "configuration",
+    "conf",
+    "settings",
+    "util",
+    "env",
+    "environment",
+    "cli",
+    "bin"
+  ],
+  "license": "MIT",
+  "author": {
+    "name": "Sindre Sorhus",
+    "email": "sindresorhus at gmail.com",
+    "url": "http://sindresorhus.com"
+  },
+  "files": [
+    "cli.js",
+    "strip-json-comments.js"
+  ],
+  "main": "strip-json-comments",
+  "bin": {
+    "strip-json-comments": "cli.js"
+  },
+  "repository": "sindresorhus/strip-json-comments",
+  "scripts": {
+    "test": "mocha"
+  },
+  "devDependencies": {
+    "mocha": "*"
+  },
+  "engines": {
+    "node": ">=0.8.0"
+  }
+}
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..3365232
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,74 @@
+# strip-json-comments [![Build Status](https://travis-ci.org/sindresorhus/strip-json-comments.svg?branch=master)](https://travis-ci.org/sindresorhus/strip-json-comments)
+
+> Strip comments from JSON. Lets you use comments in your JSON files!
+
+This is now possible:
+
+```js
+{
+	// rainbows
+	"unicorn": /* ❤ */ "cake"
+}
+```
+
+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).
+
+
+*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.*
+
+
+## Install
+
+```sh
+$ npm install --save strip-json-comments
+```
+
+```sh
+$ bower install --save strip-json-comments
+```
+
+```sh
+$ component install sindresorhus/strip-json-comments
+```
+
+
+## Usage
+
+```js
+var json = '{/*rainbows*/"unicorn":"cake"}';
+JSON.parse(stripJsonComments(json));
+//=> {unicorn: 'cake'}
+```
+
+
+## API
+
+### stripJsonComments(input)
+
+#### input
+
+Type: `string`
+
+Accepts a string with JSON and returns a string without comments.
+
+
+## CLI
+
+```sh
+$ npm install --global strip-json-comments
+```
+
+```sh
+$ strip-json-comments --help
+
+strip-json-comments input-file > output-file
+# or
+strip-json-comments < input-file > output-file
+```
+
+
+## License
+
+MIT © [Sindre Sorhus](http://sindresorhus.com)
diff --git a/strip-json-comments.js b/strip-json-comments.js
new file mode 100644
index 0000000..a47976f
--- /dev/null
+++ b/strip-json-comments.js
@@ -0,0 +1,67 @@
+/*!
+	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';
+
+	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 && str[i - 1] !== '\\' && currentChar === '"') {
+				insideString = !insideString;
+			}
+
+			if (insideString) {
+				ret += currentChar;
+				continue;
+			}
+
+			if (!insideComment && currentChar + nextChar === '//') {
+				insideComment = 'single';
+				i++;
+			} else if (insideComment === 'single' && currentChar + nextChar === '\r\n') {
+				insideComment = false;
+				i++;
+				ret += currentChar;
+				ret += nextChar;
+				continue;
+			} else if (insideComment === 'single' && currentChar === '\n') {
+				insideComment = false;
+			} else if (!insideComment && currentChar + nextChar === '/*') {
+				insideComment = 'multi';
+				i++;
+				continue;
+			} else if (insideComment === 'multi' && 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
new file mode 100644
index 0000000..4a62e0f
--- /dev/null
+++ b/test.js
@@ -0,0 +1,40 @@
+'use strict';
+var assert = require('assert');
+var strip = require('./strip-json-comments');
+
+describe('Test Cases', function() {
+	it('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"}');
+	});
+
+	it('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() {
+			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() {
+			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() {
+			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() {
+			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