-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlik_2023_search.js
376 lines (344 loc) · 14.5 KB
/
Blik_2023_search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import {note,something,record,provide,has,compose,buffer,slip,drop,stream,infer,either,swap,not,whether,pass,collect,simple,defined,string,compound,tether,is,numeric,array,basic,iterable} from "./Blik_2023_inference.js";
import {resolve} from "./Blik_2023_interface.js";
export var stringify=scope=>
scope&&iterable(Object(scope))?String(scope):JSON.stringify(scope);
export function random(length,domain="abcdefghijklmnopqrstuvwxyz123456789_")
{return Array(length).fill(domain).map(domain=>
domain.charAt(Math.floor(Math.random()*domain.length))).join("");
};
export function encrypt(hash,algorithm="sha256",value)
{if(!this)
return value=import("crypto").then(crypto=>value=crypto),function()
{return encrypt.call(value,hash,algorithm,...arguments);
};
return compose.call(this,hash,algorithm?infer("createHmac",algorithm):"createHash"
,value,"utf-8","update","hex","digest");
};
export function edit(source,edits)
{return Object.entries(edits||{}).reduce((source, [field,value]) =>
source.replace(new RegExp(field,"g"),(match, ...groups) =>
[value, ...groups.slice(0,-2)].reduce((value, group, index) =>
value.replaceAll("$"+index, group)))
,source);
};
export function sum(...context)
{// cumulate context values.
return context.flat().map(term=>Number(term)||0).reduce((sum,value)=>sum+value,0);
};
export function extreme(series)
{return ["min","max"].map(key=>Math[key](...[series].flat()));
};
export function calendar(timestamps)
{let year=
{January:31,February:(year)=>28+Number(!(year%4)),March:31,April:30,May:31
,June:30,July:31,August:31,September:30,October:31,November:30,December:31
};
if(!timestamps)return year;
return timestamps.reduce((calendar,time)=>
(['FullYear','Month','Date'].reduce((calendar,scale)=>
[calendar,new Date(time)['get'+scale]()].reduce((calendar,field)=>
(calendar[field]=[calendar[field],0].reduce((value)=>(!isNaN(value)?value+1:value))||
Object.values(year).map((days)=>Object.fromEntries(Array(days.call?.(null,field)||days).fill(0).map((value,index)=>
[index+1,value])))))
,calendar)
,calendar)
,{});
};
export function date(time)
{const date=new Date(time);
const [year,month,day]=['FullYear','Month','Date'].map(frame=>date['get'+frame]());
const [mm,dd]=[month+1,day].map(frame=>
Object.assign('00'.split(''),String(frame).split('').reverse()).reverse().join(''));
return [mm,dd,String(year)].join('/');
}
export function time(date)
{const [mm,dd,year]=date.split(/[/ ]/);
return new Date([mm,dd,year].join('/')).getTime();
}
export function round(date,up=0)
{// set date to a turn of the month.
if(!(date instanceof Date))
date=new Date(date);
return new Date(['FullYear','Month'].map((scale,month)=>
date['get'+scale]()+month+(up&&month)).reduce((year,month)=>
[year+(month>12),month%12||12].join('/')+'/1 0:0')).getTime();
};
export function normalize(samples, resolution, range)
{const grid = range.reduce((min, max) => max - min) / resolution;
return samples.map(sample=>
[[sample.lon,sample.lat].map(axis=>Math[axis<0?'ceil':'floor'](axis/grid)*grid).join(':')
,sample
]).reduce((clusters,[field,sample])=>
Object.assign(clusters,{[field]:[clusters[field]||[],sample].flat()})
,{});
}
export function kmeans(samples,bounds,size,projection)
{const clusters=bounds.reduce(([left,top],[right,bottom])=>
Array(Math.floor((right-left)/size)).fill(left).flatMap((left,x)=>
Array(Math.floor((bottom-top)/size)).fill(top).map((top,y)=>(
{center:projection.invert([left+(x+0.5)*size,top+(y+0.5)*size])
,nodes:[]
}))));
samples=samples.filter((sample)=>
{const {lat:y,lon:x}=sample;
const distances=clusters.map(({center:[cx,cy]})=>
Math.pow(cx-x,2)+Math.pow(cy-y,2));
const closest=Math.min(Infinity,...distances);
const cluster=clusters.at(distances.indexOf(closest));
return !cluster?.nodes.push(sample);
});
return clusters.filter(({nodes})=>nodes.length).flatMap((cluster)=>
cluster.nodes.length>1?cluster:cluster.nodes).concat(samples);
};
export function nearest(range,value)
{const distances = range.map((range) => Math.abs(range - value));
const index = distances.indexOf(Math.min(...distances));
return range[index];
};
export const antipode = (point, scale) => point / (scale / 360) - 180;
export const bisection = (point, scale) => (point / (scale / 2) - 1) * -1;
export const gudermannian = (normal) =>((2 * Math.atan(Math.exp(normal * Math.PI)) - Math.PI / 2) * 180) / Math.PI;
export function quadrate(center,width,ratio)
{return [-1,1].map(unit=>[0,1].map(axis=>
center[axis]+(width/(axis?ratio:1)/2)*unit));
};
export function search(term,recursive=false,path=[])
{// traverse scope for entries satisfying a term (condition or singular path).
// recursive search includes ranges in recursion domain.
if(!defined(this))
return tether(search,...arguments);
let scope=this;
if(typeof scope!=='object'||scope===null)
return [];
let condition=term instanceof Function;
if(!condition)
return [term].flat().reduce((scope,field)=>scope?.[field],scope);
let [domain,range]=Object.entries(this).reduce(function sort(group, entry)
{group[term(entry,path)?1:0].push(entry);
return group;
},[[],[]]);
if(recursive)
domain=[domain,range].flat();
let subrange=domain.flatMap(([field,value])=>
Object.entries(search.call(value,term,recursive,path.concat(field))).map(([path,value])=>
[[field,path].join('/'),value
]));
return Object.fromEntries([range,subrange].flat());
};
export function route(scope,term,path)
{// insert/invoke term in scope on given path.
if(!scope)return;
let paths=[path(scope,term)].flat().filter(path=>(path??false)!==false);
if(!paths.length)return;
let descended=paths.find(field=>route(scope[field],term,path));
if(descended)return descended;
let entries=paths.map(path=>[path,term instanceof Function?term([path,scope[path]]):term]);
return entries.reduce(record(([path,term])=>infer(term,term=>
Array.isArray(scope)?scope.splice(path,0,term):Object.assign(scope,{[path]:term})))
,[]);
};
export function prune(term,collapse,limit=[],path=[])
{// map entries recursively.
let scope=this;
if(!compound(scope))return scope;
let entries=Object.entries(scope);
if(!entries.length)
for(let field in scope)
entries.push([field,scope[field]]);
entries=entries.flatMap(function([field,source],index,entries)
{let terminal=numeric(limit)?path.length===limit:[limit].flat().some(limit=>
[[limit],[array(limit)?path:[],field]
].map(compose("flat","/","join")).reduce(Object.is))
let value=term.call(scope,[field,source],path);
let pluck=!defined(value);
if(pluck&&(!collapse||terminal))
return [];
if(terminal)
return [[field,value]];
let graft=pluck&&collapse;
let range=[...collect(graft?source:value)].map(scope=>
prune.call(scope,term,collapse,limit,path.concat(field)));
return range.flatMap(scope=>graft
?Object.entries(compound(scope)?scope:{})
:[[field,scope]]);
});
let iterable=!entries.length||!entries.some(([field],index,entries)=>
isNaN(field)||[entries[index-1]?.[0],field].map(Number).reduce((past,next)=>next<past));
if(iterable)
entries.forEach(function([field],index,entries)
{if(!index)
// snap first index.
return field!=0&&entries.forEach(entry=>entry[0]=Number(entry[0])-entries[0][0]);
let leap=1-field+Number(entries[index-1]?.[0]);
if(leap)
// spread plural indexes.
entries.slice(index).forEach((entry)=>entry[0]=Number(entry[0])+leap);
});
scope=Object.fromEntries(entries);
return iterable?Object.assign(Array(0),scope):scope;
// composition for async terms, makes some optimization overdue.
// return compose.call(entries,infer("reduce",record(function([field,source],index,entries)
// {let terminal=numeric(limit)?path.length===limit:[limit].flat().some(limit=>
// [[limit],[array(limit)?path:[],field]
// ].map(compose("flat","/","join")).reduce(Object.is));
// let dispensible=!collapse||terminal;
// return compose(tether(term),either
// (whether(dispensible&¬(defined),swap([]))
// ,whether(terminal,value=>[[field,value]])
// ,either(whether(collapse&¬(defined),swap(true,source)),slip(false))
// ),collect,([graft,...scope])=>[graft,scope.map(scope=>
// prune.call(scope,term,collapse,limit,path.concat(field)))]
// ,"flat",provide,collect,([graft,...range])=>range.flatMap(scope=>graft
// ?Object.entries(compound(scope)?scope:{})
// :[[field,scope]]))(scope,[field,source],path);
// }),[]),"flat",index);
};
var index=whether
(entries=>!entries.length||!entries.some(([field],index,entries)=>
isNaN(field)||[entries[index-1]?.[0],field].map(Number).reduce((past,next)=>next<past))
,compose(pass(infer("forEach",function([field],index,entries)
{if(!index)
// snap first index.
return field!=0&&entries.forEach(entry=>entry[0]=Number(entry[0])-entries[0][0]);
let leap=1-field+Number(entries[index-1]?.[0]);
if(leap)
// spread plural indexes.
entries.slice(index).forEach((entry)=>entry[0]=Number(entry[0])+leap);
})),Object.fromEntries,slip(Array(0)),Object.assign)
,Object.fromEntries
);
export function merge(target,source,override=1)
{// unite scopes (assign if path specified to override).
let path=[override].flat();
if(!path.length)
return source;
let assign=path.some(string);
if(assign)
return [target,...path,source].reduce((scope,field,index,route)=>route.length-index-1
?(scope[field]=route.length-index>2?scope[field]||{}:route[index+1])
:route[0]);
let Group=[Set,Map].find(group=>target instanceof group);
if(Group)
return override?source:new Group([source,target].flatMap(part=>Array.from(part)));
let extensible=[target,source].every(array);
if(extensible&&!override)
return target.concat(source);
// to merge array domains, pass the source as plain object.
let codomain=array(target)?!simple(source):array(source);
let opaque=codomain||[target,source].some(term=>!compound(term));
if(opaque)
return [target,source][Number(Boolean(override))];
return Object.entries(source).reduce(function(target,[field,next])
{const past=target[field];
const value=defined(past)?merge(past,next,override):next;
// mutation warning - reduce on an empty target to copy.
if(something(value))
return buffer(Object.assign,drop(1,2))(target,{[field]:value});
delete target[field];
return array(target)?[target].flat():target;
},target);
};
function set(options,namespace)
{return Object.entries(namespace).reduce((defaults,[field,kinds])=>
Object.assign(defaults
,{[field]:options[field]
?Object.entries(kinds).find(([kind])=>
[kind,options[field]].map((kind)=>kind.toLowerCase()).reduce(Object.is))?.pop()||
Error([String(options[field]),'not in "'+field+'" options',Object.keys(kinds),].join(" "))
:defaults[field],
}),this);
};
export function trace(term, path = []) {
// trace term in scope or stack.
let scope = this;
if (!something(term))
return stream(
[Infinity, Error.stackTraceLimit].reduce(
({ stack }, stackTraceLimit) =>
(Object.assign(Error, { stackTraceLimit }) && stack) || Error(),
{}
),
stream(
{
functor: /at (?:async )*([^ ]*)/,
file: /(?:.*[\(_|\/])*(.*?)/,
location: /(?:.js)*(?::[0-9]+){0,2}/,
},
Object.values,
(expressions) => expressions.map(({ source }) => source).join(""),
RegExp
),
(stack, details) =>
stack
.split(/\n */)
.slice(1)
.map((stack) => stack.match(details) || [stack]),
(details) => details.map((details) => details.slice(1, 3).concat(details.input).reverse()),
(details) => details.slice(details.findIndex(({ 2: term }) => term === trace.name))
);
if (scope === term || !scope) return path;
return Object.entries(scope).reduce(
(hit, [track, scope]) =>
hit ||
[term === scope, path.concat(track)].reduce((hit, path) =>
hit ? path : typeof scope == "object" ? trace.call(scope, term, path) : undefined
),
undefined
);
};
export function parse(records,separator="\",\"")
{if(records instanceof ArrayBuffer||records.constructor?.name==="Buffer")
records=new TextDecoder('utf-8').decode(records);
return records.split("\n").map(record=>
record.split(separator)).reduce((fields,record,index,records)=>
records.splice(index).map(record=>
Object.fromEntries(fields.map((field,index)=>[field,record[index]]))));
};
export function cluster(records,{field="Year"}={})
{return records.reduce((clusters,record)=>record[field]
?Object.assign(clusters,{[record[field]]:sum(clusters[record[field]],1)})
:clusters
,{});
};
export function unfold(field,ancestors=new Set())
{if(!this||ancestors.has(this))
return [];
ancestors.add(this);
let fold=[field].flat().flatMap(field=>field instanceof Function?field(this):this?.[field]||[]);
return [this,fold.flatMap(scope=>unfold.call(scope,field,ancestors))].flat();
};
export function clone(scope)
{return merge(JSON.parse(JSON.stringify(scope)),scope);
};
export function isolate(path)
{// prune scope to specified path.
return record(search.call(this,path),path);
};
export function extract(fields,source)
{if(!this)return source?extract.call(source,fields):tether(extract,fields);
return [fields].flat().reduce((term,field)=>merge(term,{[field]:this[field]}),{});
};
export function relevant(scope,term)
{return Object.fromEntries(Object.entries(scope).flatMap(([field,value])=>!string(value)
?["ends","starts"].some(side=>term[side+"With"](field))?Object.entries(value):[]
:[[field,value]]));
};
export const tests=
{merge:
[{context:[{a:1},{b:2}],terms:[{a:1,b:2}],condition:["deepEqual"]}
,{context:[{a:{}},2,"a/b/c".split("/")],terms:[{a:{b:{c:2}}}],condition:["deepEqual"]}
,{context:[undefined,{b:2}],terms:[{b:2}],condition:["deepEqual"]}
,{context:[undefined,{b:2},true],terms:[{b:2}],condition:["deepEqual"]}
,{context:[new URL("http://localhost"),{b:2},true],terms:["b",2],condition:"equal"}
],search:
[{scope:{a:{b:2}},context:[({1:value})=>value===2],terms:[{"a/b":2}],condition:"deepEqual"}
,{scope:{a:{b:1}},context:["abc".split("")],terms:[term=>term===undefined],condition:"ok"}
,{scope:{a:{b:1}},context:[["a","b"]],terms:[1],condition:["equal"]}
,{scope:{a:{b:1}},context:[entry=>entry,true],terms:[{a:{b:1},"a/b":1}],condition:["deepEqual"]}
,{scope:{a:{b:1}},context:[([field,value])=>!isNaN(value)],terms:[{"a/b":1}],condition:["deepEqual"]}
],prune:
{trim:{scope:{a:{b:{c:3}}},context:[([field,value])=>field!=='b'?value:undefined],terms:[{a:[]}],condition:"deepEqual"}
,collapse:{scope:{a:{b:{b:2,c:3}}},context:[([field,value])=>field!=='b'?value:undefined,true],terms:[{a:{c:3}}],condition:"deepEqual"}
,shave:{scope:{a:{b:{c:{d:1},f:2}},e:3},context:[([field,value],path)=>path.length<2?value:undefined],terms:[{a:{b:[]},e:3}],condition:"deepEqual"}
//,agnostic:{scope:{a:{b:[]},e:3},context:[([field,value])=>Promise.resolve(value)],terms:[note,{a:{b:[]},e:3}],condition:"deepEqual"}
}};