Skip to content

Commit

Permalink
Switch to ESM only, and update everything (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell authored May 14, 2022
1 parent 67719d4 commit d4d0249
Show file tree
Hide file tree
Showing 13 changed files with 1,579 additions and 5,497 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

42 changes: 42 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

module.exports = {
root: true,
extends: ["eslint:recommended"],
parserOptions: { sourceType: "module" },
env: {
es2020: true,
node: true,
},
rules: {
curly: "error",
"no-restricted-syntax": [
"error",
{
selector: "SequenceExpression",
message:
"The comma operator is confusing and a common mistake. Don’t use it!",
},
],
"no-var": "error",
"object-shorthand": "error",
"one-var": ["error", "never"],
"prefer-arrow-callback": "error",
"prefer-const": "error",
"prefer-destructuring": [
"error",
{
object: true,
array: false,
},
],
"prefer-exponentiation-operator": "error",
"prefer-numeric-literals": "error",
"prefer-object-spread": "error",
"prefer-promise-reject-errors": "error",
"prefer-regex-literals": "error",
"prefer-rest-params": "error",
"prefer-spread": "error",
"prefer-template": "error",
},
};
21 changes: 0 additions & 21 deletions .eslintrc.js

This file was deleted.

14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: 14.x
node-version: 18.x

- name: Cache node_modules
id: cache-node_modules
uses: actions/cache@v1
uses: actions/cache@v3
with:
path: node_modules
key: node_modules-${{ hashFiles('package-lock.json') }}
key: node_modules-${{ hashFiles('package.json', 'package-lock.json') }}

- name: npm ci
if: steps.cache-node_modules.outputs.cache-hit != 'true'
Expand All @@ -35,5 +35,5 @@ jobs:
- name: Prettier
run: npx --no-install prettier --check .

- name: Jest
run: npx --no-install jest --coverage
- name: test
run: node --test
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
coverage
node_modules
2 changes: 0 additions & 2 deletions .prettierignore

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014, 2016, 2017, 2019, 2021 Simon Lydell
Copyright (c) 2014, 2016, 2017, 2019, 2021, 2022 Simon Lydell

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ npm install json-stringify-pretty-compact
```

```js
const stringify = require("json-stringify-pretty-compact");
import stringify from "json-stringify-pretty-compact";
```

> **Note:** This is an [ESM only package]. (I haven’t written that gist, but it’s a great resource.)
>
> If you need CommonJS, install version 3.0.0. You won’t be missing out on anything: This package is _done._ No more features will be added, and no bugs have been found in years.
## `stringify(obj, options = {})`

It’s like `JSON.stringify(obj, options.replacer, options.indent)`, except that objects and arrays are on one line if they fit (according to `options.maxLength`).
Expand All @@ -50,3 +54,4 @@ It’s like `JSON.stringify(obj, options.replacer, options.indent)`, except that

[@aitodotai/json-stringify-pretty-compact]: https://www.npmjs.com/package/@aitodotai/json-stringify-pretty-compact
[json.stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
[esm only package]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
56 changes: 26 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
"use strict";

// Note: This regex matches even invalid JSON strings, but since we’re
// working on the output of `JSON.stringify` we know that only valid strings
// are present (unless the user supplied a weird `options.indent` but in
// that case we don’t care since the output would be invalid anyway).
var stringOrChar = /("(?:[^\\"]|\\.)*")|[:,]/g;

module.exports = function stringify(passedObj, options) {
var indent, maxLength, replacer;
const stringOrChar = /("(?:[^\\"]|\\.)*")|[:,]/g;

options = options || {};
indent = JSON.stringify(
export default function stringify(passedObj, options = {}) {
const indent = JSON.stringify(
[1],
undefined,
options.indent === undefined ? 2 : options.indent
).slice(2, -3);
maxLength =

const maxLength =
indent === ""
? Infinity
: options.maxLength === undefined
? 80
: options.maxLength;
replacer = options.replacer;

return (function _stringify(obj, currentIndent, reserved) {
// prettier-ignore
var end, index, items, key, keyPart, keys, length, nextIndent, prettified, start, string, value;
let { replacer } = options;

return (function _stringify(obj, currentIndent, reserved) {
if (obj && typeof obj.toJSON === "function") {
obj = obj.toJSON();
}

string = JSON.stringify(obj, replacer);
const string = JSON.stringify(obj, replacer);

if (string === undefined) {
return string;
}

length = maxLength - currentIndent.length - reserved;
const length = maxLength - currentIndent.length - reserved;

if (string.length <= length) {
prettified = string.replace(
const prettified = string.replace(
stringOrChar,
function (match, stringLiteral) {
return stringLiteral || match + " ";
(match, stringLiteral) => {
return stringLiteral || `${match} `;
}
);
if (prettified.length <= length) {
Expand All @@ -57,14 +51,16 @@ module.exports = function stringify(passedObj, options) {
}

if (typeof obj === "object" && obj !== null) {
nextIndent = currentIndent + indent;
items = [];
index = 0;
const nextIndent = currentIndent + indent;
const items = [];
let index = 0;
let start;
let end;

if (Array.isArray(obj)) {
start = "[";
end = "]";
length = obj.length;
const { length } = obj;
for (; index < length; index++) {
items.push(
_stringify(obj[index], nextIndent, index === length - 1 ? 0 : 1) ||
Expand All @@ -74,12 +70,12 @@ module.exports = function stringify(passedObj, options) {
} else {
start = "{";
end = "}";
keys = Object.keys(obj);
length = keys.length;
const keys = Object.keys(obj);
const { length } = keys;
for (; index < length; index++) {
key = keys[index];
keyPart = JSON.stringify(key) + ": ";
value = _stringify(
const key = keys[index];
const keyPart = `${JSON.stringify(key)}: `;
const value = _stringify(
obj[key],
nextIndent,
keyPart.length + (index === length - 1 ? 0 : 1)
Expand All @@ -91,12 +87,12 @@ module.exports = function stringify(passedObj, options) {
}

if (items.length > 0) {
return [start, indent + items.join(",\n" + nextIndent), end].join(
"\n" + currentIndent
return [start, indent + items.join(`,\n${nextIndent}`), end].join(
`\n${currentIndent}`
);
}
}

return string;
})(passedObj, "", 0);
};
}
Loading

0 comments on commit d4d0249

Please sign in to comment.