UNPKG

filepond

Version:

FilePond, where files go to stretch their bits.

144 lines (143 loc) • 3.9 kB
/*! * FilePond v5.0.2 * Copyright (c) 2017-2026 Pqina B.V. * Released under the MIT License * https://filepond.com */ import { isString as l, isNumber as d, isBlob as m } from "./test.js"; import { numberToFloat as g } from "./number.js"; const f = { B: 0, // B K: 1, // KB M: 2, // MB G: 3, // GB T: 4, // TB P: 5, // PB E: 6, // EB Z: 7, // ZB Y: 8 // YB }, y = Object.keys(f), F = ["name", "size", "type", "lastModified"]; function b(t, e) { F.forEach((n) => { e[n] = t[n]; }); } function v(t, e) { return m(t) ? new Blob([t], { type: e }) : new File([t], t.name, { type: e, lastModified: t.lastModified }); } function A(t, e) { return new File([t], e, { type: t.type, lastModified: t.lastModified }); } function I(t) { return t.trim().replace(/[<>:;,"/\\|?*\x00-\x1F]/gi, ""); } function S(t) { return l(t) ? /(?:\.([^.]+))?$/.exec(t)?.[0] : void 0; } function P(t) { return l(t) ? t.replace(/\.[^/.]+$/, "") : void 0; } const M = { plain: "txt" }; function z(t, e = {}) { if (l(t) && t.length) { const n = (t.match(/\/(?:x-)?([0-9a-z]+)(?:-compressed)?/i) || [])[1]; if (n) return `.${{ ...M, ...e }[n] || n}`; } } function B(t) { return new Blob([t], { type: t.type }); } function _(t) { return new File([t], t.name, { type: t.type, lastModified: t.lastModified }); } function G(t, e) { return new File([t], t.name, { type: e.type ? e.type : t.type, lastModified: e.lastModified ? e.lastModified : t.lastModified }); } function $(t, e, n) { const { type: o = t.type, lastModified: i = (/* @__PURE__ */ new Date()).getTime() } = n ?? {}; return new File([t], e, { type: o, lastModified: i }); } function N(t, e) { const { locale: n = void 0 } = {}; if (d(t)) return t; const o = (t.match(/[\d.,]+/) || [])[0]; if (!o) throw new Error(`naturalFileSizeToBytes: Invalid natural file size ${t}`); const i = g(o, n), a = t.replace(o, "").trim(), r = a[1] === "i" ? 1024 : 1e3, c = f[a[0]]; return i * Math.pow(r, c); } function U(t, e) { const { locale: n = void 0, byteUnits: o = "mega", ...i } = e || {}, a = o === "mega", r = a ? 1e3 : 1024, c = t === 0 ? 0 : Math.floor(Math.log(t) / Math.log(r)); return `${new Intl.NumberFormat(n, { style: "decimal", maximumFractionDigits: 0, ...i }).format(t / Math.pow(r, c))} ${y[c] + (a ? "" : "i") + (c > 0 ? "B" : "")}`; } function j(t) { if (!(d(t) || !t)) return /i/.test(t) ? "mebi" : "mega"; } const h = (t, e) => e.every((n, o) => t[o] === n), w = (t, e) => (n) => h(n, e) && t, u = "application/", s = "image/"; [ [s + "jpeg", [255, 216, 255]], [s + "png", [137, 80, 78, 71]], [s + "gif", [71, 73, 70]], [s + "tiff", [73, 73, 42, 0]], [s + "psd", [56, 66, 80, 83]], [s + "bmp", [66, 77]], [u + "pdf", [37, 80, 68, 70]], [u + "zip", [80, 75, 4, 4]], [u + "ogg", [79, 103, 103, 83]], [u + "x-rar-compressed", [82, 97, 114, 33, 26, 7]] ].map((t) => w(...t)); async function p(t, e = 64) { if (e <= 0) throw new Error("getApproximateBlobHash: hashSize needs to be a positive non zero integer"); const n = Math.round(t.size * 0.5), o = Math.min(e, t.size), i = Math.floor(o / 2), a = Math.ceil(o / 2), r = t.slice(n - i, n + a); return new Uint8Array(await r.arrayBuffer()).join(""); } async function D(t, e, n) { if (!t || !e) return !1; const o = await p(t, n), i = await p(e, n); return o === i; } export { $ as blobToFile, U as bytesToNaturalFileSize, B as cloneBlob, _ as cloneFile, G as cloneFileWithOptions, b as copyFilePropsToObject, D as filesAreProbablyEqual, p as getApproximateBlobHash, S as getExtensionFromFilename, z as getExtensionFromMimeType, P as getFilenameWithoutExtension, j as getFormatFromFileSize, N as naturalFileSizeToBytes, I as sanitizeFilename, v as updateFileType, A as updateFilename };