CHUTE
The MIT License
Copyright (c) 2022-2024, Greg Abbott
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Script
const chute = (()=>{
//CHUTE: Chain functions AND methods//https://github.com/gregabbott/chute//By + Copyright Greg Abbott 2024-11-27 (V1) + 2024-11-27 (V)let data
let key_is=nullconsterror=(...x)=>{thrownewError(x)}
constis_fn=x=>x instanceofFunctionconstis_js_method=(o,k)=>is_fn(Object.getPrototypeOf(o)[k])
functionsub_chain_reducer (data,f){
if(is_fn(f))returnf(data)
if(f==='log'){console[f](data);return data}
elseif(typeof f==='string'){
if(is_js_method(data,f)) return data[f]()
elseerror(`Data lacks "${f}" method`)
}
else {error('Not a function:',f)}
return data
}
constsub_chain=a=>a.reduce(sub_chain_reducer,data)
constcall_method_of_data=(key,a)=>{
if(is_js_method(data,key)){data=data[key](...a)}
elseerror(`Data lacks "${key}" method`)
}
constif_then=({cond,fn})=>{
const cond_is_fn=is_fn(cond)
const good_cond=cond_is_fn||cond===false||cond===trueif(!good_cond){
error('give .if(arg1) bool OR test=data=>{…return bool}')
}
if(!is_fn(fn)){
error('give .if(,arg2) a Fn to send data to',fn)
}
elseif(cond_is_fn&&cond(data)===true)data=fn(data)
elseif(cond===true)data=fn(data)
}
const proxy = newProxy(
functiontarget(){},
{
get(target,k){
key_is=k
return proxy
},
apply:(target,_, a)=>{
let key = key_is
if(key_is)key_is=nullif(key=='log'){console[key](...a,data);}
elseif(key=='if'){if_then({cond:a[0],fn:a[1]})}
elseif(key=='tap'&&is_fn(a[0]))a[0](data)
elseif(key=='tap')error('give .tap a fn to send data to')
elseif(key===null&&a.length===0)return data
elseif(key===null||key==='and')data=sub_chain(a)
elseif(key)call_method_of_data(key,a)
return proxy
}
})
return(x,...fns)=>{
data=x
if(fns.length>0)data=sub_chain(fns)
return proxy
}
})()
Example use
let initial_data = [1, 2, 4]
const result = chute(initial_data,/*any functions…*/push(8))
//then swap between function and method calls as needed.//call any method native to the current data as normal:
.map(double)
//".log" calls a built in method that takes 0+ arguments.
.log('doubled:')
//".log" logs any arguments and the data, then resumes.
.filter(greater_than(4))
//".and" calls a built in method that takes 1+ functions.//".and(f1,f2)" sends data to f1, then f1's return to f2.
.and(push(32),push(64))
//nameless calls work the same way as ".and":
(push(80,128),push(256))
//each sub-chain uses a reducer and mimics nested calls.//one call "(f1,f2)" == "f2(f1(data))"//many calls "(f1)(f2)" == "data=f1(data);data=f2(data)"
('reverse')//strings call methods that need no arguments.//this allows unbroken sequences: "(f1,'method',f2)"//'.tap' calls a built in method that takes a function.//'.tap(f1)' sends data to f1 but ignores the result.
.log('pre-tap')
.tap(mutate)//.tap functions might mutate data directly.
.log('post-tap')
.reduce(add_up)
//'.if' calls an inbuilt method that takes 2 arguments.
.if(
//a boolean//or a function that tests data and return a boolean.greater_than(64),
//a function to send data to if the condition === true.
halve
)
.toString()
(append('.'))
()//an empty call returns the final value//from this point, method calls directly act on the data.
.replace('.','!')
log({result})
//Demo Helper Functionsfunctionpush (...a){returnl => (l.push(...a),l)}
functionlog (x){console.log('outer_log',x);return x}
functiongreater_than(n){ returnx => x > n}
functiondouble(x){return x * 2}
functionhalve(x){return x / 2}
functionadd_up(a, b){return a + b}
functionappend(x){returnd => d+x}
functionmutate (data){
data.length=0
data.push(1452,1386,1483,1475)
return'chucks this returned value'
}