-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.mjs
More file actions
66 lines (59 loc) · 1.56 KB
/
tools.mjs
File metadata and controls
66 lines (59 loc) · 1.56 KB
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
/**
* Object extension tools.
* @module
* @see module:utils
*/
import {
properties
} from "./utils.mjs"
/**
* build the argument for Object.defineProperty
* for non getter/setters
* @param {bool} writable
* @param {bool} enumerable
* @param {*} value
*/
export const PropertyDefinitionBuilder = (writable, enumerable, value) => ({
writable,
enumerable,
value
})
/**
* makes the argument for Object.defineProperty for build a constant property
* @function
* @param {*} value
*/
export const InternalConstDefiner = PropertyDefinitionBuilder.bind(void 0, false, false)
/**
* makes the argument for Object.defineProperty for build a variable property
* @function
* @param {*} value
*/
export const variableDefiner = PropertyDefinitionBuilder.bind(void 0, true, false)
/**
* makes the argument for Object.defineProperty for build a constant enumerable property
* @function
* @param {*} value
*/
export const constDefiner = PropertyDefinitionBuilder.bind(void 0, false, true)
/**
* add property if there isn't
* @param {*} name
* @param {*} value
* @param {*} filter
*/
export function buildProperty(name, value, filter = InternalConstDefiner){
if(!(name in this)){
Object.defineProperty(this, name, filter(value))
}
}
/**
* inject properties to an object
* @param {*} name
* @param {*} value
* @param {*} filter
*/
export function injectProperties(settings, filter = variableDefiner) {
const handler = (prev, curr) => properties.call(prev, curr, filter(settings[curr]))
return Object.defineProperties(this, Object.keys(settings).reduce(handler, {}))
}