CHUTE - Chain Functions AND Native Methods
 Language: Vanilla JavaScript
Version 1: 2024-11-27
  Version: 2024-11-28
      Log: Adds calling global functions in a method call style.
 Download: Normal (2KB)
           Minified (1KB)
   Notice: By and © Greg Abbott 2024
    Links: GitHub
  License: 
CHUTE

The MIT License

Copyright (c) 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-28 (V)
  //Changes: Adds calling global functions via method style
  let data
  let key_is=null
  const error=(...x)=>{throw new Error(x)}
  const is_fn=x=>x instanceof Function
  const is_js_method=(o,k)=>is_fn(Object.getPrototypeOf(o)[k])
  function sub_chain_reducer (data,f){
    if(is_fn(f))return f(data)
    if(f==='log'){console[f](data);return data}
    else if(typeof f==='string'){
      if(is_js_method(data,f)) return data[f]()
      else error(`Data lacks "${f}" method`)
    }
    else {error('Not a function:',f)}
    return data
  }
  const sub_chain=a=>a.reduce(sub_chain_reducer,data)
  function is_global_fn(name){return is_fn(globalThis[name])}
  const call_method_of_data=(key,a)=>{
    if(is_js_method(data,key)){data=data[key](...a)}
    else if(is_global_fn(key)){
      if(a.length==0)data= globalThis[key](data)
      if(a.length>0)data= globalThis[key](...a)(data)
    }
    else error(`Data lacks "${key}" method|Not a global Fn`)
  }
  const if_then=({cond,fn})=>{
    const cond_is_fn=is_fn(cond)
    const good_cond=cond_is_fn||cond===false||cond===true
    if(!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)
    }
    else if(cond_is_fn&&cond(data)===true)data=fn(data)
    else if(cond===true)data=fn(data)
  }
  const proxy = new Proxy(
    function target(){}, 
    {
    get(target,k){
      key_is=k
      return proxy
    },
    apply:(target,_, a)=>{
      let key = key_is
      if(key_is)key_is=null
      if(key=='log'){console[key](...a,data);}
      else if(key=='if'){if_then({cond:a[0],fn:a[1]})}
      else if(key=='tap'&&is_fn(a[0]))a[0](data)
      else if(key=='tap')error('give .tap a fn to send data to')
      else if(key===null&&a.length===0)return data
      else if(key===null||key==='and')data=sub_chain(a)
      else if(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))
  //Chute can alternate function and method calls.
  //==== METHOD CALLS
  // Method calls native to the current data work as normal:
  .map(double)
  .filter(greater_than(4))
  //==== FUNCTION CALLS
  //Chute provides a few ways to call functions.
  //== SUB CHAINS:
  //".and" sends data through 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))
  //these sub-chains use a reducer to mimic nested calls.
    //"(f1,f2)" == "f2(f1(x))"
  //strings can call methods that need no arguments:
  ('reverse')//for sequences like: ".and(f1,'method',f2)".
  //== METHOD STYLE GLOBAL FUNCTION CALLS:
  //".name_of_a_global_function" calls a global function:
  .wrap_in_array()//no arguments == "fn(data)"
  .get_index(0)//arguments == "fn(arguments)(data)"
  //Under consideration: A placeholder system.
    //e.g. ".fn(arg_1, placeholder_for_data_here, arg_3)".
  //Chute can call global functions with unoccupied names.
    //Chute looks for methods before global functions.
    //It checks the current data's native methods.
    //It also check's Chute's built in methods:
  //BUILT IN METHODS
  //Chute also has "log", "tap", and "if" methods.
  //".log" calls a built in method that takes 0+ arguments.
  .log()//".log()" logs the current data, then resumes.
  .log('Pre-tap data')// ".log" logs arguments before data.
  //'.tap' calls a built in method that takes one function.
  //'.tap(f1)' sends the data to f1 but ignores the result.
  .tap(mutate)//.tap functions might mutate data directly.
  .log('Post-tap data')
  //'.if' calls a built in method that takes 2 arguments.
  .if(//Argument 1
    //a boolean (some unrelated check e.g. "is_saturday()")
    //or a function that tests the data and return a boolean.
    is_array,
    //Argument 2:A function to send the data through
      //(If the condition equals the boolean true.)
    sum
  )
  .log("after if method:")
  ()//an empty call returns the final value
  //from this point, method calls directly act on the data.
  .toString()
  .replace(/$/,'!')
log({result})

//Demo Helper Functions

function get_index(i){return data => data[i]}
function wrap_in_array(data){return [data]}
function push (...a){return l => (l.push(...a),l)}
function log (x){console.log('outer_log',x);return x}
function greater_than(n){ return x => x > n}
function double(x){return x * 2}
function sum(data){return data.reduce((a,b)=>a+b)}
function is_array(data){return Array.isArray(data)}
function mutate (data){
  data.length=2
  return 'chucks this returned value'
}