UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.

103 lines (102 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidFormData = isValidFormData; exports.isOriginFileObj = isOriginFileObj; exports.isCustomFile = isCustomFile; exports.isCustomFileArray = isCustomFileArray; exports.isFileArray = isFileArray; exports.isFileList = isFileList; exports.isFileOrBlob = isFileOrBlob; exports.isFileUpload = isFileUpload; /** * * Checks if a given value is a valid `FormData` & it's not empty. * - **N.B.** Be cautious when using this in SSR (Server-Side Rendering) environments (such as `Next.js` Server Components), as it may not work as expected. * @param value - The value to check. * @returns `true` if the value is a valid `FormData` and not empty, otherwise `false`. */ function isValidFormData(value) { if (!(value instanceof FormData)) return false; if ('entries' in value) { if (typeof value.entries !== 'function') { console.warn('`FormData.entries()` is not supported!'); return false; } return Array.from(value.entries())?.length > 0; } return false; } /** * * Checks if a given value is an `OriginFileObj`. * - **N.B.** Be cautious when using this in SSR (Server-Side Rendering) environments (such as `Next.js` Server Components), as it may not work as expected. * @param value - The value to check. * @returns `true` if the value is a valid `OriginFileObj`, otherwise `false`. */ function isOriginFileObj(value) { if (typeof value !== 'object' || value === null || Array.isArray(value)) { return false; } const obj = value; return typeof obj.uid === 'string'; } /** * * Checks if a given value is a `CustomFile`. * - **N.B.** Be cautious when using this in SSR (Server-Side Rendering) environments (such as `Next.js` Server Components), as it may not work as expected. * @param value - The value to check. * @returns `true` if the value is a valid `CustomFile`, otherwise `false`. */ function isCustomFile(value) { if (typeof value !== 'object' || value === null || Array.isArray(value)) { return false; } const obj = value; return 'originFileObj' in obj && isOriginFileObj(obj.originFileObj); } /** * * Checks if a given value is an array of `CustomFile` objects. * - **N.B.** Be cautious when using this in SSR (Server-Side Rendering) environments (such as `Next.js` Server Components), as it may not work as expected. * @param value - The value to check. * @returns `true` if the value is a valid `CustomFile[]`, otherwise `false`. */ function isCustomFileArray(value) { return (Array.isArray(value) && value?.length > 0 && value?.every(isCustomFile)); } /** * * Checks if a given value is an array of `File/Blob` objects. * - **N.B.** Be cautious when using this in SSR (Server-Side Rendering) environments (such as `Next.js` Server Components), as it may not work as expected. * @param value - The value to check. * @returns `true` if the value is a valid `File[]` or `Blob[]`, otherwise `false`. */ function isFileArray(value) { return (Array.isArray(value) && value?.length > 0 && value?.every(isFileOrBlob)); } /** * * Checks if a given value is an instance of `FileList`. * @param value - The value to check. * @returns `true` if the value is a valid `FileList`, otherwise `false`. */ function isFileList(value) { return typeof FileList !== 'undefined' && value instanceof FileList; } /** * * Checks if a given value is an instance of `File` or `Blob`. * @param value - The value to check. * @returns `true` if the value is an instance of `File` or `Blob`, otherwise `false`. */ function isFileOrBlob(value) { return ((typeof File !== 'undefined' && value instanceof File) || (typeof Blob !== 'undefined' && value instanceof Blob)); } /** * * Checks if a given value is a `FileUpload` object. * @param value - The value to check. * @returns `true` if the value is a valid `FileUpload`, otherwise `false`. */ function isFileUpload(value) { if (typeof value !== 'object' || value === null || Array.isArray(value)) { return false; } const obj = value; return (('file' in obj && isCustomFile(obj.file)) || ('fileList' in obj && isCustomFileArray(obj.fileList))); }