Skip to content

Commit 574422b

Browse files
authored
feat: expose isPlainObject util (#92)
1 parent f25d11d commit 574422b

File tree

7 files changed

+30
-32
lines changed

7 files changed

+30
-32
lines changed

src/__tests__/_utils.spec.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { isPlainObject } from '../isPlainObject';
2+
3+
it('isPlainObject', () => {
4+
expect(isPlainObject(new class Foo {}())).toBe(false);
5+
expect(isPlainObject([1, 2, 3])).toBe(false);
6+
7+
expect(isPlainObject({ x: 0, y: 0 })).toBe(true);
8+
expect(isPlainObject(Object.create(null))).toBe(true);
9+
10+
const a = new class {}();
11+
a.constructor = Object;
12+
expect(isPlainObject(a)).toBe(true);
13+
});

src/_utils.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,3 @@ export const replaceInString = (str: string, find: string, repl: string): string
2626

2727
return res;
2828
};
29-
30-
export function isPlainObject(maybeObj: unknown): maybeObj is { [key in PropertyKey]: unknown } {
31-
if (typeof maybeObj !== 'object' || maybeObj === null) {
32-
return false;
33-
}
34-
35-
const proto = Object.getPrototypeOf(maybeObj);
36-
return (
37-
proto === null ||
38-
proto === Object.prototype ||
39-
// this is to be more compatible with Lodash.isPlainObject that also checks the constructor
40-
(typeof maybeObj.constructor === 'function' &&
41-
Function.toString.call(Object) === Function.toString.call(maybeObj.constructor))
42-
);
43-
}

src/decycle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isPlainObject } from './_utils';
1+
import { isPlainObject } from './isPlainObject';
22
import { pathToPointer } from './pathToPointer';
33

44
export const decycle = (obj: unknown, replacer?: (value: any) => any) => {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from './getLastPathSegment';
1212
export * from './getLocationForJsonPath';
1313
export * from './hasRef';
1414
export * from './isLocalRef';
15+
export * from './isPlainObject';
1516
export * from './parseWithPointers';
1617
export * from './pathToPointer';
1718
export * from './pointerToPath';

src/isPlainObject.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function isPlainObject(maybeObj: unknown): maybeObj is Record<PropertyKey, unknown> {
2+
if (typeof maybeObj !== 'object' || maybeObj === null) {
3+
return false;
4+
}
5+
6+
const proto = Object.getPrototypeOf(maybeObj);
7+
return (
8+
proto === null ||
9+
proto === Object.prototype ||
10+
// this is to be more compatible with Lodash.isPlainObject that also checks the constructor
11+
(typeof maybeObj.constructor === 'function' &&
12+
Function.toString.call(Object) === Function.toString.call(maybeObj.constructor))
13+
);
14+
}

src/resolveInlineRef.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Dictionary } from '@stoplight/types';
2-
import { isPlainObject } from './_utils';
32
import { extractSourceFromRef } from './extractSourceFromRef';
3+
import { isPlainObject } from './isPlainObject';
44
import { pointerToPath } from './pointerToPath';
55

66
function _resolveInlineRef(document: Dictionary<unknown>, ref: string, seen: unknown[]): unknown {

0 commit comments

Comments
 (0)