UNPKG

human-filetypes

Version:
57 lines 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getExtensions = exports.fromExtension = void 0; const data_1 = require("./data"); const fromExtension = (input) => { var _a; if (!input) return data_1.FileKind.Unknown; const extension = (_a = `.${input === null || input === void 0 ? void 0 : input.replace(/^\./, '')}`) === null || _a === void 0 ? void 0 : _a.toLowerCase(); const match = Object.entries(data_1.mimeData).find(([, data]) => { var _a; return (_a = data.extensions) === null || _a === void 0 ? void 0 : _a.find((ext) => extension.endsWith(ext)); }); if (match) { const [, mimeData] = match; return mimeData.kind; } return data_1.FileKind.Unknown; }; exports.fromExtension = fromExtension; const getExtensions = (fileType) => { if (!fileType) return []; const fileKind = `${fileType}`.toLowerCase().trim(); // human readable file Kinds image/ video/ audio/ font/ switch (fileKind) { case 'image': return getAllowedExtensions(data_1.FileKind.Image); case 'video': return getAllowedExtensions(data_1.FileKind.Video); case 'audio': return getAllowedExtensions(data_1.FileKind.Audio); case 'font': return getAllowedExtensions(data_1.FileKind.Font); case 'archive': return getAllowedExtensions(data_1.FileKind.Archive); case 'presentation': return getAllowedExtensions(data_1.FileKind.Presentation); case 'text': return getAllowedExtensions(data_1.FileKind.Text); case 'spreadsheet': return getAllowedExtensions(data_1.FileKind.Spreadsheet); case 'document': return getAllowedExtensions(data_1.FileKind.Document); case 'application': return getAllowedExtensions(data_1.FileKind.Application); } return []; }; exports.getExtensions = getExtensions; const getAllowedExtensions = (fileKind) => { return Object.values(data_1.mimeData).flatMap((mimeData) => { var _a; if (mimeData.kind === fileKind) { return (_a = mimeData.extensions) !== null && _a !== void 0 ? _a : []; } return []; }); }; //# sourceMappingURL=ext.js.map