@javish/lib 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changes.md +6 -0
- package/index.js +73 -6
- package/package.json +2 -2
package/changes.md
ADDED
package/index.js
CHANGED
|
@@ -17,13 +17,80 @@
|
|
|
17
17
|
* @param {Function} Base
|
|
18
18
|
* @param {Record<string,Function>} methods
|
|
19
19
|
*/
|
|
20
|
-
var ImplFactory=function(Base,methods){
|
|
21
|
-
return function(){
|
|
22
|
-
var obj=new Base
|
|
23
|
-
var proto=Object.getPrototypeOf(obj)
|
|
24
|
-
for(var k in methods) proto[k]=methods[k]
|
|
20
|
+
var ImplFactory = function (Base, methods) {
|
|
21
|
+
return function () {
|
|
22
|
+
var obj = new Base
|
|
23
|
+
var proto = Object.getPrototypeOf(obj)
|
|
24
|
+
for (var k in methods) proto[k] = methods[k] // those can be passed to the proxy when creating
|
|
25
25
|
return obj
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
// var i = 0
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
*
|
|
33
|
+
* @param {new()=>B} Base
|
|
34
|
+
* @param {C} Impl
|
|
35
|
+
* @returns {new()=>B&Impl}
|
|
36
|
+
* @template {Record<string,*>} B
|
|
37
|
+
* @template {Record<string,*>} C
|
|
38
|
+
*/
|
|
39
|
+
module.exports.FnImplFactory = (Base, Impl) => {
|
|
40
|
+
var target = function (__fm, I, fn, thisArg, args) {
|
|
41
|
+
// var __fm = base.__fm.call(newTarget) // invoke
|
|
42
|
+
// console.log('> 💡 functional #' + I) // if calling .test on this, might want to access base???
|
|
43
|
+
return __fm.apply(this, args)
|
|
44
|
+
}
|
|
45
|
+
var $fields
|
|
46
|
+
var handler = {
|
|
47
|
+
apply:target,
|
|
48
|
+
construct(fn, args, Target) {
|
|
49
|
+
// var I=++i
|
|
50
|
+
var __fm=Base.prototype.__fm.call(Target.prototype)
|
|
51
|
+
if(!__fm) {
|
|
52
|
+
__fm=Base.prototype.__fm.call(Impl)
|
|
53
|
+
}
|
|
54
|
+
var base=new Base
|
|
55
|
+
// var target=new (Target.prototype.constructor)
|
|
56
|
+
var descs={}
|
|
57
|
+
if(!$fields) {
|
|
58
|
+
$fields = new Set
|
|
59
|
+
var _$fields=Object.entries(Object.getOwnPropertyDescriptors(base))
|
|
60
|
+
for(var [k,v] of _$fields) {
|
|
61
|
+
if(['length','name','arguments','caller','prototype'].includes(k)) continue
|
|
62
|
+
$fields.add(k)
|
|
63
|
+
descs[k]=v
|
|
64
|
+
// descs[k]=Object.getOwnPropertyDescriptor(target,k)
|
|
65
|
+
}
|
|
66
|
+
}else {
|
|
67
|
+
for(var k of $fields) {
|
|
68
|
+
descs[k]=Object.getOwnPropertyDescriptor(base,k)
|
|
69
|
+
// descs[k]=Object.getOwnPropertyDescriptor(target,k)
|
|
70
|
+
} // what about private fields though
|
|
71
|
+
}
|
|
72
|
+
var STATE=Object.create(Target.prototype,descs)
|
|
73
|
+
|
|
74
|
+
// if(typeof Target.prototype.constructor=='function') {
|
|
75
|
+
// new Target.prototype.constructor.call(STATE)
|
|
76
|
+
// }
|
|
77
|
+
if(typeof Target.prototype.const=='function') {
|
|
78
|
+
Target.prototype.const.call(STATE)
|
|
79
|
+
}
|
|
80
|
+
// else if(typeof Target.prototype.constr=='function') {
|
|
81
|
+
// Target.prototype.constr.call(STATE)
|
|
82
|
+
// }
|
|
83
|
+
// plus base
|
|
84
|
+
var I = I || void 0
|
|
85
|
+
return fn.bind(STATE,__fm, I)
|
|
86
|
+
},
|
|
87
|
+
get(fn, prop) {
|
|
88
|
+
return Base[prop] // requests prototype?
|
|
89
|
+
},
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
console.log('created new functional', Base)
|
|
93
|
+
return new Proxy(target, handler)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports.ImplFactory = ImplFactory
|