Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 4, 2021
1 parent 3b7a5b1 commit c7c5e8e
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 145 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
100 changes: 47 additions & 53 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
declare namespace parseColumns {
interface Options<ValuesType = string> {
/**
Separator to split columns on.
export interface Options<Value = string> {
/**
Separator to split columns on.
@default ' '
*/
readonly separator?: string;
@default ' '
*/
readonly separator?: string;

/**
Headers to use instead of the existing ones.
*/
readonly headers?: readonly string[];
/**
Headers to use instead of the existing ones.
*/
readonly headers?: readonly string[];

/**
Transform elements.
/**
Transform elements.
Useful for being able to cleanup or change the type of elements.
*/
readonly transform?: (
element: string,
header: string,
columnIndex: number,
rowIndex: number
) => ValuesType;
}
Useful for being able to cleanup or change the type of elements.
*/
readonly transform?: (
element: string,
header: string,
columnIndex: number,
rowIndex: number
) => Value;
}

/**
Expand All @@ -33,42 +31,38 @@ Parse text columns, like the output of Unix commands.
@example
```
import {promisify} from 'util';
import * as childProcess from 'child_process';
import parseColumns = require('parse-columns');
import {promisify} from 'node:util';
import childProcess from 'node:child_process';
import parseColumns from 'parse-columns';
const execFileP = promisify(childProcess.execFile);
(async () => {
const {stdout} = await execFileP('df', ['-kP']);
console.log(parseColumns(stdout, {
transform: (item, header, columnIndex) => {
// Coerce elements in column index 1 to 3 to a number
if (columnIndex >= 1 && columnIndex <= 3) {
return Number(item);
}
const {stdout} = await execFileP('df', ['-kP']);
return item;
console.log(parseColumns(stdout, {
transform: (item, header, columnIndex) => {
// Coerce elements in column index 1 to 3 to a number
if (columnIndex >= 1 && columnIndex <= 3) {
return Number(item);
}
}));
// [
// {
// Filesystem: '/dev/disk1',
// '1024-blocks': 487350400,
// Used: 467528020,
// Available: 19566380,
// Capacity: '96%',
// 'Mounted on': '/'
// },
// …
// ]
})();
return item;
}
}));
// [
// {
// Filesystem: '/dev/disk1',
// '1024-blocks': 487350400,
// Used: 467528020,
// Available: 19566380,
// Capacity: '96%',
// 'Mounted on': '/'
// },
// …
// ]
```
*/
declare function parseColumns<ValuesType = string>(
export default function parseColumns<Value = string>(
textColumns: string,
options?: parseColumns.Options<ValuesType>
): Array<{[key: string]: ValuesType}>;

export = parseColumns;
options?: Options<Value>
): Array<Record<string, Value>>;
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const execall = require('execall');
const splitAt = require('split-at');
const escapeStringRegexp = require('escape-string-regexp');
import execall from 'execall';
import splitAt from 'split-at';
import escapeStringRegexp from 'escape-string-regexp';

/*
Algorithm:
Expand Down Expand Up @@ -46,7 +45,7 @@ const getSplits = (lines, separator) => {
return splits;
};

module.exports = (input, options = {}) => {
export default function parseColumns(input, options = {}) {
const lines = input.replace(/^\s*\n|\s+$/g, '').split('\n');
let splits = getSplits(lines, options.separator);
const {transform} = options;
Expand Down Expand Up @@ -83,4 +82,4 @@ module.exports = (input, options = {}) => {
}

return rows;
};
}
18 changes: 9 additions & 9 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {expectType} from 'tsd';
import parseColumns = require('.');
import parseColumns from './index.js';

expectType<Array<{[key: string]: string}>>(parseColumns('foo'));
expectType<Array<{[key: string]: string}>>(
parseColumns('foo', {separator: ' '})
expectType<Array<Record<string, string>>>(parseColumns('foo'));
expectType<Array<Record<string, string>>>(
parseColumns('foo', {separator: ' '}),
);
expectType<Array<{[key: string]: string}>>(
parseColumns('foo', {headers: ['foo', 'bar']})
expectType<Array<Record<string, string>>>(
parseColumns('foo', {headers: ['foo', 'bar']}),
);
expectType<Array<{[key: string]: string | number}>>(
expectType<Array<Record<string, string | number>>>(
parseColumns('foo', {
transform(element, header, columnIndex, rowIndex) {
expectType<string>(element);
Expand All @@ -21,6 +21,6 @@ expectType<Array<{[key: string]: string | number}>>(
}

return element;
}
})
},
}),
);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

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:

Expand Down
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Parse text columns, like the output of Unix commands",
"license": "MIT",
"repository": "sindresorhus/parse-columns",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -37,13 +40,13 @@
"table"
],
"dependencies": {
"escape-string-regexp": "^2.0.0",
"execall": "^2.0.0",
"split-at": "^2.0.0"
"escape-string-regexp": "^5.0.0",
"execall": "^3.0.0",
"split-at": "^3.0.0"
},
"devDependencies": {
"ava": "^2.4.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.18.0",
"xo": "^0.46.4"
}
}
71 changes: 30 additions & 41 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

> Parse text columns, like the output of Unix commands

## Install

```sh
npm install parse-columns
```
$ npm install parse-columns
```


## Usage

Expand All @@ -21,51 +19,48 @@ map -hosts 0 0 0 100% /net
```

```js
const {promisify} = require('util');
const childProcess = require('child_process');
const parseColumns = require('parse-columns');
import {promisify} from 'node:util';
import childProcess from 'node:child_process';
import parseColumns from 'parse-columns';

const execFileP = promisify(childProcess.execFile);

(async () => {
const {stdout} = await execFileP('df', ['-kP']);
const {stdout} = await execFileP('df', ['-kP']);

console.log(parseColumns(stdout, {
transform: (item, header, columnIndex) => {
// Coerce elements in column index 1 to 3 to a number
if (columnIndex >= 1 && columnIndex <= 3) {
return Number(item);
}

return item;
console.log(parseColumns(stdout, {
transform: (item, header, columnIndex) => {
// Coerce elements in column index 1 to 3 to a number
if (columnIndex >= 1 && columnIndex <= 3) {
return Number(item);
}
}));
/*
[
{
Filesystem: '/dev/disk1',
'1024-blocks': 487350400,
Used: 467528020,
Available: 19566380,
Capacity: '96%',
'Mounted on': '/'
},
]
*/
})();
```

return item;
}
}));
/*
[
{
Filesystem: '/dev/disk1',
'1024-blocks': 487350400,
Used: 467528020,
Available: 19566380,
Capacity: '96%',
'Mounted on': '/'
},
]
*/
```

## API

### parseColumns(textColumns, [options])
### parseColumns(textColumns, options?)

#### textColumns

Type: `string`

Text columns to parse.
The text columns to parse.

#### options

Expand Down Expand Up @@ -99,12 +94,6 @@ The supplied function gets the following arguments and is expected to return the
- `columnIndex` *(number)*
- `rowIndex` *(number)*


## Related

- [parse-columns-cli](https://github.com/sindresorhus/parse-columns-cli) - CLI for this module


## License

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

0 comments on commit c7c5e8e

Please sign in to comment.