Skip to content

Commit 42b658f

Browse files
authored
Merge pull request #203 from webdeveric/dev
Added `isJSONable()`
2 parents c932a5a + 429b1fd commit 42b658f

7 files changed

Lines changed: 445 additions & 404 deletions

File tree

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"url": "https://github.com/webdeveric/utils/issues"
6767
},
6868
"homepage": "https://github.com/webdeveric/utils/#readme",
69-
"packageManager": "pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748",
69+
"packageManager": "pnpm@10.15.1+sha512.34e538c329b5553014ca8e8f4535997f96180a1d0f614339357449935350d924e22f8614682191264ec33d1462ac21561aff97f6bb18065351c162c7e8f6de67",
7070
"scripts": {
7171
"clean": "rimraf ./dist/",
7272
"prebuild": "pnpm clean",
@@ -87,21 +87,21 @@
8787
"devDependencies": {
8888
"@commitlint/config-conventional": "^19.8.1",
8989
"@commitlint/types": "^19.8.1",
90-
"@types/node": "^22.17.2",
90+
"@types/node": "^22.18.0",
9191
"@vitest/coverage-v8": "^3.2.4",
9292
"@webdeveric/eslint-config-ts": "^0.11.0",
9393
"@webdeveric/prettier-config": "^0.3.0",
9494
"commitlint": "^19.8.1",
9595
"commitlint-plugin-cspell": "^0.3.0",
9696
"conventional-changelog-conventionalcommits": "^9.1.0",
97-
"cspell": "^9.2.0",
97+
"cspell": "^9.2.1",
9898
"eslint": "^8.57.1",
9999
"eslint-config-prettier": "^10.1.8",
100100
"eslint-import-resolver-typescript": "^4.4.4",
101101
"eslint-plugin-import": "^2.32.0",
102102
"husky": "^9.1.7",
103103
"jsdom": "^26.1.0",
104-
"lint-staged": "^16.1.5",
104+
"lint-staged": "^16.1.6",
105105
"prettier": "^3.6.2",
106106
"rimraf": "^6.0.1",
107107
"semantic-release": "^24.2.7",

pnpm-lock.yaml

Lines changed: 377 additions & 368 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/comment.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,34 @@ export type CommentOptions = {
1616
type?: CommentType | `${CommentType}`;
1717
} & Partial<CommentParts>;
1818

19-
const commentTypes: Record<CommentType, CommentParts> = {
20-
[CommentType.Block]: {
21-
commentStart: '/**\n',
22-
commentEnd: '\n */',
23-
lineStart: ' * ',
24-
lineEnd: '',
25-
},
26-
[CommentType.Legal]: {
27-
commentStart: '/*!\n',
28-
commentEnd: '\n */',
29-
lineStart: ' * ',
30-
lineEnd: '',
31-
},
32-
[CommentType.Single]: {
33-
commentStart: '',
34-
commentEnd: '',
35-
lineStart: '// ',
36-
lineEnd: '',
37-
},
38-
[CommentType.HTML]: {
39-
commentStart: '<!-- ',
40-
commentEnd: ' -->',
41-
lineStart: '',
42-
lineEnd: '',
43-
},
44-
};
19+
function getCommentParts(type: CommentType | `${CommentType}`): CommentParts {
20+
return {
21+
[CommentType.Block]: {
22+
commentStart: '/**\n',
23+
commentEnd: '\n */',
24+
lineStart: ' * ',
25+
lineEnd: '',
26+
},
27+
[CommentType.Legal]: {
28+
commentStart: '/*!\n',
29+
commentEnd: '\n */',
30+
lineStart: ' * ',
31+
lineEnd: '',
32+
},
33+
[CommentType.Single]: {
34+
commentStart: '',
35+
commentEnd: '',
36+
lineStart: '// ',
37+
lineEnd: '',
38+
},
39+
[CommentType.HTML]: {
40+
commentStart: '<!-- ',
41+
commentEnd: ' -->',
42+
lineStart: '',
43+
lineEnd: '',
44+
},
45+
}[type];
46+
}
4547

4648
export function comment(text: string, options?: CommentOptions): string {
4749
if (!text) {
@@ -56,7 +58,7 @@ export function comment(text: string, options?: CommentOptions): string {
5658
lineStart = '',
5759
lineEnd = '',
5860
} = {
59-
...commentTypes[type],
61+
...getCommentParts(type),
6062
...partOptions,
6163
};
6264

src/predicate/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from './isInteger.js';
2121
export * from './isIntString.js';
2222
export * from './isISODateString.js';
2323
export * from './isIterable.js';
24+
export * from './isJSONable.js';
2425
export * from './isLengthAware.js';
2526
export * from './isNonNullable.js';
2627
export * from './isNull.js';

src/predicate/isJSONable.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { JsonValue } from '../types/common.js';
2+
3+
export const isJSONable = (input: unknown): input is { toJSON: (key: string) => JsonValue } => {
4+
return (
5+
input !== null &&
6+
typeof input === 'object' &&
7+
'toJSON' in input &&
8+
typeof input.toJSON === 'function' &&
9+
input.toJSON.length <= 1 // maybe uses the `key` argument
10+
);
11+
};

src/trimIndentation.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
const isWhitespaceOnly = (input: string): boolean => /^\s*$/.test(input);
2-
31
export function trimIndentation(input: string): string {
42
const lines = input.split('\n');
5-
const whiteSpace = lines.find((line) => !isWhitespaceOnly(line))?.match(/^(?<whiteSpace>\s*)/)?.groups?.[
6-
'whiteSpace'
7-
];
3+
// Find indentation on first line that isn't whitespace only.
4+
const whiteSpace = lines.find((line) => !/^\s*$/.test(line))?.match(/^(?<whiteSpace>\s*)/)?.groups?.['whiteSpace'];
85

96
if (whiteSpace) {
107
const wsPattern = new RegExp(`^${whiteSpace}`);

test/predicate/isJSONable.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { isJSONable } from '../../src/predicate/isJSONable.js';
4+
5+
import type { JsonValue } from '../../src/index.js';
6+
7+
describe('isJSONable()', () => {
8+
it('should return true for objects with a toJSON method', () => {
9+
const obj = {
10+
toJSON(key: string): JsonValue {
11+
return { key };
12+
},
13+
};
14+
15+
expect(isJSONable(obj)).toBe(true);
16+
});
17+
18+
it.each([{}, null, true, 'test', 123])('should return false for objects without a toJSON method: %j', (input) => {
19+
expect(isJSONable(input)).toBe(false);
20+
});
21+
});

0 commit comments

Comments
 (0)