Skip to content

Commit b671650

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent 56a7297 commit b671650

5 files changed

Lines changed: 107 additions & 100 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
17-
- 6
13+
- 16
1814
steps:
1915
- uses: actions/checkout@v2
20-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
2117
with:
2218
node-version: ${{ matrix.node-version }}
2319
- run: npm install

index.js

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
'use strict';
2-
const isRegexp = require('is-regexp');
3-
const isObj = require('is-obj');
4-
const getOwnEnumPropSymbols = require('get-own-enumerable-property-symbols').default;
1+
import isRegexp from 'is-regexp';
2+
import isObject from 'is-obj';
3+
import getOwnEnumPropSymbols from 'get-own-enumerable-property-symbols';
54

6-
module.exports = (input, options, pad) => {
5+
export default function stringifyObject(input, options, pad) {
76
const seen = [];
87

98
return (function stringify(input, options = {}, pad = '') {
10-
options.indent = options.indent || '\t';
9+
const indent = options.indent || '\t';
1110

1211
let tokens;
13-
1412
if (options.inlineCharacterLimit === undefined) {
1513
tokens = {
16-
newLine: '\n',
17-
newLineOrSpace: '\n',
14+
newline: '\n',
15+
newlineOrSpace: '\n',
1816
pad,
19-
indent: pad + options.indent
17+
indent: pad + indent,
2018
};
2119
} else {
2220
tokens = {
23-
newLine: '@@__STRINGIFY_OBJECT_NEW_LINE__@@',
24-
newLineOrSpace: '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@',
21+
newline: '@@__STRINGIFY_OBJECT_NEW_LINE__@@',
22+
newlineOrSpace: '@@__STRINGIFY_OBJECT_NEW_LINE_OR_SPACE__@@',
2523
pad: '@@__STRINGIFY_OBJECT_PAD__@@',
26-
indent: '@@__STRINGIFY_OBJECT_INDENT__@@'
24+
indent: '@@__STRINGIFY_OBJECT_INDENT__@@',
2725
};
2826
}
2927

@@ -33,31 +31,32 @@ module.exports = (input, options, pad) => {
3331
}
3432

3533
const oneLined = string
36-
.replace(new RegExp(tokens.newLine, 'g'), '')
37-
.replace(new RegExp(tokens.newLineOrSpace, 'g'), ' ')
34+
.replace(new RegExp(tokens.newline, 'g'), '')
35+
.replace(new RegExp(tokens.newlineOrSpace, 'g'), ' ')
3836
.replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), '');
3937

4038
if (oneLined.length <= options.inlineCharacterLimit) {
4139
return oneLined;
4240
}
4341

4442
return string
45-
.replace(new RegExp(tokens.newLine + '|' + tokens.newLineOrSpace, 'g'), '\n')
43+
.replace(new RegExp(tokens.newline + '|' + tokens.newlineOrSpace, 'g'), '\n')
4644
.replace(new RegExp(tokens.pad, 'g'), pad)
47-
.replace(new RegExp(tokens.indent, 'g'), pad + options.indent);
45+
.replace(new RegExp(tokens.indent, 'g'), pad + indent);
4846
};
4947

50-
if (seen.indexOf(input) !== -1) {
48+
if (seen.includes(input)) {
5149
return '"[Circular]"';
5250
}
5351

54-
if (input === null ||
55-
input === undefined ||
56-
typeof input === 'number' ||
57-
typeof input === 'boolean' ||
58-
typeof input === 'function' ||
59-
typeof input === 'symbol' ||
60-
isRegexp(input)
52+
if (
53+
input === null
54+
|| input === undefined
55+
|| typeof input === 'number'
56+
|| typeof input === 'boolean'
57+
|| typeof input === 'function'
58+
|| typeof input === 'symbol'
59+
|| isRegexp(input)
6160
) {
6261
return String(input);
6362
}
@@ -73,10 +72,10 @@ module.exports = (input, options, pad) => {
7372

7473
seen.push(input);
7574

76-
const ret = '[' + tokens.newLine + input.map((el, i) => {
77-
const eol = input.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace;
75+
const returnValue = '[' + tokens.newline + input.map((element, i) => {
76+
const eol = input.length - 1 === i ? tokens.newline : ',' + tokens.newlineOrSpace;
7877

79-
let value = stringify(el, options, pad + options.indent);
78+
let value = stringify(element, options, pad + indent);
8079
if (options.transform) {
8180
value = options.transform(input, i, value);
8281
}
@@ -86,39 +85,43 @@ module.exports = (input, options, pad) => {
8685

8786
seen.pop();
8887

89-
return expandWhiteSpace(ret);
88+
return expandWhiteSpace(returnValue);
9089
}
9190

92-
if (isObj(input)) {
93-
let objKeys = Object.keys(input).concat(getOwnEnumPropSymbols(input));
91+
if (isObject(input)) {
92+
let objectKeys = [
93+
...Object.keys(input),
94+
...getOwnEnumPropSymbols.default(input),
95+
];
9496

9597
if (options.filter) {
96-
objKeys = objKeys.filter(el => options.filter(input, el));
98+
// eslint-disable-next-line unicorn/no-array-callback-reference, unicorn/no-array-method-this-argument
99+
objectKeys = objectKeys.filter(element => options.filter(input, element));
97100
}
98101

99-
if (objKeys.length === 0) {
102+
if (objectKeys.length === 0) {
100103
return '{}';
101104
}
102105

103106
seen.push(input);
104107

105-
const ret = '{' + tokens.newLine + objKeys.map((el, i) => {
106-
const eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace;
107-
const isSymbol = typeof el === 'symbol';
108-
const isClassic = !isSymbol && /^[a-z$_][a-z$_0-9]*$/i.test(el);
109-
const key = isSymbol || isClassic ? el : stringify(el, options);
108+
const returnValue = '{' + tokens.newline + objectKeys.map((element, i) => {
109+
const eol = objectKeys.length - 1 === i ? tokens.newline : ',' + tokens.newlineOrSpace;
110+
const isSymbol = typeof element === 'symbol';
111+
const isClassic = !isSymbol && /^[a-z$_][$\w]*$/i.test(element);
112+
const key = isSymbol || isClassic ? element : stringify(element, options);
110113

111-
let value = stringify(input[el], options, pad + options.indent);
114+
let value = stringify(input[element], options, pad + indent);
112115
if (options.transform) {
113-
value = options.transform(input, el, value);
116+
value = options.transform(input, element, value);
114117
}
115118

116119
return tokens.indent + String(key) + ': ' + value + eol;
117120
}).join('') + tokens.pad + '}';
118121

119122
seen.pop();
120123

121-
return expandWhiteSpace(ret);
124+
return expandWhiteSpace(returnValue);
122125
}
123126

124127
input = String(input).replace(/[\r\n]/g, x => x === '\n' ? '\\n' : '\\r');
@@ -131,4 +134,4 @@ module.exports = (input, options, pad) => {
131134
input = input.replace(/\\?'/g, '\\\'');
132135
return `'${input}'`;
133136
})(input, options, pad);
134-
};
137+
}

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
"email": "sindresorhus@gmail.com",
1010
"url": "https://sindresorhus.com"
1111
},
12+
"type": "module",
13+
"exports": "./index.js",
1214
"engines": {
13-
"node": ">=6"
15+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1416
},
1517
"scripts": {
1618
"test": "xo && ava"
@@ -29,13 +31,13 @@
2931
"json"
3032
],
3133
"dependencies": {
32-
"get-own-enumerable-property-symbols": "^3.0.0",
33-
"is-obj": "^1.0.1",
34-
"is-regexp": "^2.0.0"
34+
"get-own-enumerable-property-symbols": "^3.0.2",
35+
"is-obj": "^3.0.0",
36+
"is-regexp": "^3.0.0"
3537
},
3638
"devDependencies": {
37-
"ava": "^0.25.0",
38-
"xo": "^0.23.0"
39+
"ava": "^3.15.0",
40+
"xo": "^0.44.0"
3941
},
4042
"xo": {
4143
"ignores": [

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $ npm install stringify-object
1515
## Usage
1616

1717
```js
18-
const stringifyObject = require('stringify-object');
18+
import stringifyObject from 'stringify-object';
1919

2020
const object = {
2121
foo: 'bar',
@@ -92,7 +92,7 @@ Expected to return a `string` that transforms the string that resulted from stri
9292
Here's an example that uses the `transform` option to mask fields named "password":
9393

9494
```js
95-
const stringifyObject = require('stringify-object');
95+
import stringifyObject from 'stringify-object';
9696

9797
const object = {
9898
user: 'becky',
@@ -127,7 +127,7 @@ When set, will inline values up to `inlineCharacterLimit` length for the sake of
127127
For example, given the example at the top of the README:
128128

129129
```js
130-
const stringifyObject = require('stringify-object');
130+
import stringifyObject from 'stringify-object';
131131

132132
const object = {
133133
foo: 'bar',

0 commit comments

Comments
 (0)