Skip to content

Commit

Permalink
feat: treat module imports as records
Browse files Browse the repository at this point in the history
fix #133
  • Loading branch information
RebeccaStevens committed Jun 13, 2022
1 parent 6becb68 commit 20c0dfb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@
"test:js": "c8 ava",
"test:types": "dtslint --localTs node_modules/typescript/lib --expectOnly tests/types"
},
"dependencies": {
"is-plain-object": "^5.0.0"
},
"devDependencies": {
"@commitlint/cli": "^17.0.2",
"@commitlint/config-conventional": "^17.0.2",
Expand Down
48 changes: 45 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isPlainObject } from "is-plain-object";

/**
* The different types of objects deepmerge-ts support.
*/
Expand Down Expand Up @@ -27,7 +25,7 @@ export function getObjectType(object: unknown): ObjectType {
return ObjectType.ARRAY;
}

if (isPlainObject(object)) {
if (isRecord(object)) {
return ObjectType.RECORD;
}

Expand Down Expand Up @@ -102,3 +100,47 @@ export function getIterableOfIterables<T>(
},
};
}

const validRecordToStringValues = new Set([
"[object Object]",
"[object Module]",
]);

/**
* Does the given object appear to be a record.
*/
function isRecord(value: object): value is Record<PropertyKey, unknown> {
// All records are objects.
if (!validRecordToStringValues.has(Object.prototype.toString.call(value))) {
return false;
}

const { constructor } = value;

// If has modified constructor.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (constructor === undefined) {
return true;
}

// eslint-disable-next-line prefer-destructuring
const prototype: unknown = constructor.prototype;

// If has modified prototype.
if (
prototype === null ||
typeof prototype !== "object" ||
!validRecordToStringValues.has(Object.prototype.toString.call(prototype))
) {
return false;
}

// If constructor does not have an Object-specific method.
// eslint-disable-next-line sonarjs/prefer-single-boolean-return, no-prototype-builtins
if (!prototype.hasOwnProperty("isPrototypeOf")) {
return false;
}

// Most likely a record.
return true;
}
1 change: 0 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3488,7 +3488,6 @@ __metadata:
eslint-plugin-unicorn: ^42.0.0
husky: ^8.0.1
if-env-defined: ^1.0.0
is-plain-object: ^5.0.0
lint-staged: ^13.0.1
lodash: ^4.17.21
markdownlint-cli: ^0.31.1
Expand Down

0 comments on commit 20c0dfb

Please sign in to comment.