Skip to content

Commit 3c3a155

Browse files
committedMay 3, 2021
Require Node.js 12 and move to ESM
1 parent 88490a0 commit 3c3a155

File tree

8 files changed

+105
-117
lines changed

8 files changed

+105
-117
lines changed
 

‎.github/workflows/main.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
16-
- 8
1716
steps:
1817
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
18+
- uses: actions/setup-node@v2
2019
with:
2120
node-version: ${{ matrix.node-version }}
2221
- run: npm install

‎index.d.ts

+79-85
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,78 @@
1-
declare namespace dargs {
2-
interface Options {
3-
/**
4-
Keys or regex of keys to exclude. Takes precedence over `includes`.
5-
*/
6-
excludes?: ReadonlyArray<string | RegExp>;
7-
8-
/**
9-
Keys or regex of keys to include.
10-
*/
11-
includes?: ReadonlyArray<string | RegExp>;
12-
13-
/**
14-
Maps keys in `input` to an aliased name. Matching keys are converted to arguments with a single dash (`-`) in front of the aliased key and the value in a separate array item. Keys are still affected by `includes` and `excludes`.
15-
*/
16-
aliases?: {[key: string]: string};
17-
18-
/**
19-
Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags.
20-
21-
@default true
22-
23-
@example
24-
```
25-
import dargs = require('dargs');
26-
27-
console.log(dargs({foo: 'bar'}, {useEquals: false}));
28-
// [
29-
// '--foo', 'bar'
30-
// ]
31-
```
32-
*/
33-
useEquals?: boolean;
34-
35-
/**
36-
Make a single character option key `{a: true}` become a short flag `-a` instead of `--a`.
37-
38-
@default true
39-
40-
@example
41-
```
42-
import dargs = require('dargs');
43-
44-
console.log(dargs({a: true}));
45-
//=> ['-a']
46-
47-
console.log(dargs({a: true}, {shortFlag: false}));
48-
//=> ['--a']
49-
```
50-
*/
51-
shortFlag?: boolean;
52-
53-
/**
54-
Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
55-
56-
@default false
57-
*/
58-
ignoreFalse?: boolean;
59-
60-
/**
61-
By default, camel-cased keys will be hyphenated. Enabling this will bypass the conversion process.
62-
63-
@default false
64-
65-
@example
66-
```
67-
import dargs = require('dargs');
68-
69-
console.log(dargs({fooBar: 'baz'}));
70-
//=> ['--foo-bar', 'baz']
71-
72-
console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
73-
//=> ['--fooBar', 'baz']
74-
```
75-
*/
76-
allowCamelCase?: boolean;
77-
}
1+
export interface Options {
2+
/**
3+
Keys or regex of keys to exclude. Takes precedence over `includes`.
4+
*/
5+
readonly excludes?: ReadonlyArray<string | RegExp>;
6+
7+
/**
8+
Keys or regex of keys to include.
9+
*/
10+
readonly includes?: ReadonlyArray<string | RegExp>;
11+
12+
/**
13+
Maps keys in `input` to an aliased name. Matching keys are converted to arguments with a single dash (`-`) in front of the aliased key and the value in a separate array item. Keys are still affected by `includes` and `excludes`.
14+
*/
15+
readonly aliases?: Record<string, string>;
16+
17+
/**
18+
Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags.
19+
20+
@default true
21+
22+
@example
23+
```
24+
import dargs from 'dargs';
25+
26+
console.log(dargs({foo: 'bar'}, {useEquals: false}));
27+
// [
28+
// '--foo', 'bar'
29+
// ]
30+
```
31+
*/
32+
readonly useEquals?: boolean;
33+
34+
/**
35+
Make a single character option key `{a: true}` become a short flag `-a` instead of `--a`.
36+
37+
@default true
38+
39+
@example
40+
```
41+
import dargs from 'dargs';
42+
43+
console.log(dargs({a: true}));
44+
//=> ['-a']
45+
46+
console.log(dargs({a: true}, {shortFlag: false}));
47+
//=> ['--a']
48+
```
49+
*/
50+
readonly shortFlag?: boolean;
51+
52+
/**
53+
Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
54+
55+
@default false
56+
*/
57+
readonly ignoreFalse?: boolean;
58+
59+
/**
60+
By default, camel-cased keys will be hyphenated. Enabling this will bypass the conversion process.
61+
62+
@default false
63+
64+
@example
65+
```
66+
import dargs from 'dargs';
67+
68+
console.log(dargs({fooBar: 'baz'}));
69+
//=> ['--foo-bar', 'baz']
70+
71+
console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
72+
//=> ['--fooBar', 'baz']
73+
```
74+
*/
75+
readonly allowCamelCase?: boolean;
7876
}
7977

8078
/**
@@ -84,7 +82,7 @@ Reverse [`minimist`](https://github.com/substack/minimist). Convert an object of
8482
8583
@example
8684
```
87-
import dargs = require('dargs');
85+
import dargs from 'dargs';
8886
8987
const input = {
9088
_: ['some', 'option'], // Values in '_' will be appended to the end of the generated argument list
@@ -124,7 +122,6 @@ console.log(dargs(input, {excludes, includes}));
124122
// '--multiple=value2'
125123
// ]
126124
127-
128125
console.log(dargs(input, {includes}));
129126
// [
130127
// '--camel-case=5',
@@ -134,7 +131,6 @@ console.log(dargs(input, {includes}));
134131
// '--sad=:('
135132
// ]
136133
137-
138134
console.log(dargs({
139135
foo: 'bar',
140136
hello: true,
@@ -147,12 +143,10 @@ console.log(dargs({
147143
// ]
148144
```
149145
*/
150-
declare function dargs(
146+
export default function dargs(
151147
object: {
152148
'--'?: string[];
153149
_?: string[];
154-
} & {[key: string]: string | boolean | number | readonly string[]},
155-
options?: dargs.Options
150+
} & Record<string, string | boolean | number | readonly string[]>,
151+
options?: Options
156152
): string[];
157-
158-
export = dargs;

‎index.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
31
const match = (array, value) =>
4-
array.some(x => (x instanceof RegExp ? x.test(value) : x === value));
2+
array.some(element => (element instanceof RegExp ? element.test(value) : element === value));
53

6-
const dargs = (object, options) => {
4+
export default function dargs(object, options) {
75
const arguments_ = [];
86
let extraArguments = [];
97
let separatedArguments = [];
@@ -115,6 +113,4 @@ const dargs = (object, options) => {
115113
}
116114

117115
return arguments_;
118-
};
119-
120-
module.exports = dargs;
116+
}

‎index.test-d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType, expectError} from 'tsd';
2-
import dargs = require('.');
2+
import dargs from './index.js';
33

44
const object = {
55
_: ['some', 'option'],

‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

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

55
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:
66

‎package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Reverse minimist. Convert an object of options into an array of command-line arguments.",
55
"license": "MIT",
66
"repository": "sindresorhus/dargs",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": ">=12"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd"
@@ -41,8 +44,8 @@
4144
"argv"
4245
],
4346
"devDependencies": {
44-
"ava": "^2.1.0",
45-
"tsd": "^0.7.3",
46-
"xo": "^0.24.0"
47+
"ava": "^3.15.0",
48+
"tsd": "^0.14.0",
49+
"xo": "^0.39.1"
4750
}
4851
}

‎readme.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
55
Useful when spawning command-line tools.
66

7-
87
## Install
98

109
```
1110
$ npm install dargs
1211
```
1312

14-
1513
## Usage
1614

1715
```js
18-
const dargs = require('dargs');
16+
import dargs from 'dargs';
1917

2018
const object = {
2119
_: ['some', 'option'], // Values in '_' will be appended to the end of the generated argument list
@@ -86,7 +84,6 @@ console.log(dargs({
8684
*/
8785
```
8886

89-
9087
## API
9188

9289
### dargs(object, options?)
@@ -121,13 +118,13 @@ Maps keys in `object` to an aliased name. Matching keys are converted to argumen
121118

122119
##### useEquals
123120

124-
Type: `boolean`<br>
121+
Type: `boolean`\
125122
Default: `true`
126123

127124
Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags.
128125

129126
```js
130-
const dargs = require('dargs');
127+
import dargs from 'dargs';
131128

132129
console.log(dargs({foo: 'bar'}, {useEquals: false}));
133130
/*
@@ -139,13 +136,13 @@ console.log(dargs({foo: 'bar'}, {useEquals: false}));
139136

140137
##### shortFlag
141138

142-
Type: `boolean`<br>
139+
Type: `boolean`\
143140
Default: `true`
144141

145142
Make a single character option key `{a: true}` become a short flag `-a` instead of `--a`.
146143

147144
```js
148-
const dargs = require('dargs');
145+
import dargs from 'dargs';
149146

150147
console.log(dargs({a: true}));
151148
//=> ['-a']
@@ -156,20 +153,20 @@ console.log(dargs({a: true}, {shortFlag: false}));
156153

157154
##### ignoreFalse
158155

159-
Type: `boolean`<br>
156+
Type: `boolean`\
160157
Default: `false`
161158

162159
Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`.
163160

164161
##### allowCamelCase
165162

166-
Type: `boolean`<br>
163+
Type: `boolean`\
167164
Default: `false`
168165

169166
By default, camel-cased keys will be hyphenated. Enabling this will bypass the conversion process.
170167

171168
```js
172-
const dargs = require('dargs');
169+
import dargs from 'dargs';
173170

174171
console.log(dargs({fooBar: 'baz'}));
175172
//=> ['--foo-bar', 'baz']
@@ -178,7 +175,6 @@ console.log(dargs({fooBar: 'baz'}, {allowCamelCase: true}));
178175
//=> ['--fooBar', 'baz']
179176
```
180177

181-
182178
---
183179

184180
<div align="center">

‎test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import dargs from '.';
2+
import dargs from './index.js';
33

44
const fixture = {
55
_: ['some', 'option'],
@@ -38,11 +38,11 @@ test('convert options to cli flags', t => {
3838
});
3939

4040
test('raises a TypeError if \'_\' value is not an Array', t => {
41-
t.throws(dargs.bind(dargs, {a: 'foo', _: 'baz'}), TypeError);
41+
t.throws(dargs.bind(dargs, {a: 'foo', _: 'baz'}), {instanceOf: TypeError});
4242
});
4343

4444
test('raises a TypeError if \'--\' value is not an Array', t => {
45-
t.throws(dargs.bind(dargs, {a: 'foo', '--': 'baz'}), TypeError);
45+
t.throws(dargs.bind(dargs, {a: 'foo', '--': 'baz'}), {instanceOf: TypeError});
4646
});
4747

4848
test('useEquals options', t => {

0 commit comments

Comments
 (0)
Please sign in to comment.