Comparing version 0.7.5 to 0.8.0
'use strict'; | ||
var utils = require('./utils'); | ||
var EOL = require('os').EOL; // OS-dependent End-of-Line; | ||
@@ -24,2 +23,3 @@ function parser(code, options, config) { | ||
optTrim = options && options.trim, // 'trim' option; | ||
EOL = utils.getEOL(code, options && options.platform), | ||
isHtml, // set when the input is recognized as HTML; | ||
@@ -26,0 +26,0 @@ regEx; // regular expression details; |
'use strict'; | ||
var esprima = require('esprima'); | ||
var os = require('os'); | ||
/////////////////////////////////////////////////// | ||
// Returns the End Of Line based on the 'platform' | ||
// option, the text and the current environment. | ||
function getEOL(text, platform) { | ||
if (typeof platform === 'string') { | ||
var type = platform.trim().toLowerCase(); | ||
if (type === 'auto') { | ||
type = getLineBreakType(text); | ||
} | ||
switch (type) { | ||
case 'windows': | ||
return '\r\n'; | ||
case 'unix': | ||
return '\n'; | ||
default: | ||
break; | ||
} | ||
} | ||
return os.EOL; | ||
} | ||
///////////////////////////////////// | ||
// Automatically determines the type | ||
// of line breaks used in the text. | ||
function getLineBreakType(text) { | ||
var idx = 0, unix = 0, windows = 0; | ||
while (idx < text.length) { | ||
idx = text.indexOf('\n', idx); | ||
if (idx == -1) { | ||
break; | ||
} | ||
if (idx > 0 && text[idx - 1] === '\r') { | ||
windows++; | ||
} else { | ||
unix++; | ||
} | ||
idx++; | ||
} | ||
if (unix === windows) { | ||
return 'unknown'; | ||
} | ||
return unix > windows ? 'unix' : 'windows'; | ||
} | ||
//////////////////////////////////////////////////////////////////// | ||
@@ -73,2 +118,3 @@ // Tokenizes JSON or JavaScript via Esprima, enumerates and returns | ||
module.exports = { | ||
getEOL: getEOL, | ||
getSpaces: getSpaces, | ||
@@ -75,0 +121,0 @@ parseRegEx: parseRegEx, |
{ | ||
"name": "decomment", | ||
"version": "0.7.5", | ||
"version": "0.8.0", | ||
"description": "Removes comments from JSON, JavaScript, CSS, HTML, etc.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -71,2 +71,3 @@ decomment | ||
##### options.safe ⇒ Boolean | ||
* `false (default)` - remove all multi-line comments | ||
@@ -87,2 +88,3 @@ * `true` - keep multi-line comments that start with `/*!` | ||
##### options.space ⇒ Boolean | ||
* `false (default)` - remove comment blocks entirely | ||
@@ -104,2 +106,3 @@ * `true` - replace comment blocks with white spaces where needed, in order to preserve | ||
##### options.trim ⇒ Boolean | ||
* `false (default)` - do not trim comments | ||
@@ -119,2 +122,18 @@ * `true` - remove empty lines that follow removed full-line comments | ||
##### options.platform ⇒ String | ||
Overrides the environment platform settings. | ||
By default, this library processes line breaks according to the environment OS, | ||
which this option overrides, according to the value specified: | ||
* `auto` - determine the platform automatically from the text | ||
* `unix` - force Unix notation for the line breaks | ||
* `windows` - force Windows notation for the line breaks | ||
When `auto` is specified, `text` is scanned to determine the platform | ||
according to the frequency of the line break notations used in it. | ||
When it is impossible to determine automatically, the library defaults | ||
back to the environment-based settings. | ||
### decomment.text(text, [options]) ⇒ String | ||
@@ -121,0 +140,0 @@ |
47870
13
975
169