UNPKG

3.08 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 };
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var DEFAULT_FIELD_DELIMITER = ',';
17var VALID_FIELD_DELIMITERS = [DEFAULT_FIELD_DELIMITER, ';'];
18var FieldStringifier = /** @class */ (function () {
19 function FieldStringifier(fieldDelimiter) {
20 this.fieldDelimiter = fieldDelimiter;
21 }
22 FieldStringifier.prototype.isEmpty = function (value) {
23 return typeof value === 'undefined' || value === null || value === '';
24 };
25 FieldStringifier.prototype.quoteField = function (field) {
26 return "\"" + field.replace(/"/g, '""') + "\"";
27 };
28 return FieldStringifier;
29}());
30exports.FieldStringifier = FieldStringifier;
31var DefaultFieldStringifier = /** @class */ (function (_super) {
32 __extends(DefaultFieldStringifier, _super);
33 function DefaultFieldStringifier() {
34 return _super !== null && _super.apply(this, arguments) || this;
35 }
36 DefaultFieldStringifier.prototype.stringify = function (value) {
37 if (this.isEmpty(value))
38 return '';
39 var str = String(value);
40 return this.needsQuote(str) ? this.quoteField(str) : str;
41 };
42 DefaultFieldStringifier.prototype.needsQuote = function (str) {
43 return str.includes(this.fieldDelimiter) || str.includes('\n') || str.includes('"');
44 };
45 return DefaultFieldStringifier;
46}(FieldStringifier));
47var ForceQuoteFieldStringifier = /** @class */ (function (_super) {
48 __extends(ForceQuoteFieldStringifier, _super);
49 function ForceQuoteFieldStringifier() {
50 return _super !== null && _super.apply(this, arguments) || this;
51 }
52 ForceQuoteFieldStringifier.prototype.stringify = function (value) {
53 return this.isEmpty(value) ? '' : this.quoteField(String(value));
54 };
55 return ForceQuoteFieldStringifier;
56}(FieldStringifier));
57function createFieldStringifier(fieldDelimiter, alwaysQuote) {
58 if (fieldDelimiter === void 0) { fieldDelimiter = DEFAULT_FIELD_DELIMITER; }
59 if (alwaysQuote === void 0) { alwaysQuote = false; }
60 _validateFieldDelimiter(fieldDelimiter);
61 return alwaysQuote ? new ForceQuoteFieldStringifier(fieldDelimiter) : new DefaultFieldStringifier(fieldDelimiter);
62}
63exports.createFieldStringifier = createFieldStringifier;
64function _validateFieldDelimiter(delimiter) {
65 if (VALID_FIELD_DELIMITERS.indexOf(delimiter) === -1) {
66 throw new Error("Invalid field delimiter `" + delimiter + "` is specified");
67 }
68}
69//# sourceMappingURL=field-stringifier.js.map
\No newline at end of file