Skip to content

Commit 9126cbc

Browse files
committed
Require Node.js 8
1 parent 730fd42 commit 9126cbc

11 files changed

+158
-186
lines changed

.editorconfig

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ charset = utf-8
77
trim_trailing_whitespace = true
88
insert_final_newline = true
99

10-
[{package.json,*.yml}]
10+
[*.yml]
1111
indent_style = space
1212
indent_size = 2
13-
14-
[*.md]
15-
trim_trailing_whitespace = false

.gitattributes

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* text=auto
1+
* text=auto eol=lf

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
yarn.lock

.jshintrc

-12
This file was deleted.

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
sudo: false
21
language: node_js
32
node_js:
4-
- 'iojs'
5-
- '0.12'
6-
- '0.10'
3+
- '10'
4+
- '8'

index.js

+40-54
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,67 @@
11
'use strict';
2-
var execall = require('execall');
3-
var splitAt = require('split-at');
4-
var escapeStringRegexp = require('escape-string-regexp');
5-
var repeating = require('repeating');
2+
const execall = require('execall');
3+
const splitAt = require('split-at');
4+
const escapeStringRegexp = require('escape-string-regexp');
65

76
/*
87
Algorithm:
98
Find separators that are on the same index on each line, remove consecutive ones, then split on those indexes. It's important to check each line as you don't want to split in the middle of a column row just because it contains the separator.
109
*/
1110

12-
function countSeps(lines, separator) {
13-
separator = separator || ' ';
11+
const countSeparators = (lines, separator = ' ') => {
12+
const counts = [];
13+
const reSeparator = new RegExp(escapeStringRegexp(separator), 'g');
14+
const headerLength = (lines[0] || '').length;
1415

15-
var counts = [];
16-
var reSeparator = new RegExp(escapeStringRegexp(separator), 'g');
17-
var headerLength = (lines[0] || '').length;
16+
for (let line of lines) {
17+
// Ensure lines are as long as the header
18+
const padAmount = Math.ceil(Math.max(headerLength - line.length, 0) / separator.length);
19+
line += separator.repeat(padAmount);
1820

19-
for (var i = 0, line; i < lines.length; i++) {
20-
line = lines[i];
21-
22-
// ensure lines are as long as the header
23-
var padAmount = Math.ceil(Math.max(headerLength - line.length, 0) / separator.length);
24-
line += repeating(separator, padAmount);
25-
26-
var matches = execall(reSeparator, line);
27-
28-
for (var j = 0, col; j < matches.length; j++) {
29-
col = matches[j].index;
30-
counts[col] = typeof counts[col] === 'number' ? counts[col] + 1 : 1;
21+
for (const {index: column} of execall(reSeparator, line)) {
22+
counts[column] = typeof counts[column] === 'number' ? counts[column] + 1 : 1;
3123
}
3224
}
3325

3426
return counts;
35-
}
36-
37-
function getSplits(lines, separator) {
38-
var counts = countSeps(lines, separator);
39-
var splits = [];
40-
var consecutive = false;
27+
};
4128

42-
for (var col = 0, count; col < counts.length; col++) {
43-
count = counts[col];
29+
const getSplits = (lines, separator) => {
30+
const counts = countSeparators(lines, separator);
31+
const splits = [];
32+
let consecutive = false;
4433

34+
for (const [index, count] of counts.entries()) {
4535
if (count !== lines.length) {
4636
consecutive = false;
4737
} else {
48-
if (col !== 0 && !consecutive) {
49-
splits.push(col);
38+
if (index !== 0 && !consecutive) {
39+
splits.push(index);
5040
}
5141

5242
consecutive = true;
5343
}
5444
}
5545

5646
return splits;
57-
}
58-
59-
module.exports = function (str, opts) {
60-
opts = opts || {};
47+
};
6148

62-
var lines = str.replace(/^\s*\n|\s+$/g, '').split('\n');
63-
var splits = getSplits(lines, opts.separator);
64-
var rows = [];
65-
var headers = opts.headers;
66-
var transform = opts.transform;
67-
var els;
49+
module.exports = (input, options = {}) => {
50+
const lines = input.replace(/^\s*\n|\s+$/g, '').split('\n');
51+
let splits = getSplits(lines, options.separator);
52+
const {transform} = options;
53+
const rows = [];
54+
let items;
6855

56+
let {headers} = options;
6957
if (!headers) {
7058
headers = [];
71-
els = splitAt(lines[0], splits, {remove: true});
59+
items = splitAt(lines[0], splits, {remove: true});
7260

73-
for (var index = 0, el; index < els.length; ++index) {
74-
el = els[index].trim();
75-
if (el) {
76-
headers.push(el);
61+
for (let [index, item] of items.entries()) {
62+
item = item.trim();
63+
if (item) {
64+
headers.push(item);
7765
} else {
7866
splits[index - 1] = null;
7967
}
@@ -82,15 +70,13 @@ module.exports = function (str, opts) {
8270
splits = splits.filter(Boolean);
8371
}
8472

85-
for (var i = 1; i < lines.length; i++) {
86-
els = splitAt(lines[i], splits, {remove: true});
87-
88-
var row = {};
73+
for (const [index, line] of lines.slice(1).entries()) {
74+
items = splitAt(line, splits, {remove: true});
8975

90-
for (var j = 0, el, header; j < headers.length; j++) {
91-
el = (els[j] || '').trim();
92-
header = headers[j];
93-
row[header] = transform ? transform(el, header, j, i) : el;
76+
const row = {};
77+
for (const [index2, header] of headers.entries()) {
78+
const item = (items[index2] || '').trim();
79+
row[header] = transform ? transform(item, header, index2, index) : item;
9480
}
9581

9682
rows.push(row);

license

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
The MIT License (MIT)
1+
MIT License
22

33
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
5+
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:
116

12-
The above copyright notice and this permission notice shall be included in
13-
all copies or substantial portions of the Software.
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
148

15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
THE SOFTWARE.
9+
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.

package.json

+48-48
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
{
2-
"name": "parse-columns",
3-
"version": "1.3.0",
4-
"description": "Parse text columns, like the output of unix commands",
5-
"license": "MIT",
6-
"repository": "sindresorhus/parse-columns",
7-
"author": {
8-
"name": "Sindre Sorhus",
9-
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11-
},
12-
"engines": {
13-
"node": ">=0.10.0"
14-
},
15-
"scripts": {
16-
"test": "ava"
17-
},
18-
"files": [
19-
"index.js"
20-
],
21-
"keywords": [
22-
"parse",
23-
"parser",
24-
"columns",
25-
"column",
26-
"col",
27-
"row",
28-
"text",
29-
"string",
30-
"str",
31-
"unix",
32-
"command",
33-
"cmd",
34-
"output",
35-
"csv",
36-
"shell",
37-
"sh",
38-
"term",
39-
"table"
40-
],
41-
"dependencies": {
42-
"escape-string-regexp": "^1.0.3",
43-
"execall": "^1.0.0",
44-
"repeating": "^2.0.0",
45-
"split-at": "^1.1.0"
46-
},
47-
"devDependencies": {
48-
"ava": "*"
49-
}
2+
"name": "parse-columns",
3+
"version": "1.3.0",
4+
"description": "Parse text columns, like the output of Unix commands",
5+
"license": "MIT",
6+
"repository": "sindresorhus/parse-columns",
7+
"author": {
8+
"name": "Sindre Sorhus",
9+
"email": "sindresorhus@gmail.com",
10+
"url": "sindresorhus.com"
11+
},
12+
"engines": {
13+
"node": ">=8"
14+
},
15+
"scripts": {
16+
"test": "ava"
17+
},
18+
"files": [
19+
"index.js"
20+
],
21+
"keywords": [
22+
"parse",
23+
"parser",
24+
"columns",
25+
"column",
26+
"col",
27+
"row",
28+
"text",
29+
"string",
30+
"str",
31+
"unix",
32+
"command",
33+
"cmd",
34+
"output",
35+
"csv",
36+
"shell",
37+
"sh",
38+
"term",
39+
"table"
40+
],
41+
"dependencies": {
42+
"escape-string-regexp": "^1.0.5",
43+
"execall": "^1.0.0",
44+
"split-at": "^1.2.0"
45+
},
46+
"devDependencies": {
47+
"ava": "^1.1.0",
48+
"xo": "^0.24.0"
49+
}
5050
}

0 commit comments

Comments
 (0)