-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhas_own.ts
92 lines (89 loc) · 4.06 KB
/
has_own.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* @nevware21/ts-utils
* https://github.com/nevware21/ts-utils
*
* Copyright (c) 2022 NevWare21 Solutions LLC
* Licensed under the MIT license.
*/
import { ObjClass } from "../internal/constants";
import { _pureAssign, _pureRef } from "../internal/treeshake_helpers";
import { objGetOwnPropertyDescriptor } from "./get_own_prop_desc";
import { objHasOwnProperty } from "./has_own_prop";
/**
* The objHasOwn() method returns a boolean indicating whether the object
* has the specified property as its own property (as opposed to inheriting it).
* If the property is inherited, or does not exist, the method returns false.
*
* The objHasOwn() method returns true if the specified property is a direct property
* of the object — even if the property value is null or undefined. The method returns
* false if the property is inherited, or has not been declared at all. Unlike the in operator,
* this method does not check for the specified property in the object's prototype chain.
*
* It is recommended over {@link objHasOwnProperty} () because it works for objects created using
* objCreate(null) and with objects that have overridden the inherited hasOwnProperty() method.
* While it is possible to workaround these problems by calling Object.prototype.hasOwnProperty()
* on an external object, Object.hasOwn() is more intuitive.
*
* @since 0.4.3
* @group Object
* @param obj - The object being evaluated
* @param prop - The String or Symbol of the property to test
* @returns `true` if the object has the specified property as own property; otherwise `false`
* @example
* ```ts
* let example = {};
* objHasOwn(example, 'prop'); // false
*
* example.prop = 'exists';
* objHasOwn(example, 'prop'); // true - 'prop' has been defined
*
* example.prop = null;
* objHasOwn(example, 'prop'); // true - own property exists with value of null
*
* example.prop = undefined;
* objHasOwn(example, 'prop'); // true - own property exists with value of undefined
* ```
*/
export const objHasOwn: <T = any>(obj: T, prop: PropertyKey) => boolean = (/*#__PURE__*/_pureAssign((/* #__PURE__ */_pureRef(ObjClass as any, "hasOwn")), polyObjHasOwn));
/**
* The polyObjHasOwn() method is a polyfill for {@link objHasOwn} when the native
* [Object.hasOwnreturns](https://caniuse.com/?search=hasOwn) is not supported, it returns a
* boolean indicating whether the object has the specified property as its own property (as
* opposed to inheriting it). If the property is inherited, or does not exist, the method
* returns false.
*
* The objHasOwn() method returns true if the specified property is a direct property
* of the object — even if the property value is null or undefined. The method returns
* false if the property is inherited, or has not been declared at all. Unlike the in operator,
* this method does not check for the specified property in the object's prototype chain.
*
* It is recommended over objHasOwnProperty() because it works for objects created using
* objCreate(null) and with objects that have overridden the inherited hasOwnProperty() method.
* While it is possible to workaround these problems by calling Object.prototype.hasOwnProperty()
* on an external object, Object.hasOwn() is more intuitive.
*
* @since 0.4.3
* @group Object
* @group Polyfill
* @param obj - The object being evaluated
* @param prop - The String or Symbol of the property to test
* @returns `true` if the object has the specified property as own property; otherwise `false`
* @example
* ```ts
* let example = {};
* polyObjHasOwn(example, 'prop'); // false
*
* example.prop = 'exists';
* polyObjHasOwn(example, 'prop'); // true - 'prop' has been defined
*
* example.prop = null;
* polyObjHasOwn(example, 'prop'); // true - own property exists with value of null
*
* example.prop = undefined;
* polyObjHasOwn(example, 'prop'); // true - own property exists with value of undefined
* ```
*/
/*#__NO_SIDE_EFFECTS__*/
export function polyObjHasOwn<T = any>(obj: T, prop: PropertyKey): boolean {
return objHasOwnProperty(obj, prop) || !!objGetOwnPropertyDescriptor(obj, prop)
}