ifcurl/forgejo/custom/public/assets/viewer-util.js
Bruno Postle d60bd044dd forgejo: add JS unit tests, extract pure functions into viewer-util.js
Extract parseCommitHref, isSimpleTypeSelector, subtractIdMap, and
isCommitHash into a new viewer-util.js ES module so they can be tested
without a browser. Convert ifcurl.js from an IIFE to an ES module
importing from viewer-util.js; update footer.tmpl to type="module".

Add node:test suites for viewer-url.js (32 tests) and viewer-util.js
(28 tests). Add npm test script and document it in the build section.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-27 20:24:58 +01:00

39 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
//
// Pure utility functions shared between viewer.js, ifcurl.js, and tests.
// Parse a Forgejo browse path: /owner/repo/src/commit/{40hex}/{treepath}
// Returns {repoPath, hash, treePath} or null.
export function parseCommitHref(href) {
const m = href.match(/^\/([^/]+\/[^/]+)\/src\/commit\/([0-9a-f]{40})\/(.+)$/i);
if (!m) return null;
return { repoPath: m[1], hash: m[2], treePath: m[3] };
}
// Returns true if the selector contains only IFC type names joined by "+".
// E.g. "IfcWall", "IfcWall+IfcSlab". Property filters and name= require
// server-side resolution via /select.
export function isSimpleTypeSelector(selectorStr) {
return selectorStr.split("+").every(part => /^[Ii]fc[A-Za-z0-9]+$/.test(part.trim()));
}
// Set-subtract toRemove from allItems across all model IDs.
// Both arguments are ModelIdMap: { [modelId]: Iterable<number> }.
export function subtractIdMap(allItems, toRemove) {
const result = {};
for (const [modelId, allIds] of Object.entries(allItems)) {
const removeSet = new Set(toRemove[modelId] ?? []);
const remaining = [];
for (const id of allIds) {
if (!removeSet.has(id)) remaining.push(id);
}
if (remaining.length > 0) result[modelId] = remaining;
}
return result;
}
// Returns true if ref looks like a git commit hash (740 hex chars).
export function isCommitHash(ref) {
return /^[0-9a-f]{7,40}$/.test(ref);
}