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
// SPDX-License-Identifier: 0BSD
import { extractHeaderComments } from "https://esm.town/v/vladimyr/extractHeaderComments";
import type { ConjunctionInfo, Info, LicenseInfo } from "npm:spdx-expression-parse";
import parseLicenseExpression from "npm:spdx-expression-parse";
const isConjuction = (info: Info): info is ConjunctionInfo => "conjuction" in info;
export function parseLicenseInfo(code: string) {
const comments = extractHeaderComments(code);
const pragma = comments
.flatMap(comment => comment.split(/\r?\n/))
.filter(line => line.includes("SPDX-License-Identifier:"))
.at(-1);
const [_, expr] = pragma.split("SPDX-License-Identifier:");
return parseLicenseExpression(expr);
}
export function formatLicenseInfo(info: Info, level = 0) {
if (!isConjuction(info)) {
let output = info.license;
if (info.plus) output = `${output}+`;
return output;
}
let output = [
formatLicenseInfo(info.left, level + 1),
info.conjunction.toUpperCase(),
formatLicenseInfo(info.right, level + 1),
].join(" ");
if (level > 0) output = `(${output})`;
return output;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
March 6, 2024