sv 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/sv.d.ts +1 -0
- package/bin/sv.js +10 -8
- package/common.d.ts +2 -1
- package/common.js +2 -0
- package/index.d.ts +1 -0
- package/index.js +8 -6
- package/package.json +13 -14
- package/parser.d.ts +3 -1
- package/parser.js +36 -21
- package/stringifier.d.ts +3 -1
- package/stringifier.js +31 -16
- package/.npmignore +0 -14
package/bin/sv.d.ts
CHANGED
package/bin/sv.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
"use strict";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var
|
|
6
|
-
var
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.main = void 0;
|
|
5
|
+
var optimist = require("optimist");
|
|
6
|
+
var fs_1 = require("fs");
|
|
7
|
+
var async_1 = require("async");
|
|
8
|
+
var index_1 = require("../index");
|
|
7
9
|
function main() {
|
|
8
10
|
var argvparser = optimist
|
|
9
11
|
.usage([
|
|
@@ -103,13 +105,13 @@ function main() {
|
|
|
103
105
|
}
|
|
104
106
|
else if (!process.stdin['isTTY']) {
|
|
105
107
|
// process.stdin.setEncoding('utf8');
|
|
106
|
-
index_1.transform(process.stdin, parser_opts, stringifier_opts, exit);
|
|
108
|
+
(0, index_1.transform)(process.stdin, parser_opts, stringifier_opts, exit);
|
|
107
109
|
}
|
|
108
110
|
else if (argv._.length) {
|
|
109
111
|
var filepaths = argv._;
|
|
110
|
-
async_1.eachSeries(filepaths, function (filepath, callback) {
|
|
111
|
-
var stream = fs_1.createReadStream(filepath);
|
|
112
|
-
index_1.transform(stream, parser_opts, stringifier_opts, callback);
|
|
112
|
+
(0, async_1.eachSeries)(filepaths, function (filepath, callback) {
|
|
113
|
+
var stream = (0, fs_1.createReadStream)(filepath);
|
|
114
|
+
(0, index_1.transform)(stream, parser_opts, stringifier_opts, callback);
|
|
113
115
|
console.error(''); // newline
|
|
114
116
|
}, exit);
|
|
115
117
|
}
|
package/common.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
/**
|
|
2
3
|
- `encoding` for converting to strings.
|
|
3
4
|
- `missing` is the value we use for 'time' when we have `columns = ['index', 'time']` and `write({index: 90})` is called.
|
|
@@ -8,7 +9,7 @@
|
|
|
8
9
|
*/
|
|
9
10
|
export interface Configuration {
|
|
10
11
|
/** character encoding */
|
|
11
|
-
encoding?:
|
|
12
|
+
encoding?: BufferEncoding;
|
|
12
13
|
/** string to represent missing data */
|
|
13
14
|
missing?: string;
|
|
14
15
|
/** string to separate rows */
|
package/common.js
CHANGED
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
var
|
|
7
|
-
exports
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transform = exports.Stringifier = exports.Parser = void 0;
|
|
4
|
+
var json_1 = require("streaming/json");
|
|
5
|
+
var property_1 = require("streaming/property");
|
|
6
|
+
var parser_1 = require("./parser");
|
|
7
|
+
Object.defineProperty(exports, "Parser", { enumerable: true, get: function () { return parser_1.Parser; } });
|
|
8
|
+
var stringifier_1 = require("./stringifier");
|
|
9
|
+
Object.defineProperty(exports, "Stringifier", { enumerable: true, get: function () { return stringifier_1.Stringifier; } });
|
|
8
10
|
function transform(input, parserConfig, stringifierConfig, callback) {
|
|
9
11
|
var transforms = [
|
|
10
12
|
parserConfig.json ? new json_1.Parser() : new parser_1.Parser(parserConfig),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sv",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Any separated values.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"any",
|
|
@@ -12,24 +12,23 @@
|
|
|
12
12
|
"tables"
|
|
13
13
|
],
|
|
14
14
|
"homepage": "https://github.com/chbrown/sv",
|
|
15
|
-
"repository":
|
|
16
|
-
"type": "git",
|
|
17
|
-
"url": "https://github.com/chbrown/sv.git"
|
|
18
|
-
},
|
|
15
|
+
"repository": "github:chbrown/sv",
|
|
19
16
|
"author": "Christopher Brown <io@henrian.com> (http://henrian.com)",
|
|
20
17
|
"license": "MIT",
|
|
21
18
|
"dependencies": {
|
|
22
|
-
"async": "
|
|
23
|
-
"optimist": "
|
|
24
|
-
"streaming": "
|
|
19
|
+
"async": "^1.5.2",
|
|
20
|
+
"optimist": "^0.6.1",
|
|
21
|
+
"streaming": "^1.1.1"
|
|
25
22
|
},
|
|
26
23
|
"devDependencies": {
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
24
|
+
"@types/mocha": "^2.2.43",
|
|
25
|
+
"@types/node": "^18.11.9",
|
|
26
|
+
"@types/optimist": "^0.0.33",
|
|
27
|
+
"coveralls": "^3.1.1",
|
|
28
|
+
"istanbul": "^0.4.5",
|
|
29
|
+
"mocha": "^2.4.5",
|
|
30
|
+
"mocha-lcov-reporter": "^1.3.0",
|
|
31
|
+
"typescript": "^4.9.5"
|
|
33
32
|
},
|
|
34
33
|
"scripts": {
|
|
35
34
|
"test": "make test"
|
package/parser.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
1
3
|
import { Transform } from 'stream';
|
|
2
4
|
import { Configuration } from './common';
|
|
3
5
|
export interface ParserConfiguration extends Configuration {
|
|
@@ -25,5 +27,5 @@ export declare class Parser extends Transform {
|
|
|
25
27
|
protected writeRow(cells: string[]): void;
|
|
26
28
|
flush(callback: (error?: Error) => void, nonfinal: boolean): void;
|
|
27
29
|
_flush(callback: (error?: Error) => void): void;
|
|
28
|
-
_transform(chunk: Buffer, encoding:
|
|
30
|
+
_transform(chunk: Buffer, encoding: BufferEncoding | 'buffer', callback: (error?: Error) => void): void;
|
|
29
31
|
}
|
package/parser.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || function (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
var __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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Parser = exports.inferDelimiter = exports.defaultParserConfiguration = void 0;
|
|
19
|
+
var stream_1 = require("stream");
|
|
20
|
+
var common_1 = require("./common");
|
|
9
21
|
exports.defaultParserConfiguration = {
|
|
10
22
|
encoding: 'utf8',
|
|
11
23
|
missing: '',
|
|
@@ -32,7 +44,7 @@ function inferDelimiter(buffer) {
|
|
|
32
44
|
9,
|
|
33
45
|
59,
|
|
34
46
|
44,
|
|
35
|
-
32,
|
|
47
|
+
32, // ' ' (space)
|
|
36
48
|
];
|
|
37
49
|
// TODO: make this more robust (that's why I even counted them)
|
|
38
50
|
for (var candidate = void 0, j = 0; (candidate = candidates[j]); j++) {
|
|
@@ -47,30 +59,31 @@ exports.inferDelimiter = inferDelimiter;
|
|
|
47
59
|
- `cellBuffer` is a list of strings that have yet to be processed (and sent to output).
|
|
48
60
|
|
|
49
61
|
*/
|
|
50
|
-
var Parser = (function (_super) {
|
|
62
|
+
var Parser = /** @class */ (function (_super) {
|
|
51
63
|
__extends(Parser, _super);
|
|
52
64
|
function Parser(config) {
|
|
53
65
|
if (config === void 0) { config = {}; }
|
|
54
|
-
_super.call(this, {
|
|
66
|
+
var _this = _super.call(this, {
|
|
55
67
|
decodeStrings: true,
|
|
56
68
|
readableObjectMode: true,
|
|
57
69
|
writableObjectMode: false,
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
|
|
70
|
+
}) || this;
|
|
71
|
+
_this.byteBuffer = Buffer.alloc(0);
|
|
72
|
+
_this.cellBuffer = [];
|
|
61
73
|
// merge defaults
|
|
62
|
-
|
|
74
|
+
_this.config = (0, common_1.merge)(config, exports.defaultParserConfiguration);
|
|
63
75
|
// special demarcating characters
|
|
64
76
|
// 1. delimiter
|
|
65
|
-
if (
|
|
66
|
-
|
|
77
|
+
if (_this.config.delimiter) {
|
|
78
|
+
_this.delimiterByte = _this.config.delimiter.charCodeAt(0);
|
|
67
79
|
}
|
|
68
80
|
// 2. quote
|
|
69
|
-
|
|
70
|
-
|
|
81
|
+
_this.quoteByte = _this.config.quotechar.charCodeAt(0);
|
|
82
|
+
_this.quotequoteRegExp = new RegExp(_this.config.quotechar + _this.config.quotechar, 'g');
|
|
71
83
|
// 3. escape
|
|
72
|
-
|
|
73
|
-
|
|
84
|
+
_this.escapeByte = _this.config.escape.charCodeAt(0);
|
|
85
|
+
_this.escapeQuoteRegExp = new RegExp('\\' + _this.config.escape + _this.config.quotechar, 'g');
|
|
86
|
+
return _this;
|
|
74
87
|
}
|
|
75
88
|
Parser.prototype.writeRow = function (cells) {
|
|
76
89
|
if (!this.config.columns) {
|
|
@@ -78,7 +91,7 @@ var Parser = (function (_super) {
|
|
|
78
91
|
this.config.columns = cells;
|
|
79
92
|
}
|
|
80
93
|
else {
|
|
81
|
-
this.push(common_1.zip(this.config.columns, cells, this.config.missing));
|
|
94
|
+
this.push((0, common_1.zip)(this.config.columns, cells, this.config.missing));
|
|
82
95
|
}
|
|
83
96
|
};
|
|
84
97
|
Parser.prototype.flush = function (callback, nonfinal) {
|
|
@@ -127,7 +140,9 @@ var Parser = (function (_super) {
|
|
|
127
140
|
else if (!eos && buffer[i] === this.quoteByte && !inside_quote && i == start) {
|
|
128
141
|
// if we are not already inside, and on a "
|
|
129
142
|
inside_quote = true;
|
|
143
|
+
// we can only enter a quote at the edge of the cell (thus, i == start)
|
|
130
144
|
}
|
|
145
|
+
// otherwise we just wait for the delimiter
|
|
131
146
|
else if (
|
|
132
147
|
// if we are at the very end of the input and this is the final chunk (ignoring any sort of state)
|
|
133
148
|
eos ||
|
package/stringifier.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
1
3
|
import { Transform } from 'stream';
|
|
2
4
|
import { Configuration } from './common';
|
|
3
5
|
export interface StringifierConfiguration extends Configuration {
|
|
@@ -38,5 +40,5 @@ export declare class Stringifier extends Transform {
|
|
|
38
40
|
protected writeObjects(objects: any[]): void;
|
|
39
41
|
flush(callback: (error?: Error) => void, nonfinal: boolean): void;
|
|
40
42
|
_flush(callback: (error?: Error) => void): void;
|
|
41
|
-
_transform(chunk: any, encoding:
|
|
43
|
+
_transform(chunk: any, encoding: BufferEncoding, callback: (error?: Error) => void): void;
|
|
42
44
|
}
|
package/stringifier.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || function (
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
var __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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Stringifier = exports.inferColumns = exports.defaultStringifierConfiguration = void 0;
|
|
19
|
+
var stream_1 = require("stream");
|
|
20
|
+
var common_1 = require("./common");
|
|
9
21
|
exports.defaultStringifierConfiguration = {
|
|
10
22
|
encoding: 'utf8',
|
|
11
23
|
missing: '',
|
|
@@ -50,21 +62,22 @@ exports.inferColumns = inferColumns;
|
|
|
50
62
|
|
|
51
63
|
- `_buffer` is an array of arrays or objects that need to be written
|
|
52
64
|
*/
|
|
53
|
-
var Stringifier = (function (_super) {
|
|
65
|
+
var Stringifier = /** @class */ (function (_super) {
|
|
54
66
|
__extends(Stringifier, _super);
|
|
55
67
|
function Stringifier(config) {
|
|
56
68
|
if (config === void 0) { config = {}; }
|
|
57
|
-
_super.call(this, { readableObjectMode: false, writableObjectMode: true });
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (
|
|
69
|
+
var _this = _super.call(this, { readableObjectMode: false, writableObjectMode: true }) || this;
|
|
70
|
+
_this.rowBuffer = [];
|
|
71
|
+
_this.config = (0, common_1.merge)(config, exports.defaultStringifierConfiguration);
|
|
72
|
+
_this.quotecharRegExp = new RegExp(_this.config.quotechar, 'ig');
|
|
73
|
+
if (_this.config.columns) {
|
|
62
74
|
// maybe we should write the columns even if we don't get any data?
|
|
63
|
-
|
|
75
|
+
_this.rowBuffer = [_this.config.columns];
|
|
64
76
|
}
|
|
65
77
|
else {
|
|
66
|
-
|
|
78
|
+
_this.rowBuffer = [];
|
|
67
79
|
}
|
|
80
|
+
return _this;
|
|
68
81
|
}
|
|
69
82
|
Stringifier.prototype.writeObject = function (object) {
|
|
70
83
|
// _write is already a thing, so don't use it.
|
|
@@ -90,13 +103,15 @@ var Stringifier = (function (_super) {
|
|
|
90
103
|
// obj is definitely an array now, but the fields aren't quoted.
|
|
91
104
|
for (var j = 0; j < length_1; j++) {
|
|
92
105
|
// assume minimal quoting (don't quote unless the cell contains the delimiter)
|
|
93
|
-
var value = object[j]
|
|
106
|
+
var value = String(object[j]);
|
|
94
107
|
var contains_newline = value.indexOf('\n') > -1 || value.indexOf('\r') > -1;
|
|
95
108
|
var contains_quotechar = value.indexOf(this.config.quotechar) > -1;
|
|
96
109
|
if (value.indexOf(this.config.delimiter) > -1 || contains_newline || contains_quotechar) {
|
|
97
110
|
if (contains_quotechar) {
|
|
98
111
|
// serialize into the excel dialect, currently
|
|
99
112
|
value = value.replace(this.quotecharRegExp, this.config.quotechar + this.config.quotechar);
|
|
113
|
+
// serialize with escapes:
|
|
114
|
+
// value = value.replace(this.quotechar_regex, '\\' + this.quotechar);
|
|
100
115
|
}
|
|
101
116
|
value = this.config.quotechar + value + this.config.quotechar;
|
|
102
117
|
}
|