mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-01-06 06:54:12 +01:00
Use Proxy for array()
This commit is contained in:
@@ -1646,6 +1646,19 @@ const Iter = Class("Iter", {
|
|||||||
});
|
});
|
||||||
iter.Iter = Iter;
|
iter.Iter = Iter;
|
||||||
|
|
||||||
|
function arrayWrap(fn) {
|
||||||
|
function wrapper() {
|
||||||
|
let res = fn.apply(this, arguments);
|
||||||
|
if (isArray(res))
|
||||||
|
return array(res);
|
||||||
|
if (isinstance(res, ["Iterator", "Generator"]))
|
||||||
|
return iter(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
wrapper.wrapped = fn;
|
||||||
|
return wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Array utility methods.
|
* Array utility methods.
|
||||||
*/
|
*/
|
||||||
@@ -1656,23 +1669,25 @@ var array = Class("array", Array, {
|
|||||||
else if (ary.length)
|
else if (ary.length)
|
||||||
ary = Array.slice(ary);
|
ary = Array.slice(ary);
|
||||||
|
|
||||||
return {
|
let self = this;
|
||||||
__proto__: ary,
|
return new Proxy(ary, {
|
||||||
__iterator__: function () this.iterItems(),
|
get: function array_get(target, prop) {
|
||||||
__noSuchMethod__: function (meth, args) {
|
if (prop in array && callable(array[prop]))
|
||||||
var res = array[meth].apply(null, [this.array].concat(args));
|
return arrayWrap(array[prop].bind(array, target));
|
||||||
if (isArray(res))
|
|
||||||
return array(res);
|
if (prop == "array")
|
||||||
if (isinstance(res, ["Iterator", "Generator"]))
|
return target;
|
||||||
return iter(res);
|
|
||||||
return res;
|
let p = target[prop];
|
||||||
},
|
if (!/^\d+$/.test(prop) &&
|
||||||
array: ary,
|
prop != "toString" &&
|
||||||
toString: function () this.array.toString(),
|
prop != "toSource" &&
|
||||||
concat: function (...args) this.__noSuchMethod__("concat", args),
|
callable(p))
|
||||||
filter: function (...args) this.__noSuchMethod__("filter", args),
|
return arrayWrap(p);
|
||||||
map: function (...args) this.__noSuchMethod__("map", args)
|
|
||||||
};
|
return p;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user