11const chute = ( ( ) => {
22// https://gregabbott.github.io/chute By + Copyright Greg Abbott
3- // [V1=2024-11-27][V=2024-12-08.1 ]
3+ // [V1=2024-11-27][V=2024-12-08.2 ]
44const stringy = x => JSON . stringify ( x ) ,
55error = ( ...x ) => { throw new Error ( x ) } ,
66is_fn = x => x instanceof Function ,
@@ -17,17 +17,37 @@ lift_libraries=o=>{// hoist
1717 CHUTE . library [ k ] = v
1818 CHUTE . lifted_libraries . push ( v )
1919 } ) ( o )
20+ return CHUTE //allow chaining
2021} ,
2122load_feed_items = o => {
2223 if ( ! is_object ( o ) ) error ( `give .feed 1 object` )
2324 loop_o ( ( k , v ) => { if ( is_fn ( v ) || is_object ( v ) ) CHUTE . library [ k ] = v } )
2425 ( o )
26+ return CHUTE //allow chaining
2527} ,
2628chute_lib = { } , //Holds a chute's methods: log, tap, do, if
27- PLACEHOLDER = { } , //Sets data argument position in .fn calls.
28- CHUTE = ( seed , ...args ) => new_chute ( { seed, args} ) //Becomes chute FN
29- //`chute(seed)` calls CHUTE, returns new chute setup with seed
30- CHUTE . x = PLACEHOLDER //Gives user access via `(chute_fn_name).x`
29+ PLACEHOLDER = { } , //Stands in for data as argument in .fn calls.
30+ CHUTE = ( seed = PLACEHOLDER , ...args ) => {
31+ //if(seed===PLACEHOLDER)return CHUTE//require seed
32+ //Allow blank seed: user may wants a dot FN to return seed
33+ return new_chute ( { seed, args} )
34+ }
35+ //a default chute
36+ //^Becomes "const chute". `chute([seed,args])` makes new chute
37+ CHUTE . make = ( ...o ) => { //makes, configures and returns a new chute
38+ let settings = o . length === 1 && is_object ( o [ 0 ] ) ?o [ 0 ] :0
39+ if ( ! settings ) error ( '.with accepts 1 object for settings' )
40+ return new_chute ( { settings} )
41+ }
42+ const setup_chute = ( { settings, a_chute} ) => { //configure a chute
43+ if ( a_chute . with_received ) error ( `1 "with" per chute` )
44+ a_chute . with_received = true
45+ let o = settings
46+ if ( o . sync ) load_sync_fn ( o . sync , a_chute )
47+ if ( 'skip' in o ) a_chute . skip_void = ! ! o . skip
48+ if ( o . path ) a_chute . treat_dots_as_paths = ! ! o . path
49+ }
50+ CHUTE . x = PLACEHOLDER //User access via `(chute_fn_name).x`
3151CHUTE . library = { } //Holds Fns FEED/LIFT gave to ALL CHUTE
3252CHUTE . lifted_libraries = [ ] //Holds libs LIFT gives to all CHUTE
3353//lifted_libraries can call ".fn_name" and ".lib_name.fn_name"
@@ -38,8 +58,8 @@ chute_lib.log=({args,data})=>{
3858 else console . log ( data )
3959 return data
4060}
41- chute_lib . do = ( { args, data, a_chute} ) => { //[f1,f2,f3 ]->f3(f2(f1 (x)))
42- if ( args . length == 0 ) error ( 'give .do >0 arguments' ) //.log.do.log
61+ chute_lib . do = ( { args, data, a_chute} ) => { //[a,b,c ]->c(b(a (x)))
62+ if ( args . length == 0 ) error ( 'give .do >0 arguments' )
4363 return args . reduce ( ( data , arg ) => {
4464 if ( arg === 'log' ) { console . log ( data ) ; return data }
4565 else if ( is_fn ( arg ) ) { a_chute . keep ( arg ) ; return arg ( data ) }
@@ -134,18 +154,7 @@ chute_lib.tap=({args,data})=>{
134154 else error ( 'give .tap a fn to send data to' )
135155 return data
136156}
137- chute_lib . with = ( { args, data, a_chute} ) => { //configure a chute
138- if ( a_chute . with_received ) error ( `1 "with" per chute` )
139- a_chute . with_received = true
140- let o = args . length === 1 && is_object ( args [ 0 ] ) ?args [ 0 ] :0
141- if ( ! o ) error ( '.with accepts 1 object for settings' )
142- if ( o . sync ) load_sync_fn ( o . sync , a_chute )
143- if ( 'skip' in o ) a_chute . skip_void = ! ! o . skip
144- if ( o . feed ) load_feed_items ( o . feed )
145- if ( o . lift ) lift_libraries ( o . lift )
146- if ( o . path ) a_chute . treat_dots_as_paths = ! ! o . path
147- return data //Leaves data unchanged
148- }
157+
149158function load_sync_fn ( o , a_chute ) {
150159 if ( ! is_fn ( o ) ) error ( `give SYNC FN: "v=>user_variable=v"` )
151160 a_chute . sync_data = o
@@ -264,7 +273,7 @@ function _apply(a_chute, args){
264273 a_chute . dot_list . length = 0 //Reset : processed all call keys
265274 return should_end_chute ?a_chute . get_data ( ) :a_chute . proxy
266275}
267- function new_chute ( { seed, args} ) {
276+ function new_chute ( { seed, args, settings } ) {
268277 let a_chute = blank_chute ( )
269278 a_chute . keep = make_memoizer ( a_chute )
270279 a_chute . set_data = x => {
@@ -273,8 +282,11 @@ function new_chute({seed,args}){
273282 a_chute . data = x
274283 }
275284 a_chute . get_data = ( ) => a_chute . data
285+ if ( settings ) { //called chute.make({settings_object})
286+ setup_chute ( { settings, a_chute} )
287+ }
276288 if ( seed ) a_chute . set_data ( seed )
277- if ( args . length > 0 ) {
289+ if ( args ? .length > 0 ) {
278290 a_chute . set_data ( chute_lib . do ( { args, data :seed , a_chute} ) )
279291 }
280292 a_chute . proxy = new Proxy ( ( ) => { } , {
0 commit comments