jsonexport
Advanced tools
Comparing version 2.2.0 to 2.2.1
@@ -12,3 +12,3 @@ /* jshint node:true */ | ||
module.exports = function escapedDelimiters(textDelimiter, rowDelimiter) { | ||
module.exports = function escapedDelimiters(textDelimiter, rowDelimiter, forceTextDelimiter) { | ||
var endOfLine = '\n'; | ||
@@ -34,12 +34,15 @@ | ||
return function (value) { | ||
if (forceTextDelimiter) value = "" + value; | ||
if (!value.replace) return value; | ||
// Escape the textDelimiters contained in the field | ||
var newValue = value.replace(textDelimiterRegex, escapedDelimiter); | ||
value = value.replace(textDelimiterRegex, escapedDelimiter); | ||
// Escape the whole field if it contains a rowDelimiter or a linebreak or double quote | ||
if (enclosingCondition(value)) { | ||
newValue = textDelimiter + newValue + textDelimiter; | ||
if (forceTextDelimiter || enclosingCondition(value)) { | ||
value = textDelimiter + value + textDelimiter; | ||
} | ||
return newValue; | ||
return value; | ||
}; | ||
}; |
@@ -24,3 +24,3 @@ /* jshint node:true */ | ||
this._headers = this._options.headers || []; | ||
this._escape = require('../core/escape-delimiters')(this._options.textDelimiter, this._options.rowDelimiter); | ||
this._escape = require('../core/escape-delimiters')(this._options.textDelimiter, this._options.rowDelimiter, this._options.forceTextDelimiter); | ||
} | ||
@@ -264,2 +264,3 @@ | ||
verticalOutput: true, // Boolean | ||
forceTextDelimiter: false, //Boolean | ||
//Handlers | ||
@@ -279,5 +280,13 @@ handleString: undefined, // Function | ||
var headers = this._headers; | ||
if (this._options.rename && this._options.rename.length > 0) headers = headers.map(function (header) { | ||
return _this._options.rename[_this._options.headers.indexOf(header)] || header; | ||
}); | ||
if (this._options.forceTextDelimiter) { | ||
headers = headers.map(function (header) { | ||
return '' + _this._options.textDelimiter + header + _this._options.textDelimiter; | ||
}); | ||
} | ||
return headers.join(this._options.rowDelimiter); | ||
@@ -284,0 +293,0 @@ } |
@@ -12,3 +12,3 @@ /* jshint node:true */ | ||
module.exports = function escapedDelimiters(textDelimiter, rowDelimiter) { | ||
module.exports = function escapedDelimiters(textDelimiter, rowDelimiter, forceTextDelimiter) { | ||
let endOfLine = '\n'; | ||
@@ -35,12 +35,15 @@ | ||
return function(value) { | ||
if(forceTextDelimiter) value = "" + value; | ||
if (!value.replace) return value; | ||
// Escape the textDelimiters contained in the field | ||
var newValue = value.replace(textDelimiterRegex, escapedDelimiter); | ||
value = value.replace(textDelimiterRegex, escapedDelimiter); | ||
// Escape the whole field if it contains a rowDelimiter or a linebreak or double quote | ||
if (enclosingCondition(value)) { | ||
newValue = textDelimiter + newValue + textDelimiter; | ||
if (forceTextDelimiter || enclosingCondition(value)) { | ||
value = textDelimiter + value + textDelimiter; | ||
} | ||
return newValue; | ||
return value; | ||
}; | ||
}; |
@@ -19,3 +19,4 @@ /* jshint node:true */ | ||
this._options.textDelimiter, | ||
this._options.rowDelimiter | ||
this._options.rowDelimiter, | ||
this._options.forceTextDelimiter | ||
); | ||
@@ -41,4 +42,12 @@ } | ||
let headers = this._headers; | ||
if (this._options.rename && this._options.rename.length > 0) | ||
headers = headers.map((header) => this._options.rename[this._options.headers.indexOf(header)] || header); | ||
if(this._options.forceTextDelimiter) { | ||
headers = headers.map((header) => { | ||
return `${this._options.textDelimiter}${header}${this._options.textDelimiter}`; | ||
}); | ||
} | ||
return headers.join(this._options.rowDelimiter); | ||
@@ -174,2 +183,3 @@ } | ||
verticalOutput: true, // Boolean | ||
forceTextDelimiter: false, //Boolean | ||
//Handlers | ||
@@ -176,0 +186,0 @@ handleString: undefined, // Function |
{ | ||
"name": "jsonexport", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Makes easy to convert JSON to CSV", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -254,2 +254,3 @@ # jsonexport | ||
- `textDelimiter` - `String` The character used to escape the text content if needed (default to `"`) | ||
- `forceTextDelimiter` - `Boolean` Set this option to true to wrap every data item and header in the textDelimiter. Defaults to `false` | ||
- `endOfLine` - `String` Replace the OS default EOL. | ||
@@ -341,2 +342,3 @@ - `mainPathItem` - `String` Every header will have the `mainPathItem` as the base. | ||
- [Acker Apple](https://github.com/AckerApple) [![hire me](https://ackerapple.github.io/resume/assets/images/hire-me-badge.svg)](https://ackerapple.github.io/resume/) | ||
- [Victor Hahn](https://github.com/rv-vhahn) | ||
- [And many more...](https://github.com/kauegimenes/jsonexport/graphs/contributors) |
@@ -16,3 +16,4 @@ /* jshint node:true */ | ||
complexField: 'I am a \n multi line field containing "textDelimiters"', | ||
alreadyEscaped: '"I contain "double quotes" everywhere !"' | ||
alreadyEscaped: '"I contain "double quotes" everywhere !"', | ||
forceEscape: 42 | ||
}; | ||
@@ -39,2 +40,9 @@ | ||
}); | ||
it('should escape if forceTextDelimiter flag is true', () => { | ||
var escapeDelimiters = require('../lib/core/escape-delimiters')('"', '\n', true); | ||
expect(escapeDelimiters(mocks.forceEscape)).to.be.a.string; | ||
expect(escapeDelimiters(mocks.forceEscape)).to.be.equal('"42"'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
88104
1712
342
0