-
Notifications
You must be signed in to change notification settings - Fork 1
/
turboprop.js
80 lines (64 loc) · 2.28 KB
/
turboprop.js
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
const TURBOPROP_MARKER = Symbol('turboprop')
export const
TARGET_STRING = {
object: String.prototype,
methods: {
getter: (indexes, str) => indexes.reduce((newStr, index) => newStr + str[index], ''),
setter: false
}
},
TARGET_ARRAY = {
object: Array.prototype,
methods: {
getter: (indexes, arr) => indexes.reduce((newArr, index) => [...newArr, arr[index]], []),
setter: (indexes, values, arr) => indexes.forEach((index, i) => arr[index] = values[i])
}
},
TARGET_OBJECT = {
object: Object.prototype,
methods: {
getter: (keys, obj) => keys.reduce((newArr, key) => [...newArr, obj[key]], []),
setter: (keys, values, obj) => keys.forEach((key, i) => obj[key] = values[i])
}
}
const DEFAULT_TARGETS = [ TARGET_STRING, TARGET_ARRAY, TARGET_OBJECT ]
export function initialise(array = [], targetOrTargets = DEFAULT_TARGETS) {
const TURBOPROP_METHODS = Symbol('turboprop_methods')
const targets = Array.isArray(targetOrTargets) ? targetOrTargets : [targetOrTargets]
array[TURBOPROP_MARKER] = true
array[Symbol.toPrimitive] = function (hint) {
// retain normal behaviour of array coercion if asked for 'default' or 'number'
if (hint === 'default') return this.toString()
if (hint === 'number') return Number(this.toString())
const
keys = this.map(key => (Array.isArray(key) && !isInitialisedArray(key)) ? initialise(key, targets) : key),
tempSym = Symbol('turboprop_singleUseMethod'),
removeTempSymbols = () => targets.forEach(({object}) => {
delete object[tempSym]
delete object[TURBOPROP_METHODS]
}),
get = function () {
const ret = this[TURBOPROP_METHODS].getter(keys, this)
removeTempSymbols()
return ret
},
set = function (values) {
this[TURBOPROP_METHODS].setter && this[TURBOPROP_METHODS].setter(keys, values, this)
removeTempSymbols()
}
targets.forEach(({object, methods}) => {
Object.defineProperty(object, tempSym, { configurable: true, get, set})
object[TURBOPROP_METHODS] = methods
})
return tempSym
}
return array
}
export const isInitialisedArray = array => array[TURBOPROP_MARKER]
export const initialiseGlobally = (state = true, targetOrTargets = DEFAULT_TARGETS) => {
if (state) {
initialise(Array.prototype, targetOrTargets)
} else {
delete Array.prototype[Symbol.toPrimitive]
}
}