pax_global_header00006660000000000000000000000064126563216130014517gustar00rootroot0000000000000052 comment=1aef99eaa70d07981156e8aaa722e750c3b4eaf9 strip-json-comments-2.0.1/000077500000000000000000000000001265632161300154525ustar00rootroot00000000000000strip-json-comments-2.0.1/.editorconfig000066400000000000000000000003471265632161300201330ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 [*.md] trim_trailing_whitespace = false strip-json-comments-2.0.1/.gitattributes000066400000000000000000000000141265632161300203400ustar00rootroot00000000000000* text=auto strip-json-comments-2.0.1/.gitignore000066400000000000000000000000151265632161300174360ustar00rootroot00000000000000node_modules strip-json-comments-2.0.1/.travis.yml000066400000000000000000000001121265632161300175550ustar00rootroot00000000000000sudo: false language: node_js node_js: - 'stable' - '0.12' - '0.10' strip-json-comments-2.0.1/index.js000066400000000000000000000032441265632161300171220ustar00rootroot00000000000000'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 + (insideComment ? strip(str.substr(offset)) : str.substr(offset)); }; strip-json-comments-2.0.1/license000066400000000000000000000021371265632161300170220ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (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. strip-json-comments-2.0.1/package.json000066400000000000000000000013551265632161300177440ustar00rootroot00000000000000{ "name": "strip-json-comments", "version": "2.0.1", "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@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=0.10.0" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "json", "strip", "remove", "delete", "trim", "comments", "multiline", "parse", "config", "configuration", "conf", "settings", "util", "env", "environment" ], "devDependencies": { "ava": "*", "xo": "*" } } strip-json-comments-2.0.1/readme.md000066400000000000000000000027251265632161300172370ustar00rootroot00000000000000# 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 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. ## Install ``` $ npm install --save strip-json-comments ``` ## Usage ```js const json = '{/*rainbows*/"unicorn":"cake"}'; JSON.parse(stripJsonComments(json)); //=> {unicorn: 'cake'} ``` ## API ### stripJsonComments(input, [options]) #### input Type: `string` Accepts a string with JSON and returns a string without comments. #### options ##### whitespace Type: `boolean` Default: `true` Replace comments with whitespace instead of stripping them entirely. ## Related - [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 MIT © [Sindre Sorhus](http://sindresorhus.com) strip-json-comments-2.0.1/test.js000066400000000000000000000045511265632161300167740ustar00rootroot00000000000000import 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"}'); }); 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"}'); }); 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"}'); }); 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"}'); }); 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}'); }); 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}'); }); 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}'); }); 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}'); }); test('line endings - works at EOF', t => { const opts = {whitespace: false}; t.is(fn('{\r\n\t"a":"b"\r\n} //EOF'), '{\r\n\t"a":"b"\r\n} '); t.is(fn('{\r\n\t"a":"b"\r\n} //EOF', opts), '{\r\n\t"a":"b"\r\n} '); });