Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

parse-columns

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-columns - npm Package Compare versions

Comparing version 1.3.0 to 2.0.0

index.d.ts

96

index.js
'use strict';
var execall = require('execall');
var splitAt = require('split-at');
var escapeStringRegexp = require('escape-string-regexp');
var repeating = require('repeating');
const execall = require('execall');
const splitAt = require('split-at');
const escapeStringRegexp = require('escape-string-regexp');

@@ -12,21 +11,14 @@ /*

function countSeps(lines, separator) {
separator = separator || ' ';
const countSeparators = (lines, separator = ' ') => {
const counts = [];
const separatorRegex = new RegExp(escapeStringRegexp(separator), 'g');
const headerLength = (lines[0] || '').length;
var counts = [];
var reSeparator = new RegExp(escapeStringRegexp(separator), 'g');
var headerLength = (lines[0] || '').length;
for (let line of lines) {
// Ensure lines are as long as the header
const padAmount = Math.ceil(Math.max(headerLength - line.length, 0) / separator.length);
line += separator.repeat(padAmount);
for (var i = 0, line; i < lines.length; i++) {
line = lines[i];
// ensure lines are as long as the header
var padAmount = Math.ceil(Math.max(headerLength - line.length, 0) / separator.length);
line += repeating(separator, padAmount);
var matches = execall(reSeparator, line);
for (var j = 0, col; j < matches.length; j++) {
col = matches[j].index;
counts[col] = typeof counts[col] === 'number' ? counts[col] + 1 : 1;
for (const {index: column} of execall(separatorRegex, line)) {
counts[column] = typeof counts[column] === 'number' ? counts[column] + 1 : 1;
}

@@ -36,17 +28,15 @@ }

return counts;
}
};
function getSplits(lines, separator) {
var counts = countSeps(lines, separator);
var splits = [];
var consecutive = false;
const getSplits = (lines, separator) => {
const counts = countSeparators(lines, separator);
const splits = [];
let consecutive = false;
for (var col = 0, count; col < counts.length; col++) {
count = counts[col];
if (count !== lines.length) {
for (const [index, count] of counts.entries()) {
if (count !== lines.length) { // eslint-disable-line no-negated-condition
consecutive = false;
} else {
if (col !== 0 && !consecutive) {
splits.push(col);
if (index !== 0 && !consecutive) {
splits.push(index);
}

@@ -59,22 +49,20 @@

return splits;
}
};
module.exports = function (str, opts) {
opts = opts || {};
module.exports = (input, options = {}) => {
const lines = input.replace(/^\s*\n|\s+$/g, '').split('\n');
let splits = getSplits(lines, options.separator);
const {transform} = options;
const rows = [];
let items;
var lines = str.replace(/^\s*\n|\s+$/g, '').split('\n');
var splits = getSplits(lines, opts.separator);
var rows = [];
var headers = opts.headers;
var transform = opts.transform;
var els;
let {headers} = options;
if (!headers) {
headers = [];
els = splitAt(lines[0], splits, {remove: true});
items = splitAt(lines[0], splits, {remove: true});
for (var index = 0, el; index < els.length; ++index) {
el = els[index].trim();
if (el) {
headers.push(el);
for (let [index, item] of items.entries()) {
item = item.trim();
if (item) {
headers.push(item);
} else {

@@ -88,11 +76,9 @@ splits[index - 1] = null;

for (var i = 1; i < lines.length; i++) {
els = splitAt(lines[i], splits, {remove: true});
for (const [index, line] of lines.slice(1).entries()) {
items = splitAt(line, splits, {remove: true});
var row = {};
for (var j = 0, el, header; j < headers.length; j++) {
el = (els[j] || '').trim();
header = headers[j];
row[header] = transform ? transform(el, header, j, i) : el;
const row = {};
for (const [index2, header] of headers.entries()) {
const item = (items[index2] || '').trim();
row[header] = transform ? transform(item, header, index2, index) : item;
}

@@ -99,0 +85,0 @@

{
"name": "parse-columns",
"version": "1.3.0",
"description": "Parse text columns, like the output of unix commands",
"license": "MIT",
"repository": "sindresorhus/parse-columns",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "ava"
},
"files": [
"index.js"
],
"keywords": [
"parse",
"parser",
"columns",
"column",
"col",
"row",
"text",
"string",
"str",
"unix",
"command",
"cmd",
"output",
"csv",
"shell",
"sh",
"term",
"table"
],
"dependencies": {
"escape-string-regexp": "^1.0.3",
"execall": "^1.0.0",
"repeating": "^2.0.0",
"split-at": "^1.1.0"
},
"devDependencies": {
"ava": "*"
}
"name": "parse-columns",
"version": "2.0.0",
"description": "Parse text columns, like the output of Unix commands",
"license": "MIT",
"repository": "sindresorhus/parse-columns",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"parse",
"parser",
"columns",
"column",
"row",
"text",
"string",
"unix",
"command",
"output",
"csv",
"shell",
"sh",
"term",
"table"
],
"dependencies": {
"escape-string-regexp": "^2.0.0",
"execall": "^2.0.0",
"split-at": "^2.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
# parse-columns [![Build Status](https://travis-ci.org/sindresorhus/parse-columns.svg?branch=master)](https://travis-ci.org/sindresorhus/parse-columns)
> Parse text columns, like the output of unix commands
> Parse text columns, like the output of Unix commands

@@ -9,3 +9,3 @@

```
$ npm install --save parse-columns
$ npm install parse-columns
```

@@ -25,27 +25,35 @@

```js
var childProcess = require('child_process');
var parseColumns = require('parse-columns');
const {promisify} = require('util');
const childProcess = require('child_process');
const parseColumns = require('parse-columns');
childProcess.execFile('df', ['-kP'], function (err, stdout) {
const execFileP = promisify(childProcess.execFile);
(async () => {
const {stdout} = await execFileP('df', ['-kP']);
console.log(parseColumns(stdout, {
transform: function (el, header, columnIndex) {
// coerce elements in column index 1 to 3 to a number
transform: (item, header, columnIndex) => {
// Coerce elements in column index 1 to 3 to a number
if (columnIndex >= 1 && columnIndex <= 3) {
return Number(el);
return Number(item);
}
return el;
return item;
}
}));
/*
[{
Filesystem: '/dev/disk1',
'1024-blocks': 487350400,
Used: 467528020,
Available: 19566380,
Capacity: '96%',
'Mounted on': '/'
}, ...]
[
{
Filesystem: '/dev/disk1',
'1024-blocks': 487350400,
Used: 467528020,
Available: 19566380,
Capacity: '96%',
'Mounted on': '/'
},
]
*/
});
})();
```

@@ -56,7 +64,6 @@

### parseColumns(input, [options])
### parseColumns(textColumns, [options])
#### input
#### textColumns
*Required*
Type: `string`

@@ -68,5 +75,7 @@

Type: `object`
##### separator
Type: `string`
Type: `string`
Default: `' '`

@@ -78,3 +87,3 @@

Type: `array`
Type: `string[]`

@@ -85,3 +94,3 @@ Headers to use instead of the existing ones.

Type: `function`
Type: `Function`

@@ -107,2 +116,2 @@ Transform elements.

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc