UNPKG

alinea

Version:
58 lines (56 loc) 1.49 kB
import "../../chunks/chunk-NZLE2WMY.js"; // src/core/source/Utils.ts import { crypto } from "@alinea/iso"; async function sha1Bytes(data) { const hashBuffer = await crypto.subtle.digest("SHA-1", data); return new Uint8Array(hashBuffer); } async function sha1Hash(data) { return bytesToHex(await sha1Bytes(data)); } function concatUint8Arrays(arrays) { const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0); const result = new Uint8Array(totalLength); let offset = 0; for (const arr of arrays) { result.set(arr, offset); offset += arr.length; } return result; } function hexToBytes(hex) { if (hex.length % 2 !== 0) throw new Error("Invalid hex string"); const bytes = new Uint8Array(hex.length / 2); for (let i = 0; i < hex.length; i += 2) { bytes[i / 2] = Number.parseInt(hex.slice(i, i + 2), 16); } return bytes; } var hexes = Array.from( { length: 256 }, (_, i) => i.toString(16).padStart(2, "0") ); function bytesToHex(bytes) { let hex = ""; for (let i = 0; i < bytes.length; i++) hex += hexes[bytes[i]]; return hex; } function compareStrings(a, b) { return -(a < b) || +(a > b); } function splitPath(path) { const firstSlash = path.indexOf("/"); if (firstSlash === -1) return [path, void 0]; const name = path.slice(0, firstSlash); const rest = path.slice(firstSlash + 1); return [name, rest]; } export { bytesToHex, compareStrings, concatUint8Arrays, hexToBytes, sha1Bytes, sha1Hash, splitPath };