Docs: Update benchmark results

This commit is contained in:
github-actions[bot]
2025-11-07 21:32:49 +00:00
parent f9da7d4ed7
commit d0bc3b95dd
54 changed files with 1115 additions and 372 deletions

View File

@@ -1,44 +1,62 @@
{ {
"google/gemini-2.5-pro": { "openrouter/polaris-alpha": {
"1_dijkstra": 37.563614472, "1_dijkstra": 5.063492935,
"2_convex_hull": 30.02974868399999, "2_convex_hull": 7.998615312,
"3_lis": 42.240308332, "3_lis": 2.2021910790000003,
"4_determinant": 13.347867265000009 "4_determinant": 2.4727166589999996,
"5_markdown_parser": 2.1889706240000013,
"6_csv_processor": 6.424773092,
"7_scheduler": 19.749020437,
"8_json_validator": 3.7860971980000033
}, },
"google/gemini-2.5-pro TEMP:0.7": { "google/gemini-2.5-pro TEMP:0.7": {
"1_dijkstra": 57.442001282000014, "1_dijkstra": 30.986608248000003,
"2_convex_hull": 94.02301281699998, "2_convex_hull": 58.090286611,
"3_lis": 11.979348513000005, "3_lis": 39.50342178599999,
"4_determinant": 12.74124894999998 "4_determinant": 21.314621672999987,
}, "5_markdown_parser": 21.140068067999994,
"google/gemini-2.5-pro TEMP:0.4": { "6_csv_processor": 46.383014058,
"1_dijkstra": 47.84090968399995, "7_scheduler": 74.787878823,
"2_convex_hull": 42.86752973399998, "8_json_validator": 39.59319957399997
"3_lis": 27.62078084000002,
"4_determinant": 8.960109082999988
}, },
"anthropic/claude-sonnet-4.5": { "anthropic/claude-sonnet-4.5": {
"1_dijkstra": 13.460692856000039, "1_dijkstra": 4.355259537999983,
"2_convex_hull": 5.613852328000008, "2_convex_hull": 4.980808106000011,
"3_lis": 2.8292644480000018, "3_lis": 2.2764143039999762,
"4_determinant": 3.0375356369999937 "4_determinant": 1.8504535560000221,
"5_markdown_parser": 1.841865622000012,
"6_csv_processor": 4.9831007339999775,
"7_scheduler": 12.904369474000006,
"8_json_validator": 2.7832529310000247
}, },
"anthropic/claude-sonnet-4.5 TEMP:0.7": { "anthropic/claude-sonnet-4.5 TEMP:0.7": {
"1_dijkstra": 5.0235283530000014, "1_dijkstra": 3.933310278000019,
"2_convex_hull": 5.469308726000017, "2_convex_hull": 4.819035326000012,
"3_lis": 2.9667647899999867, "3_lis": 2.34827887899999,
"4_determinant": 6.369721143000002 "4_determinant": 1.644420714000007,
"5_markdown_parser": 2.280882503999979,
"6_csv_processor": 4.7151394149999835,
"7_scheduler": 13.658607328000013,
"8_json_validator": 3.4110046910000382
}, },
"anthropic/claude-sonnet-4.5 TEMP:0.4": { "anthropic/claude-sonnet-4.5 TEMP:0.4": {
"1_dijkstra": null, "1_dijkstra": 3.954044443999999,
"2_convex_hull": 6.095885308999976, "2_convex_hull": 4.5914942489999815,
"3_lis": 3.749924349999987, "3_lis": 2.4153946509999806,
"4_determinant": 12.665039036000032 "4_determinant": 1.7196861260000151,
"5_markdown_parser": 1.801734215000004,
"6_csv_processor": 4.646366793999972,
"7_scheduler": 12.781063763999962,
"8_json_validator": 3.3014615179999964
}, },
"openai/gpt-5-codex": { "openai/gpt-5-codex": {
"1_dijkstra": 97.35094948799996, "1_dijkstra": 36.88321140999993,
"2_convex_hull": 145.25300972899993, "2_convex_hull": 26.59820080500003,
"3_lis": 53.988241607000006, "3_lis": 12.8049466599999,
"4_determinant": 6.342495287999976 "4_determinant": 10.523928330000023,
"5_markdown_parser": 57.74550891099998,
"6_csv_processor": 105.18542238100001,
"7_scheduler": 127.650813921,
"8_json_validator": 24.58168081499997
} }
} }

View File

@@ -0,0 +1,28 @@
async function findShortestPath(graph, start, end) {
const { default: PriorityQueue } = await import('https://cdn.skypack.dev/js-priority-queue');
const distances = {};
const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist });
for (const node in graph) distances[node] = Infinity;
distances[start] = 0;
pq.queue({ node: start, dist: 0 });
while (pq.length) {
const { node, dist } = pq.dequeue();
if (node === end) return dist;
if (dist > distances[node]) continue;
for (const neighbor in graph[node]) {
const newDist = dist + graph[node][neighbor];
if (newDist < distances[neighbor]) {
distances[neighbor] = newDist;
pq.queue({ node: neighbor, dist: newDist });
}
}
}
return distances[end];
}
export default findShortestPath;

View File

@@ -1,35 +1,29 @@
async function findShortestPath(graph, start, end) { async function findShortestPath(graph, start, end) {
const { default: PriorityQueue } = await import( const { default: PriorityQueue } = await import('https://cdn.skypack.dev/js-priority-queue');
'https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/+esm'
);
const distances = Object.keys(graph).reduce((acc, node) => {
acc[node] = node === start ? 0 : Infinity;
return acc;
}, {});
const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist });
pq.queue({ node: start, dist: 0 });
const dist = { [start]: 0 };
const pq = new PriorityQueue({ comparator: (a, b) => a[1] - b[1] });
const visited = new Set(); const visited = new Set();
pq.queue([start, 0]);
while (pq.length) { while (pq.length) {
const { node, dist } = pq.dequeue(); const [node, d] = pq.dequeue();
if (visited.has(node)) continue; if (visited.has(node)) continue;
visited.add(node); visited.add(node);
if (node === end) return dist; if (node === end) return d;
for (const [neighbor, weight] of Object.entries(graph[node] || {})) { for (const [neighbor, weight] of Object.entries(graph[node] || {})) {
const newDist = dist + weight; const newDist = d + weight;
if (newDist < distances[neighbor]) { if (newDist < (dist[neighbor] ?? Infinity)) {
distances[neighbor] = newDist; dist[neighbor] = newDist;
pq.queue({ node: neighbor, dist: newDist }); pq.queue([neighbor, newDist]);
} }
} }
} }
return distances[end]; return Infinity;
} }
export default findShortestPath; export default findShortestPath;

View File

@@ -1,26 +1,22 @@
const findShortestPath = async (graph, start, end) => { async function findShortestPath(graph, start, end) {
const { default: PriorityQueue } = await import( const { default: PriorityQueue } = await import('https://cdn.skypack.dev/js-priority-queue');
'https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/+esm'
);
const distances = Object.keys(graph).reduce((acc, node) => { const distances = Object.keys(graph).reduce((acc, node) => ({ ...acc, [node]: Infinity }), {});
acc[node] = node === start ? 0 : Infinity; distances[start] = 0;
return acc;
}, {});
const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist }); const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist });
const visited = new Set();
pq.queue({ node: start, dist: 0 }); pq.queue({ node: start, dist: 0 });
const visited = new Set();
while (pq.length) { while (pq.length) {
const { node, dist } = pq.dequeue(); const { node, dist } = pq.dequeue();
if (visited.has(node)) continue; if (visited.has(node)) continue;
if (node === end) return dist;
visited.add(node); visited.add(node);
if (node === end) return dist;
for (const [neighbor, weight] of Object.entries(graph[node] || {})) { for (const [neighbor, weight] of Object.entries(graph[node] || {})) {
const newDist = dist + weight; const newDist = dist + weight;
if (newDist < distances[neighbor]) { if (newDist < distances[neighbor]) {
@@ -31,5 +27,5 @@ const findShortestPath = async (graph, start, end) => {
} }
return distances[end]; return distances[end];
}; }
export default findShortestPath; export default findShortestPath;

View File

@@ -1,27 +0,0 @@
async function findShortestPath(graph, startNode, endNode) {
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js');
const distances = { [startNode]: 0 };
const pq = new PriorityQueue({ comparator: (a, b) => a.dist - b.dist });
pq.queue({ node: startNode, dist: 0 });
while (pq.length) {
const { node: u, dist: uDist } = pq.dequeue();
if (u === endNode) return uDist;
if (uDist > (distances[u] ?? Infinity)) continue;
for (const v in graph[u] || {}) {
const newDist = uDist + graph[u][v];
if (newDist < (distances[v] ?? Infinity)) {
distances[v] = newDist;
pq.queue({ node: v, dist: newDist });
}
}
}
return Infinity;
}
export default findShortestPath;

View File

@@ -1,27 +1,33 @@
async function findShortestPath(graph, startNode, endNode) { const findShortestPath = async (graph, start, end) => {
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js'); const cdn = 'https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js';
const { default: PriorityQueue } = await import(cdn);
const distances = { [startNode]: 0 }; const distances = Object.fromEntries(
const pq = new PriorityQueue({ comparator: (a, b) => a.priority - b.priority }); Object.keys(graph).map(node => [node, Infinity])
);
distances[start] = 0;
pq.queue({ value: startNode, priority: 0 }); const pq = new PriorityQueue({
comparator: (a, b) => a.priority - b.priority
});
pq.queue({ value: start, priority: 0 });
while (pq.length) { while (pq.length) {
const { value: currentNode } = pq.dequeue(); const { value: current } = pq.dequeue();
if (currentNode === endNode) break; if (current === end) break;
const neighbors = graph[currentNode] ?? {}; const neighbors = graph[current] || {};
for (const [neighbor, weight] of Object.entries(neighbors)) { for (const [neighbor, weight] of Object.entries(neighbors)) {
const newDist = distances[currentNode] + weight; const newDist = distances[current] + weight;
if (newDist < (distances[neighbor] ?? Infinity)) { if (newDist < distances[neighbor]) {
distances[neighbor] = newDist; distances[neighbor] = newDist;
pq.queue({ value: neighbor, priority: newDist }); pq.queue({ value: neighbor, priority: newDist });
} }
} }
} }
return distances[endNode] ?? Infinity; return distances[end];
} };
export default findShortestPath; export default findShortestPath;

View File

@@ -1,32 +0,0 @@
const findShortestPath = async (graph, start, end) => {
const { default: PriorityQueue } = await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/priority-queue.min.js');
const dist = { [start]: 0 };
const pq = new PriorityQueue({ comparator: (a, b) => a.weight - b.weight });
pq.enqueue({ node: start, weight: 0 });
while (pq.length) {
const { node: u, weight: uWeight } = pq.dequeue();
if (uWeight > (dist[u] ?? Infinity)) {
continue;
}
if (u === end) {
return uWeight;
}
for (const v in graph[u] || {}) {
const newWeight = uWeight + graph[u][v];
if (newWeight < (dist[v] ?? Infinity)) {
dist[v] = newWeight;
pq.enqueue({ node: v, weight: newWeight });
}
}
}
return Infinity;
};
export default findShortestPath;

View File

@@ -1,29 +1,24 @@
let u; let u
const loadQueue=async()=> (u=u||import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.2.0/es6.js')).then(r=>r.default); const o=()=>u||(u=import('https://cdn.skypack.dev/js-priority-queue').then(r=>r.default||r))
async function findShortestPath(g,s,t){
export async function findShortestPath(graph,start,end){ const PQ=await o()
if(start===end) return 0; if(s===t)return 0
if(!graph||typeof graph!=='object') return Infinity; const d={},q=new PQ({comparator:(a,b)=>a[1]-b[1]})
const own=Object.prototype.hasOwnProperty; for(const k in g)d[k]=Infinity
if(!own.call(graph,start)||!own.call(graph,end)) return Infinity; d[s]=0
const PriorityQueue=await loadQueue(); q.queue([s,0])
const dist=new Map([[start,0]]); while(q.length){
const heap=new PriorityQueue({comparator:(a,b)=>a[1]-b[1]}); const[n,w]=q.dequeue()
heap.queue([start,0]); if(w>d[n])continue
while(heap.length){ if(n===t)return w
const [node,score]=heap.dequeue(); for(const[nb,c]of Object.entries(g[n]||{})){
if(score>dist.get(node)) continue; const nw=w+c
if(node===end) return score; if(nw<d[nb]){
const edges=graph[node]; d[nb]=nw
if(!edges) continue; q.queue([nb,nw])
for(const [next,weight] of Object.entries(edges)){
const total=score+weight;
if(total<(dist.get(next)??Infinity)){
dist.set(next,total);
heap.queue([next,total]);
} }
} }
} }
return Infinity; return Infinity
} }
export default findShortestPath; export default findShortestPath;

View File

@@ -0,0 +1,24 @@
async function findShortestPath(g,s,e){
if(!g||!g[s]||!g[e])return Infinity
const{default:PriorityQueue}=await import('https://cdn.jsdelivr.net/npm/js-priority-queue@0.1.5/js/priority-queue.min.js')
const d={},v={}
Object.keys(g).forEach(k=>d[k]=k===s?0:Infinity)
const q=new PriorityQueue({comparator:(a,b)=>a.w-b.w})
q.queue({n:s,w:0})
while(q.length){
const{n,w}=q.dequeue()
if(v[n])continue
v[n]=1
if(n===e)return w
const nbrs=g[n]
for(const x in nbrs){
const nw=w+nbrs[x]
if(nw<d[x]){
d[x]=nw
q.queue({n:x,w:nw})
}
}
}
return Infinity
}
export default findShortestPath;

View File

@@ -1,18 +1,18 @@
async function findConvexHull(points) { async function findConvexHull(points) {
const { sortBy, uniqWith } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.js'); const _ = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js').then(m => m.default);
if (points.length < 3) return points; if (points.length < 3) return points;
const sorted = sortBy(uniqWith(points, (a, b) => a.x === b.x && a.y === b.y), ['x', 'y']); const sorted = _.sortBy(_.uniqWith(points, _.isEqual), ['x', 'y']);
if (sorted.length < 3) return sorted; if (sorted.length < 3) return sorted;
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
const buildHull = arr => { const buildHull = pts => {
const hull = []; const hull = [];
for (const p of arr) { for (const p of pts) {
while (hull.length > 1 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) { while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
hull.pop(); hull.pop();
} }
hull.push(p); hull.push(p);

View File

@@ -1,9 +1,9 @@
async function findConvexHull(points) { async function findConvexHull(points) {
const { sortBy, uniqWith } = await import('https://cdn.skypack.dev/lodash'); const _ = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js').then(m => m.default);
if (points.length < 3) return points; if (points.length < 3) return points;
const sorted = sortBy(uniqWith(points, (a, b) => a.x === b.x && a.y === b.y), ['x', 'y']); const sorted = _.sortBy(_.uniqWith(points, _.isEqual), ['x', 'y']);
if (sorted.length < 3) return sorted; if (sorted.length < 3) return sorted;
@@ -12,7 +12,7 @@ async function findConvexHull(points) {
const buildHull = arr => { const buildHull = arr => {
const hull = []; const hull = [];
for (const p of arr) { for (const p of arr) {
while (hull.length > 1 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) { while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
hull.pop(); hull.pop();
} }
hull.push(p); hull.push(p);
@@ -21,6 +21,9 @@ async function findConvexHull(points) {
return hull; return hull;
}; };
return [...buildHull(sorted), ...buildHull(sorted.slice().reverse())]; const lower = buildHull(sorted);
const upper = buildHull([...sorted].reverse());
return [...lower, ...upper].reverse();
} }
export default findConvexHull; export default findConvexHull;

View File

@@ -1,15 +1,15 @@
async function findConvexHull(points) { const findConvexHull = async (points) => {
const { sortBy, uniqWith, isEqual } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js'); const { sortBy, uniqWith } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js');
if (points.length < 3) return points; if (points.length < 3) return points;
const sorted = sortBy(uniqWith(points, isEqual), ['x', 'y']); const sorted = sortBy(uniqWith(points, (a, b) => a.x === b.x && a.y === b.y), ['x', 'y']);
if (sorted.length < 3) return sorted; if (sorted.length < 3) return sorted;
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
const buildHull = (pts) => { const buildHalf = (pts) => {
const hull = []; const hull = [];
for (const p of pts) { for (const p of pts) {
while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) { while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
@@ -21,9 +21,9 @@ async function findConvexHull(points) {
return hull; return hull;
}; };
const lower = buildHull(sorted); const lower = buildHalf(sorted);
const upper = buildHull([...sorted].reverse()); const upper = buildHalf([...sorted].reverse());
return [...lower, ...upper].reverse(); return [...lower, ...upper].reverse();
} };
export default findConvexHull; export default findConvexHull;

View File

@@ -1,33 +0,0 @@
async function findConvexHull(points) {
const _ = await import('https://cdn.jsdelivr.net/npm/lodash@4.17.21/+esm');
const uniquePoints = _.uniqWith(points, _.isEqual);
if (uniquePoints.length < 3) {
return uniquePoints;
}
const sorted = _.sortBy(uniquePoints, ['x', 'y']);
const crossProduct = (p1, p2, p3) =>
(p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
const buildHalfHull = (pts) => {
const hull = [];
for (const p of pts) {
while (hull.length >= 2 && crossProduct(hull.at(-2), hull.at(-1), p) <= 0) {
hull.pop();
}
hull.push(p);
}
return hull;
};
const lower = buildHalfHull(sorted);
const upper = buildHalfHull([...sorted].reverse());
const hull = [...lower.slice(0, -1), ...upper.slice(0, -1)];
return [hull[0], ...hull.slice(1).reverse()];
}
export default findConvexHull;

View File

@@ -1,34 +1,29 @@
async function findConvexHull(points) { async function findConvexHull(points) {
const _ = await import('https://cdn.jsdelivr.net/npm/lodash@4.17.21/+esm'); const _ = await import('https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js');
const uniqueSortedPoints = _.sortBy( const sortedUniquePoints = _.uniqWith(_.sortBy(points, ['x', 'y']), _.isEqual);
_.uniqBy(points, p => `${p.x}:${p.y}`),
['x', 'y']
);
if (uniqueSortedPoints.length < 3) { if (sortedUniquePoints.length < 3) {
return uniqueSortedPoints; return sortedUniquePoints;
} }
const crossProduct = (p1, p2, p3) => const crossProduct = (p1, p2, p3) =>
(p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x); (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
const buildHalfHull = pts => { const buildHalfHull = (pts) => {
const half = []; const hull = [];
for (const p of pts) { for (const p of pts) {
while (half.length >= 2 && crossProduct(half.at(-2), half.at(-1), p) <= 0) { while (hull.length >= 2 && crossProduct(hull.at(-2), hull.at(-1), p) <= 0) {
half.pop(); hull.pop();
} }
half.push(p); hull.push(p);
} }
return half; return hull;
}; };
const lower = buildHalfHull(uniqueSortedPoints); const lowerHull = buildHalfHull(sortedUniquePoints);
const upper = buildHalfHull([...uniqueSortedPoints].reverse()); const upperHull = buildHalfHull([...sortedUniquePoints].reverse());
const ccwHull = lower.slice(0, -1).concat(upper.slice(0, -1)); return [...upperHull.slice(0, -1), ...lowerHull.slice(0, -1)];
return [ccwHull[0], ...ccwHull.slice(1).reverse()];
} }
export default findConvexHull; export default findConvexHull;

View File

@@ -1,35 +0,0 @@
const findConvexHull = async (points) => {
const _ = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/+esm');
const sortedPoints = _.sortBy(points, ['x', 'y']);
const uniquePoints = _.uniqWith(sortedPoints, _.isEqual);
if (uniquePoints.length < 3) {
return uniquePoints;
}
const crossProduct = (p1, p2, p3) =>
(p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
const lower = [];
for (const p of uniquePoints) {
while (lower.length >= 2 && crossProduct(lower.at(-2), lower.at(-1), p) <= 0) {
lower.pop();
}
lower.push(p);
}
const upper = [];
for (let i = uniquePoints.length - 1; i >= 0; i--) {
const p = uniquePoints[i];
while (upper.length >= 2 && crossProduct(upper.at(-2), upper.at(-1), p) <= 0) {
upper.pop();
}
upper.push(p);
}
const hull = [...lower.slice(0, -1), ...upper.slice(0, -1)];
return hull.reverse();
};
export default findConvexHull;

View File

@@ -1,23 +1,19 @@
const lod=import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.js'); async function findConvexHull(points){
const {default:_}=await import('https://cdn.skypack.dev/lodash-es');
export async function findConvexHull(input){ const pts=_.uniqWith(_.sortBy(points,['x','y']),(a,b)=>a.x===b.x&&a.y===b.y);
if(!Array.isArray(input)) return []; if(pts.length<3) return pts.slice();
const {uniqBy:uniq,sortBy:sort}=await lod; const c=(o,a,b)=>(a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
const pts=sort(uniq(input.filter(p=>p&&Number.isFinite(p.x)&&Number.isFinite(p.y)).map(p=>({x:+p.x,y:+p.y})),p=>`${p.x}:${p.y}`),['x','y']); const l=[],u=[];
if(pts.length<2) return pts; for(const p of pts){
const area=(a,b,c)=>(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x); while(l.length>1&&c(l.at(-2),l.at(-1),p)<=0)l.pop();
const walk=list=>{ l.push(p);
const res=[];
for(const p of list){
while(res.length>1&&area(res[res.length-2],res[res.length-1],p)<=0)res.pop();
res.push(p);
} }
return res; for(let i=pts.length;i--;){
}; const p=pts[i];
const low=walk(pts); while(u.length>1&&c(u.at(-2),u.at(-1),p)<=0)u.pop();
const up=walk(pts.slice().reverse()); u.push(p);
const hull=low.slice(0,-1).concat(up.slice(0,-1)); }
const out=hull.length?hull:pts.slice(0,2); l.pop();u.pop();
return out.length<3?out:out.reverse(); return [...l,...u].reverse();
} }
export default findConvexHull; export default findConvexHull;

View File

@@ -0,0 +1,29 @@
async function findConvexHull(points) {
if (!Array.isArray(points) || points.length < 3) return points || [];
const { default: _ } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.default.min.js');
const ps = _.uniqWith(points, (a, b) => a.x === b.x && a.y === b.y);
if (ps.length < 3) return ps;
const pts = _.sortBy(ps, ['x', 'y']);
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
const lower = [];
for (let p of pts) {
for (; lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0;) lower.pop();
lower.push(p);
}
const upper = [];
for (let i = pts.length - 1; i >= 0; i--) {
const p = pts[i];
for (; upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0;) upper.pop();
upper.push(p);
}
const hull = lower.slice(0, -1).concat(upper.slice(0, -1));
if (hull.length <= 2) return hull;
let area = 0;
for (let i = 0; i < hull.length; i++) {
const j = (i + 1) % hull.length;
area += hull[i].x * hull[j].y - hull[j].x * hull[i].y;
}
if (area > 0) hull.reverse();
return hull;
}
export default findConvexHull;

View File

@@ -2,7 +2,6 @@ const findLISLength = async (arr) => {
if (!arr?.length) return 0; if (!arr?.length) return 0;
const { bisectLeft } = await import('https://cdn.skypack.dev/d3-array'); const { bisectLeft } = await import('https://cdn.skypack.dev/d3-array');
const tails = []; const tails = [];
for (const num of arr) { for (const num of arr) {
@@ -12,5 +11,4 @@ const findLISLength = async (arr) => {
return tails.length; return tails.length;
}; };
export default findLISLength; export default findLISLength;

View File

@@ -1,8 +1,8 @@
const findLISLength = async (arr) => { async function findLISLength(arr) {
if (!arr?.length) return 0;
const { bisectLeft } = await import('https://cdn.skypack.dev/d3-array'); const { bisectLeft } = await import('https://cdn.skypack.dev/d3-array');
if (!arr?.length) return 0;
const tails = []; const tails = [];
for (const num of arr) { for (const num of arr) {
@@ -11,5 +11,5 @@ const findLISLength = async (arr) => {
} }
return tails.length; return tails.length;
}; }
export default findLISLength; export default findLISLength;

View File

@@ -1,15 +1,15 @@
const findLISLength = async (nums) => { async function findLISLength(nums) {
const { bisectLeft } = await import('https://cdn.skypack.dev/d3-array');
if (!nums?.length) return 0; if (!nums?.length) return 0;
const { bisectLeft } = await import('https://cdn.skypack.dev/d3-array');
const tails = []; const tails = [];
for (const num of nums) { for (const num of nums) {
const idx = bisectLeft(tails, num); const pos = bisectLeft(tails, num);
tails[idx] = num; tails[pos] = num;
} }
return tails.length; return tails.length;
}; }
export default findLISLength; export default findLISLength;

View File

@@ -1,14 +0,0 @@
const findLISLength = async (nums) => {
if (!nums?.length) return 0;
const { bisectLeft } = await import('https://cdn.jsdelivr.net/npm/d3-array@3/+esm');
const tails = [];
for (const num of nums) {
tails[bisectLeft(tails, num)] = num;
}
return tails.length;
};
export default findLISLength;

View File

@@ -1,13 +1,15 @@
const findLISLength = async (nums) => { const findLISLength = async (nums) => {
if (!nums?.length) return 0; if (!nums?.length) {
return 0;
const { bisectLeft } = await import('https://cdn.jsdelivr.net/npm/d3-array@3/+esm');
const tails = [];
for (const num of nums) {
tails[bisectLeft(tails, num)] = num;
} }
const { bisectLeft } = await import("https://cdn.jsdelivr.net/npm/d3-array@3");
const tails = nums.reduce((sub, num) => {
sub[bisectLeft(sub, num)] = num;
return sub;
}, []);
return tails.length; return tails.length;
}; };
export default findLISLength; export default findLISLength;

View File

@@ -1,13 +0,0 @@
const findLISLength = async (nums) => {
if (!Array.isArray(nums) || !nums.length) return 0;
const { bisectLeft } = await import("https://cdn.jsdelivr.net/npm/d3-array");
const tails = [];
for (const num of nums) {
tails[bisectLeft(tails, num)] = num;
}
return tails.length;
};
export default findLISLength;

View File

@@ -1,14 +1,14 @@
const loadBisectLeft=(()=>{let cache;return()=>cache||(cache=import('https://cdn.jsdelivr.net/npm/d3-array@3/+esm').then(({bisectLeft})=>bisectLeft));})(); const findLISLength=async a=>{
export async function findLISLength(nums){ if(!Array.isArray(a)) throw new TypeError('Expected an array')
if(!Array.isArray(nums)) throw new TypeError('Expected an array'); const {bisectLeft:b}=await (findLISLength.m??=import('https://cdn.jsdelivr.net/npm/d3-array@3/+esm'))
if(!nums.length) return 0; const t=[]
const bisectLeft=await loadBisectLeft(); for(const v of a){
const tails=[]; if(!Number.isFinite(v)) throw new TypeError('Array values must be finite numbers')
for(const value of nums){ const i=b(t,v)
if(!Number.isFinite(value)) throw new TypeError('Array must contain only finite numbers'); if(i===t.length) t.push(v)
const index=bisectLeft(tails,value,0,tails.length); else t[i]=v
tails[index]=value;
} }
return tails.length; return t.length
} }
window.findLISLength=findLISLength
export default findLISLength; export default findLISLength;

View File

@@ -0,0 +1,12 @@
async function findLISLength(a){
if(!a||!a.length)return 0
const{bisectLeft}=await import('https://cdn.jsdelivr.net/npm/d3-array@3/+esm')
const t=[]
for(const x of a){
const i=bisectLeft(t,x)
if(i===t.length)t.push(x)
else t[i]=x
}
return t.length
}
export default findLISLength;

View File

@@ -1,5 +0,0 @@
const calculateDeterminant = async (matrix) => {
const { det } = await import('https://cdn.jsdelivr.net/npm/mathjs@13.0.0/es/index.js');
return det(matrix);
};
export default calculateDeterminant;

View File

@@ -1,5 +1,5 @@
const calculateDeterminant = async (matrix) => { const calculateDeterminant = async m => {
const { det } = await import('https://cdn.jsdelivr.net/npm/mathjs@13.0.0/es/index.js'); const { det } = await import('https://cdn.jsdelivr.net/npm/mathjs@13.0.0/es/math.mjs');
return det(matrix); return det(m);
}; };
export default calculateDeterminant; export default calculateDeterminant;

View File

@@ -1,5 +0,0 @@
const calculateDeterminant = async (matrix) => {
const { det } = await import('https://esm.sh/mathjs');
return det(matrix);
};
export default calculateDeterminant;

View File

@@ -1,7 +1,5 @@
const src='https://cdn.jsdelivr.net/npm/mathjs@11.11.0/+esm' async function calculateDeterminant(m){
export const calculateDeterminant=async m=>{ const {det}=await import('https://cdn.jsdelivr.net/npm/mathjs@11.11.0/dist/esm/math.js')
if(!Array.isArray(m)||!m.length||m.some(r=>!Array.isArray(r)||r.length!==m.length))throw new TypeError('Matrix must be square')
const{det}=await import(src)
return det(m) return det(m)
} }
export default calculateDeterminant; export default calculateDeterminant;

View File

@@ -0,0 +1,7 @@
async function calculateDeterminant(m){
if(!Array.isArray(m)||!m.length||!m.every(r=>Array.isArray(r)&&r.length===m.length))throw new Error('Input must be an n x n matrix');
const {create,all,det}=await import('https://cdn.jsdelivr.net/npm/mathjs@11.11.0/+esm');
const math=create(all);
return math.det(m);
}
export default calculateDeterminant;

View File

@@ -0,0 +1,5 @@
async function parseMarkdown(md) {
const { marked } = await import('https://cdn.jsdelivr.net/npm/marked@11.1.1/+esm');
return marked.parse(md);
}
export default parseMarkdown;

View File

@@ -0,0 +1,5 @@
async function parseMarkdown(md) {
const { marked } = await import('https://cdn.jsdelivr.net/npm/marked@11.1.1/+esm');
return marked.parse(md);
}
export default parseMarkdown;

View File

@@ -0,0 +1,5 @@
async function parseMarkdown(md) {
const { marked } = await import('https://cdn.jsdelivr.net/npm/marked@11.1.1/+esm');
return marked.parse(md);
}
export default parseMarkdown;

View File

@@ -0,0 +1,5 @@
const parseMarkdown = async (md) => {
const { marked } = await import('https://cdn.jsdelivr.net/npm/marked@5/+esm');
return marked.parse(md);
};
export default parseMarkdown;

View File

@@ -0,0 +1,15 @@
let md,pd
const getMd=()=>md||(md=import('https://cdn.jsdelivr.net/npm/marked@12.0.2/+esm').then(x=>{
let m=x.marked||x.default
m.setOptions?.({gfm:true,breaks:true,smartypants:true})
return s=>m.parse?m.parse(s):m(s)
}))
const getPurify=()=>pd||(pd=import('https://esm.sh/dompurify@3?target=es2020').then(x=>{
let d=x.default||x.DOMPurify||x
return typeof d.sanitize=='function'?d:d(window)
}))
async function parseMarkdown(s){
let [render,pur]=await Promise.all([getMd(),getPurify()])
return pur.sanitize(render(s))
}
export default parseMarkdown;

View File

@@ -0,0 +1,11 @@
async function parseMarkdown(markdown) {
const {marked} = await import('https://cdn.jsdelivr.net/npm/marked@12.0.2/lib/marked.esm.js')
marked.setOptions({
gfm:true,
breaks:false,
headerIds:true,
mangle:false
})
return marked.parse(String(markdown ?? ''))
}
export default parseMarkdown;

View File

@@ -0,0 +1,26 @@
async function processCSV(csv, config) {
const { parse } = await import('https://cdn.skypack.dev/papaparse@5.4.1');
const { filterColumn: fc, filterValue: fv, groupBy: gb, aggregateColumn: ac, operation: op } = config;
const { data } = parse(csv, { header: true, skipEmptyLines: true });
const filtered = data.filter(r => r[fc] == fv);
const grouped = filtered.reduce((acc, row) => {
const key = row[gb];
if (!acc[key]) acc[key] = [];
acc[key].push(row);
return acc;
}, {});
return Object.entries(grouped).map(([key, rows]) => {
const vals = rows.map(r => parseFloat(r[ac]) || 0);
const result = op === 'sum'
? vals.reduce((a, b) => a + b, 0)
: op === 'avg'
? vals.reduce((a, b) => a + b, 0) / vals.length
: vals.length;
return { [gb]: key, result };
});
}
export default processCSV;

View File

@@ -0,0 +1,30 @@
const processCSV = async (csv, { filterColumn, filterValue, groupBy, aggregateColumn, operation }) => {
const { default: Papa } = await import('https://cdn.jsdelivr.net/npm/papaparse@5.4.1/+esm');
const { data } = Papa.parse(csv, { header: true, skipEmptyLines: true });
const filtered = data.filter(row => row[filterColumn] == filterValue);
const grouped = filtered.reduce((acc, row) => {
const key = row[groupBy];
if (!acc[key]) acc[key] = [];
acc[key].push(row);
return acc;
}, {});
return Object.entries(grouped).map(([key, rows]) => {
let result;
if (operation === 'count') {
result = rows.length;
} else {
const values = rows.map(r => parseFloat(r[aggregateColumn]) || 0);
result = operation === 'sum'
? values.reduce((a, b) => a + b, 0)
: values.reduce((a, b) => a + b, 0) / values.length;
}
return { [groupBy]: key, [operation]: result };
});
};
export default processCSV;

View File

@@ -0,0 +1,30 @@
async function processCSV(csvString, config) {
const { parse } = await import('https://cdn.skypack.dev/papaparse@5.4.1');
const { filterColumn: fc, filterValue: fv, groupBy: gb, aggregateColumn: ac, operation: op } = config;
const { data } = parse(csvString, { header: true, skipEmptyLines: true });
const filtered = data.filter(row => row[fc] == fv);
const grouped = filtered.reduce((acc, row) => {
const key = row[gb];
if (!acc[key]) acc[key] = [];
acc[key].push(row);
return acc;
}, {});
return Object.entries(grouped).map(([key, rows]) => {
let result;
if (op === 'count') {
result = rows.length;
} else {
const values = rows.map(r => parseFloat(r[ac]) || 0);
result = op === 'sum'
? values.reduce((a, b) => a + b, 0)
: values.reduce((a, b) => a + b, 0) / values.length;
}
return { [gb]: key, result };
});
}
export default processCSV;

View File

@@ -0,0 +1,40 @@
const processCSV = async (csvString, {
filterColumn,
filterValue,
groupBy,
aggregateColumn,
operation
}) => {
const { default: Papa } = await import('https://cdn.jsdelivr.net/npm/papaparse@5.4.1/+esm');
const aggregations = {
sum: g => g.s,
count: g => g.c,
avg: g => g.c ? g.s / g.c : 0,
};
const groupedData = Papa.parse(csvString, {
header: true,
skipEmptyLines: true,
dynamicTyping: true,
}).data
.filter(row => row[filterColumn] === filterValue)
.reduce((acc, row) => {
const key = row[groupBy];
const val = row[aggregateColumn];
acc[key] = acc[key] || { s: 0, c: 0 };
if (typeof val === 'number' && !isNaN(val)) {
acc[key].s += val;
}
acc[key].c++;
return acc;
}, {});
return Object.entries(groupedData).map(([key, group]) => ({
[groupBy]: /^-?\d+(\.\d+)?$/.test(key) ? Number(key) : key,
[operation]: aggregations[operation](group),
}));
};
export default processCSV;

View File

@@ -0,0 +1,20 @@
let cache;
async function processCSV(csv,cfg){
const {csvParse,rollups}=await (cache??=import('https://cdn.jsdelivr.net/npm/d3@7/+esm'));
const {filterColumn,filterValue,groupBy,aggregateColumn,operation}=cfg;
if(!['sum','avg','count'].includes(operation))throw new Error('Unsupported operation');
if(!csv||!filterColumn||!groupBy)throw new Error('Missing essentials');
if(operation!=='count'&&!aggregateColumn)throw new Error('Missing aggregateColumn');
const rows=csvParse(csv).filter(r=>r[filterColumn]==filterValue);
if(!rows.length)return[];
const fn=operation==='count'?v=>v.length:v=>{
let s=0,n=0;
for(const row of v){
const val=+row[aggregateColumn];
if(!Number.isNaN(val)){s+=val;n++;}
}
return operation==='sum'?s:n?s/n:0;
};
return rollups(rows,fn,r=>r[groupBy]).map(([k,v])=>({[groupBy]:k,[operation]:v}));
}
export default processCSV;

View File

@@ -0,0 +1,30 @@
async function processCSV(csv, cfg) {
const {filterColumn,filterValue,groupBy,aggregateColumn,operation} = cfg
if(!csv||!filterColumn||!groupBy||!operation) throw new Error('Invalid configuration')
const [{parse},{default:lodash}] = await Promise.all([
import('https://cdn.jsdelivr.net/npm/papaparse@5.4.1/+esm'),
import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/+esm')
])
const {data,errors} = parse(csv,{header:true,skipEmptyLines:true,dynamicTyping:true})
if(errors?.length) throw new Error('CSV parse error')
const rows = data.filter(r=>r[filterColumn]===filterValue)
if(!rows.length) return []
const grouped = lodash.groupBy(rows,r=>r[groupBy])
const out = []
for(const k in grouped){
const g = grouped[k]
let v
if(operation==='count') v = g.length
else{
const nums = g.map(r=>Number(r[aggregateColumn])).filter(n=>Number.isFinite(n))
if(!nums.length) { v = operation==='sum'?0:null }
else{
const s = nums.reduce((a,b)=>a+b,0)
v = operation==='sum'?s:s/nums.length
}
}
out.push({[groupBy]:k,result:v})
}
return out
}
export default processCSV;

View File

@@ -0,0 +1,78 @@
const findAvailableSlots = async (cal1, cal2, constraints) => {
const { default: dayjs } = await import('https://cdn.jsdelivr.net/npm/dayjs@1.11.10/+esm');
const [{ default: utc }, { default: customParseFormat }, { default: isBetween }] = await Promise.all([
import('https://cdn.jsdelivr.net/npm/dayjs@1.11.10/plugin/utc.js'),
import('https://cdn.jsdelivr.net/npm/dayjs@1.11.10/plugin/customParseFormat.js'),
import('https://cdn.jsdelivr.net/npm/dayjs@1.11.10/plugin/isBetween.js')
]);
dayjs.extend(utc);
dayjs.extend(customParseFormat);
dayjs.extend(isBetween);
const { durationMinutes, searchRange, workHours } = constraints;
const duration = durationMinutes;
const mergedBusy = [...cal1, ...cal2]
.map(({ start, end }) => ({ start: dayjs(start), end: dayjs(end) }))
.sort((a, b) => a.start - b.start)
.reduce((acc, curr) => {
if (!acc.length) return [curr];
const last = acc[acc.length - 1];
if (curr.start <= last.end) {
last.end = dayjs.max(last.end, curr.end);
return acc;
}
return [...acc, curr];
}, []);
const searchStart = dayjs(searchRange.start);
const searchEnd = dayjs(searchRange.end);
const [workStart, workEnd] = [workHours.start, workHours.end];
const slots = [];
let current = searchStart;
while (current < searchEnd) {
const dayStart = dayjs(`${current.format('YYYY-MM-DD')}T${workStart}`);
const dayEnd = dayjs(`${current.format('YYYY-MM-DD')}T${workEnd}`);
const workStart_ = dayjs.max(current, dayStart);
const workEnd_ = dayjs.min(searchEnd, dayEnd);
if (workStart_ < workEnd_) {
const dayBusy = mergedBusy.filter(b =>
b.start < workEnd_ && b.end > workStart_
);
let pointer = workStart_;
for (const busy of dayBusy) {
const gapEnd = dayjs.min(busy.start, workEnd_);
while (pointer.add(duration, 'minute') <= gapEnd) {
slots.push({
start: pointer.toISOString(),
end: pointer.add(duration, 'minute').toISOString()
});
pointer = pointer.add(duration, 'minute');
}
pointer = dayjs.max(pointer, busy.end);
}
while (pointer.add(duration, 'minute') <= workEnd_) {
slots.push({
start: pointer.toISOString(),
end: pointer.add(duration, 'minute').toISOString()
});
pointer = pointer.add(duration, 'minute');
}
}
current = current.add(1, 'day').startOf('day');
}
return slots;
};
export default findAvailableSlots;

View File

@@ -0,0 +1,93 @@
const findAvailableSlots = async (cal1, cal2, constraints) => {
const { default: dayjs } = await import('https://cdn.skypack.dev/dayjs@1.11.10');
const { default: utc } = await import('https://cdn.skypack.dev/dayjs@1.11.10/plugin/utc');
const { default: customParseFormat } = await import('https://cdn.skypack.dev/dayjs@1.11.10/plugin/customParseFormat');
dayjs.extend(utc);
dayjs.extend(customParseFormat);
const { durationMinutes, searchRange, workHours } = constraints;
const duration = durationMinutes * 60000;
const merged = [...cal1, ...cal2]
.map(({ start, end }) => ({ start: new Date(start).getTime(), end: new Date(end).getTime() }))
.sort((a, b) => a.start - b.start);
const busy = merged.reduce((acc, slot) => {
if (!acc.length) return [slot];
const last = acc[acc.length - 1];
if (slot.start <= last.end) {
last.end = Math.max(last.end, slot.end);
return acc;
}
acc.push(slot);
return acc;
}, []);
const rangeStart = new Date(searchRange.start).getTime();
const rangeEnd = new Date(searchRange.end).getTime();
const [whStart, whEnd] = [workHours.start, workHours.end];
const isWithinWorkHours = (ts) => {
const d = dayjs(ts);
const time = d.format('HH:mm');
return time >= whStart && time <= whEnd;
};
const getWorkDayBounds = (ts) => {
const d = dayjs(ts);
const [h1, m1] = whStart.split(':').map(Number);
const [h2, m2] = whEnd.split(':').map(Number);
return {
start: d.hour(h1).minute(m1).second(0).millisecond(0).valueOf(),
end: d.hour(h2).minute(m2).second(0).millisecond(0).valueOf()
};
};
const slots = [];
let current = rangeStart;
for (let ts = rangeStart; ts < rangeEnd; ts += 86400000) {
const { start: dayStart, end: dayEnd } = getWorkDayBounds(ts);
current = Math.max(current, dayStart);
busy.forEach(({ start: bStart, end: bEnd }) => {
if (bStart > current && bStart <= dayEnd) {
let slotStart = current;
while (slotStart + duration <= Math.min(bStart, dayEnd)) {
const slotEnd = slotStart + duration;
if (isWithinWorkHours(slotStart) && isWithinWorkHours(slotEnd)) {
slots.push({
start: new Date(slotStart).toISOString(),
end: new Date(slotEnd).toISOString()
});
}
slotStart = slotEnd;
}
current = Math.max(current, bEnd);
}
if (bEnd > current && bEnd <= dayEnd) {
current = Math.max(current, bEnd);
}
});
if (current < dayEnd) {
let slotStart = current;
while (slotStart + duration <= dayEnd) {
const slotEnd = slotStart + duration;
if (isWithinWorkHours(slotStart) && isWithinWorkHours(slotEnd)) {
slots.push({
start: new Date(slotStart).toISOString(),
end: new Date(slotEnd).toISOString()
});
}
slotStart = slotEnd;
}
}
current = dayStart + 86400000;
}
return slots;
};
export default findAvailableSlots;

View File

@@ -0,0 +1,74 @@
const findAvailableSlots = async (cal1, cal2, constraints) => {
const { DateTime, Interval } = await import('https://cdn.skypack.dev/luxon');
const { durationMinutes: dur, searchRange: sr, workHours: wh } = constraints;
const [srStart, srEnd] = [DateTime.fromISO(sr.start), DateTime.fromISO(sr.end)];
const [whStart, whEnd] = wh.start.split(':').map(Number);
const toInterval = ({ start, end }) =>
Interval.fromDateTimes(DateTime.fromISO(start), DateTime.fromISO(end));
const busySlots = [...cal1, ...cal2]
.map(toInterval)
.sort((a, b) => a.start - b.start);
const merged = busySlots.reduce((acc, curr) => {
if (!acc.length) return [curr];
const last = acc[acc.length - 1];
return last.overlaps(curr) || last.abutsStart(curr)
? [...acc.slice(0, -1), last.union(curr)]
: [...acc, curr];
}, []);
const isWorkHours = dt => {
const h = dt.hour, m = dt.minute;
const mins = h * 60 + m;
const whStartMins = whStart[0] * 60 + whStart[1];
const whEndMins = whEnd[0] * 60 + whEnd[1];
return mins >= whStartMins && mins < whEndMins;
};
const slots = [];
let curr = srStart;
while (curr < srEnd) {
const dayStart = curr.set({ hour: whStart[0], minute: whStart[1], second: 0 });
const dayEnd = curr.set({ hour: whEnd[0], minute: whEnd[1], second: 0 });
if (dayStart >= srEnd) break;
const effectiveStart = dayStart > srStart ? dayStart : srStart;
const effectiveEnd = dayEnd < srEnd ? dayEnd : srEnd;
let pointer = effectiveStart;
while (pointer.plus({ minutes: dur }) <= effectiveEnd) {
const slotEnd = pointer.plus({ minutes: dur });
const slotInterval = Interval.fromDateTimes(pointer, slotEnd);
const isFree = !merged.some(busy =>
busy.overlaps(slotInterval) || slotInterval.overlaps(busy)
);
if (isFree && isWorkHours(pointer) && isWorkHours(slotEnd.minus({ minutes: 1 }))) {
slots.push({
start: pointer.toISO(),
end: slotEnd.toISO()
});
pointer = slotEnd;
} else {
const nextBusy = merged.find(busy => busy.start > pointer);
if (nextBusy && nextBusy.start < effectiveEnd) {
pointer = nextBusy.end > pointer ? nextBusy.end : pointer.plus({ minutes: 1 });
} else {
pointer = pointer.plus({ minutes: 1 });
}
}
}
curr = curr.plus({ days: 1 }).startOf('day');
}
return slots;
};
export default findAvailableSlots;

View File

@@ -0,0 +1,56 @@
async function findAvailableSlots(
calendar1,
calendar2,
{ durationMinutes: dur, searchRange: sr, workHours: wh }
) {
const dayjs = (await import('https://cdn.skypack.dev/dayjs')).default;
const utc = (await import('https://cdn.skypack.dev/dayjs/plugin/utc')).default;
dayjs.extend(utc);
const d = (s) => dayjs.utc(s);
const rangeStart = d(sr.start);
const rangeEnd = d(sr.end);
const [whsH, whsM] = wh.start.split(':');
const [wheH, wheM] = wh.end.split(':');
const busySlots = [...calendar1, ...calendar2].map(({ start, end }) => ({
start: d(start),
end: d(end),
}));
for (let day = rangeStart.clone().startOf('d'); day.isBefore(rangeEnd); day = day.add(1, 'd')) {
busySlots.push({ start: day.startOf('d'), end: day.hour(whsH).minute(whsM) });
busySlots.push({ start: day.hour(wheH).minute(wheM), end: day.endOf('d') });
}
const merged = busySlots
.filter(({ start, end }) => start.isBefore(end))
.sort((a, b) => a.start - b.start)
.reduce((acc, cur) => {
const last = acc.at(-1);
if (!last || cur.start.isAfter(last.end)) {
acc.push(cur);
} else if (cur.end.isAfter(last.end)) {
last.end = cur.end;
}
return acc;
}, []);
const available = [];
let cursor = rangeStart;
[...merged, { start: rangeEnd, end: rangeEnd }].forEach((busy) => {
const gapEnd = busy.start;
for (let slotStart = cursor; !slotStart.add(dur, 'm').isAfter(gapEnd); slotStart = slotStart.add(dur, 'm')) {
const slotEnd = slotStart.add(dur, 'm');
available.push({
start: slotStart.toISOString(),
end: slotEnd.toISOString(),
});
}
cursor = cursor.isAfter(busy.end) ? cursor : busy.end;
});
return available;
}
export default findAvailableSlots;

View File

@@ -0,0 +1,55 @@
let df
async function findAvailableSlots(a,b,c){
df??=await import('https://cdn.jsdelivr.net/npm/date-fns@3.6.0/+esm')
const {parseISO:pi,formatISO:fi,eachDayOfInterval:ed,set:st}=df
const sr=pi(c.searchRange.start),er=pi(c.searchRange.end),srT=+sr,erT=+er
const [hs,ms]=c.workHours.start.split(':').map(Number)
const [he,me]=c.workHours.end.split(':').map(Number)
const step=c.durationMinutes*6e4
const norm=u=>{
const arr=u.map(v=>({s:+pi(v.start),e:+pi(v.end)})).filter(v=>v.e>srT&&v.s<erT).map(v=>({s:Math.max(v.s,srT),e:Math.min(v.e,erT)})).filter(v=>v.e>v.s).sort((x,y)=>x.s-y.s)
const out=[]
arr.forEach(v=>{
const w=out[out.length-1]
if(!w||v.s>w.e)out.push({s:v.s,e:v.e})
else w.e=Math.max(w.e,v.e)
})
return out
}
const free=u=>{
const busy=norm(u),days=ed({start:sr,end:er}),out=[]
let i=0
for(const day of days){
const d0=Math.max(+st(day,{hours:hs,minutes:ms,seconds:0,milliseconds:0}),srT)
const d1=Math.min(+st(day,{hours:he,minutes:me,seconds:0,milliseconds:0}),erT)
if(d0>=d1)continue
while(i<busy.length&&busy[i].e<=d0)i++
let cur=d0,j=i
while(j<busy.length&&busy[j].s<d1){
if(busy[j].s>cur)out.push({s:cur,e:Math.min(busy[j].s,d1)})
cur=Math.max(cur,busy[j].e)
if(cur>=d1)break
j++
}
if(cur<d1)out.push({s:cur,e:d1})
i=j
}
return out
}
const x=free(a),y=free(b),res=[]
let i=0,j=0
while(i<x.length&&j<y.length){
const s=Math.max(x[i].s,y[j].s)
const e=Math.min(x[i].e,y[j].e)
if(e-s>=step){
const k=Math.floor((e-s)/step)
for(let n=0;n<k;n++){
const start=s+n*step,end=start+step
res.push({start:fi(new Date(start)),end:fi(new Date(end))})
}
}
x[i].e<y[j].e?i++:x[i].e>y[j].e?j++:(i++,j++)
}
return res
}
export default findAvailableSlots;

View File

@@ -0,0 +1,115 @@
async function findAvailableSlots(calA, calB, c) {
const { DateTime, Interval } = await import('https://cdn.skypack.dev/luxon').then(m => m);
const d = c.durationMinutes;
const z = DateTime.utc().zoneName;
const parseIso = s => DateTime.fromISO(s, { zone: 'utc' });
const parseHm = (hm, base) => {
const [H, M] = hm.split(':').map(Number);
return base.set({ hour: H, minute: M, second: 0, millisecond: 0 });
};
const toIso = dt => dt.toUTC().toISO();
const rStart = parseIso(c.searchRange.start);
const rEnd = parseIso(c.searchRange.end);
const dayRange = [];
for (let d0 = rStart.startOf('day'); d0 < rEnd; d0 = d0.plus({ days: 1 })) {
const ws = parseHm(c.workHours.start, d0);
const we = parseHm(c.workHours.end, d0);
const s = ws < rStart ? rStart : ws;
const e = we > rEnd ? rEnd : we;
if (e > s) dayRange.push(Interval.fromDateTimes(s, e));
}
const normalizeBusy = cal =>
cal
.map(({ start, end }) => {
const s = parseIso(start), e = parseIso(end);
return e > s ? Interval.fromDateTimes(s, e) : null;
})
.filter(Boolean)
.sort((a, b) => a.start - b.start);
const mergeBusy = busy => {
const m = [];
for (const i of busy) {
const last = m[m.length - 1];
if (!last || i.start > last.end) m.push(i);
else if (i.end > last.end) last.end = i.end;
}
return m;
};
const intersectWithRange = (busy, ranges) => {
const res = [];
let j = 0;
for (const r of ranges) {
while (j < busy.length && busy[j].end <= r.start) j++;
let k = j;
while (k < busy.length && busy[k].start < r.end) {
const s = busy[k].start < r.start ? r.start : busy[k].start;
const e = busy[k].end > r.end ? r.end : busy[k].end;
if (e > s) res.push(Interval.fromDateTimes(s, e));
k++;
}
}
return mergeBusy(res);
};
const invertBusy = (busy, ranges) => {
const free = [];
for (const r of ranges) {
let cur = r.start;
for (const b of busy) {
if (b.end <= r.start || b.start >= r.end) continue;
if (b.start > cur) free.push(Interval.fromDateTimes(cur, b.start));
if (b.end > cur) cur = b.end;
if (cur >= r.end) break;
}
if (cur < r.end) free.push(Interval.fromDateTimes(cur, r.end));
}
return free;
};
const intersectFree = (fa, fb) => {
const res = [];
let i = 0, j = 0;
while (i < fa.length && j < fb.length) {
const a = fa[i], b = fb[j];
const s = a.start > b.start ? a.start : b.start;
const e = a.end < b.end ? a.end : b.end;
if (e > s) res.push(Interval.fromDateTimes(s, e));
if (a.end < b.end) i++;
else j++;
}
return res;
};
const filterByDuration = (slots, minutes) => {
const out = [];
const dur = { minutes };
for (const s of slots) {
let cur = s.start;
while (cur.plus(dur) <= s.end) {
const end = cur.plus(dur);
out.push({ start: toIso(cur), end: toIso(end) });
cur = end;
}
}
return out;
};
const busyA = mergeBusy(normalizeBusy(calA));
const busyB = mergeBusy(normalizeBusy(calB));
const busyAIn = intersectWithRange(busyA, dayRange);
const busyBIn = intersectWithRange(busyB, dayRange);
const freeA = invertBusy(busyAIn, dayRange);
const freeB = invertBusy(busyBIn, dayRange);
const mutualFree = intersectFree(freeA, freeB);
return filterByDuration(mutualFree, d);
}
export default findAvailableSlots;

View File

@@ -0,0 +1,18 @@
const validateJSON = async (data, schema) => {
const { default: Ajv } = await import('https://cdn.jsdelivr.net/npm/ajv@8.12.0/dist/2020.min.js');
const ajv = new Ajv({ allErrors: true, strict: false });
const validate = ajv.compile(schema);
const valid = validate(data);
return {
valid,
errors: valid ? [] : validate.errors.map(e => ({
path: e.instancePath || '/',
message: e.message,
keyword: e.keyword,
params: e.params
}))
};
};
export default validateJSON;

View File

@@ -0,0 +1,16 @@
const validateJSON = async (data, schema) => {
const { default: Ajv } = await import('https://cdn.jsdelivr.net/npm/ajv@8/dist/2020.min.js');
const ajv = new Ajv({ allErrors: true, verbose: true });
const validate = ajv.compile(schema);
const valid = validate(data);
return {
valid,
errors: valid ? [] : validate.errors.map(e => ({
path: e.instancePath || '/',
message: e.message,
keyword: e.keyword,
params: e.params
}))
};
};
export default validateJSON;

View File

@@ -0,0 +1,15 @@
const validateJSON = async (data, schema) => {
const { default: Ajv } = await import('https://cdn.jsdelivr.net/npm/ajv@8/dist/ajv.bundle.js');
const ajv = new Ajv({ allErrors: true });
const validate = ajv.compile(schema);
const valid = validate(data);
return {
valid,
errors: valid ? [] : validate.errors.map(e =>
`${e.instancePath || 'root'} ${e.message}`
)
};
};
export default validateJSON;

View File

@@ -0,0 +1,32 @@
let ajvPromise;
const compiledSchemas = new WeakMap();
const validateJSON = async (json, schema) => {
try {
ajvPromise ||= import('https://esm.sh/ajv@8').then(
({ default: Ajv }) => new Ajv({ allErrors: true })
);
const ajv = await ajvPromise;
let validate = compiledSchemas.get(schema);
if (!validate) {
validate = ajv.compile(schema);
compiledSchemas.set(schema, validate);
}
const valid = validate(json);
const errors = valid
? []
: (validate.errors ?? []).map(({ instancePath, message }) =>
`${instancePath.substring(1) || 'root'}: ${message}`
);
return { valid, errors };
} catch (error) {
return {
valid: false,
errors: [error.message || 'An unknown validation error occurred.'],
};
}
};
export default validateJSON;

View File

@@ -0,0 +1,17 @@
let ajvReady
const loadAjv=()=>ajvReady??=(async()=>{
const {default:Ajv}=await import('https://cdn.jsdelivr.net/npm/ajv@8/dist/ajv2020.mjs')
return new Ajv({allErrors:true,strict:false})
})()
async function validateJSON(data,schema){
const ajv=await loadAjv()
const validate=ajv.compile(schema)
const valid=validate(data)
const errors=valid?[]:validate.errors?.map(({instancePath,message,params})=>{
const here=instancePath||'/'
const extra=params&&Object.keys(params).length?JSON.stringify(params):''
return [here,message,extra].filter(Boolean).join(' ')
})||[]
return {valid,errors}
}
export default validateJSON;

View File

@@ -0,0 +1,15 @@
async function validateJSON(data, schema) {
const {default: Ajv} = await import('https://cdn.jsdelivr.net/npm/ajv@8/dist/ajv.min.js')
const ajv = new Ajv({allErrors:true, strict:false})
const validate = ajv.compile(schema)
const valid = validate(data)
if (valid) return {valid:true, errors:[]}
const errors = (validate.errors || []).map(e => {
const path = (e.instancePath || e.dataPath || '') || (e.schemaPath || '')
const msg = e.message || 'Invalid value'
const params = e.params ? JSON.stringify(e.params) : ''
return [path, msg, params].filter(Boolean).join(' - ')
})
return {valid:false, errors}
}
export default validateJSON;