-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathobject.ts
102 lines (95 loc) · 2.31 KB
/
object.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
93
94
95
96
97
98
99
100
101
102
import { is_null } from './null'
/**
* get object type
*
* @param object - any object
* @return Object type string
*/
export function object_type(object: any): string {
const iden = toString.call(object)
return iden.substring(8, iden.length - 1)
}
/**
* Test value is object
*
* @param value - test value
* @returns test results
* @example ```ts
* is_object({}) // true
* is_object(null) // false
* ```
*/
export function is_object(value: unknown): value is object {
return 'object' === typeof value && !is_null(value)
}
//#region null prototype object
/**
* Create object with null prototype
*
* @param object - object
* @returns null object
*/
export function create_null_object<T extends object>(object?: T): T {
const obj = Object.create(null)
if(!object) return obj
return Object.assign(obj, object)
}
/**
* Test a object is null prototype
*
* @param object - object
* @returns test result
* @example ```ts
* is_null_object(Object.create(null)) // true
* is_null_object({}) // false
* ```
*/
export function is_null_object<T extends object>(object: T) {
return is_null(Object.getPrototypeOf(object))
}
//#endregion
//#region enumerable
/**
* Set property unenumerable
*
* @param object - object
* @param property - property name
* @example ```ts
* const obj = { foo: 42 }
* set_unenumerable(obj, 'foo')
* obj.propertyIsEnumerable('foo') // false
* ```
*/
export function set_unenumerable<T extends object>(object: T, property: keyof T) {
Object.defineProperty(object, property, { enumerable: false })
}
/**
* Set property enumerable
*
* @param object - object
* @param property - property name
* @example ```ts
* const obj = { foo: 42 }
* set_unenumerable(obj, 'foo')
* obj.propertyIsEnumerable('foo') // true
* ```
*/
export function set_enumerable<T extends object>(object: T, property: keyof T) {
Object.defineProperty(object, property, { enumerable: true })
}
/**
* Test property is enumerable
*
* @param object - object
* @param property - property name
* @returns test result
* @example ```ts
* const obj = { foo: 42 }
* is_enumerable(obj, 'foo') // true
* is_enumerable(obj, 'bar') // false
* ```
*/
export function is_enumerable<T extends object>(object: T, property: keyof T) {
return object.propertyIsEnumerable(property)
}
//#endregion