-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmarker.ts
31 lines (27 loc) · 890 Bytes
/
marker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { dataTypes } from '../constant';
const constructorString = Object.prototype.constructor.toString();
/**
* Check if the value is a simple object(No prototype chain object or iframe same-origin object),
* support case: https://github.com/unadlib/mutative/issues/17
*/
const isSimpleObject = (value: unknown) => {
if (!value || typeof value !== 'object') return false;
const prototype = Object.getPrototypeOf(value);
if (prototype === null) {
return true;
}
const constructor =
Object.hasOwnProperty.call(prototype, 'constructor') &&
prototype.constructor;
if (constructor === Object) return true;
return (
typeof constructor === 'function' &&
Function.toString.call(constructor) === constructorString
);
};
export const markSimpleObject = (value: unknown) => {
if (isSimpleObject(value)) {
return dataTypes.immutable;
}
return;
};