Object.defineProperty(Array.prototype, 'toStreamUsing', {
configurable: true,
value: function (methods) {
let array = this, funcs = [x => x], args = []
function P(...arg) {
return new Proxy({}, {
get: (r, key) => {
funcs.push(methods[key])
args.push(arg)
return key=='toArray' ? A : P
}
})
}
function A() {
return array.map(
v => args.reduce(
(v, arg, i) => funcs[i].apply(null ,[v, ...arg]), v
)
)
}
return P()
}
})
//////////////////////////////////////////////////////////
arr = [1,2,3,4,5]
methods = {
allPlus : (n, m) => n + m ,
allMinus: (n, m) => n - m
}
//////////////////////////////////////////////////////////
arr.toStreamUsing(methods).allPlus(5).allMinus(3).toArray()
// 3,4,5,6,7
stream = arr.toStreamUsing(methods)
stream = stream.allPlus(5)
stream = stream.allMinus(3)
stream.toArray()
// 3,4,5,6,7
T2JqZWN0LmRlZmluZVByb3BlcnR5KEFycmF5LnByb3RvdHlwZSwgJ3RvU3RyZWFtVXNpbmcnLCB7CiAgICBjb25maWd1cmFibGU6IHRydWUsCiAgICB2YWx1ZTogZnVuY3Rpb24gKG1ldGhvZHMpIHsKCiAgICAgICAgbGV0IGFycmF5ID0gdGhpcywgZnVuY3MgPSBbeCA9PiB4XSwgYXJncyA9IFtdCgogICAgICAgIGZ1bmN0aW9uIFAoLi4uYXJnKSB7CiAgICAgICAgICAgIHJldHVybiBuZXcgUHJveHkoe30sIHsKICAgICAgICAgICAgICAgIGdldDogKHIsIGtleSkgPT4gewogICAgICAgICAgICAgICAgICAgIGZ1bmNzLnB1c2gobWV0aG9kc1trZXldKQogICAgICAgICAgICAgICAgICAgIGFyZ3MucHVzaChhcmcpCiAgICAgICAgICAgICAgICAgICAgcmV0dXJuIGtleT09J3RvQXJyYXknID8gQSA6IFAKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfSkKICAgICAgICB9CiAgICAKICAgICAgICBmdW5jdGlvbiBBKCkgewogICAgICAgICAgICByZXR1cm4gYXJyYXkubWFwKAogICAgICAgICAgICAgICAgdiA9PiBhcmdzLnJlZHVjZSgKICAgICAgICAgICAgICAgICAgICAodiwgYXJnLCBpKSA9PiAgZnVuY3NbaV0uYXBwbHkobnVsbCAsW3YsIC4uLmFyZ10pLCB2CiAgICAgICAgICAgICAgICApCiAgICAgICAgICAgICkKICAgICAgICB9CiAgICAKICAgICAgICByZXR1cm4gUCgpCiAgICAgICAgCiAgICB9Cn0pCgoKLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLwoKCmFyciA9IFsxLDIsMyw0LDVdCgptZXRob2RzID0gewogICAgYWxsUGx1cyA6IChuLCBtKSA9PiBuICsgbSAsCiAgICBhbGxNaW51czogKG4sIG0pID0+IG4gLSBtCn0KCgovLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vCgoKYXJyLnRvU3RyZWFtVXNpbmcobWV0aG9kcykuYWxsUGx1cyg1KS5hbGxNaW51cygzKS50b0FycmF5KCkKLy8gMyw0LDUsNiw3CgoKc3RyZWFtID0gYXJyLnRvU3RyZWFtVXNpbmcobWV0aG9kcykKc3RyZWFtID0gc3RyZWFtLmFsbFBsdXMoNSkKc3RyZWFtID0gc3RyZWFtLmFsbE1pbnVzKDMpCnN0cmVhbS50b0FycmF5KCkKLy8gMyw0LDUsNiw3