Skip to content

Instantly share code, notes, and snippets.

@SirMoustache
Last active May 3, 2019 08:36
Show Gist options
  • Select an option

  • Save SirMoustache/b91fd50682a15e53783984a4f919ff34 to your computer and use it in GitHub Desktop.

Select an option

Save SirMoustache/b91fd50682a15e53783984a4f919ff34 to your computer and use it in GitHub Desktop.
Utils functions
import accepts from 'attr-accept';
/**
* @param {Number} maxSize Maximum File size (in bytes)
* @param {Number} minSize Minimum File size (in bytes)
* @param {File} file File Object
*/
export const fileMatchSize = (maxSize, minSize, file) =>
file.size <= maxSize && file.size >= minSize;
/**
* @param {Number} maxSize Maximum File size (in bytes)
* @param {Number} minSize Minimum File size (in bytes)
* @param {Array<File>} files List of Files
*/
export const allFilesMatchSize = (maxSize, minSize, files) =>
files.every(file => fileMatchSize(maxSize, minSize, file));
/**
* @param {Number} maxLength Maximum File name length
* @param {Number} minLength Minimum File name length
* @param {File} file File Object
*/
export const fileMatchNameLength = (maxLength, minLength, file) =>
file.name.length <= maxLength && file.name.length >= minLength;
/**
* @param {Number} maxLength Maximum File name length
* @param {Number} minLength Minimum File name length
* @param {Array<File>} files List of Files
*/
export const allFilesMatchNameLength = (maxLength, minLength, files) =>
files.every(file => fileMatchNameLength(maxLength, minLength, file));
/**
* @param {String | Array<String>} accept Mime types or extensions
* @param {File} file File Object
*/
export const fileAccepted = (accept, file) => accepts(file, accept);
/**
* @param {String | Array<String>} accept Mime types or extensions
* @param {Array<File>} files List of Files
*/
export const allFilesAccepted = (accept, files) =>
files.every(file => fileAccepted(accept, file));
import { fileMatchSize, fileMatchNameLength } from '../fileUtils';
describe('fileMatchSize', () => {
it('should return true if File size is in range of maximum and minimum sizes', () => {
const file = {
size: 50000000,
};
const maxFileSize = 500000000;
const minFileSize = 0;
expect(fileMatchSize(maxFileSize, minFileSize, file)).toBe(true);
});
it('should return false if File size is exceed maximum size', () => {
const file = {
size: 50000000,
};
const maxFileSize = 5000000;
const minFileSize = 0;
expect(fileMatchSize(maxFileSize, minFileSize, file)).toBe(false);
});
it('should return false if File size is fail to reach minimum size', () => {
const file = {
size: 500000,
};
const maxFileSize = 500000000;
const minFileSize = 5000000;
expect(fileMatchSize(maxFileSize, minFileSize, file)).toBe(false);
});
});
describe('fileMatchNameLength', () => {
it('should return true if File name length is in range of maximum and minimum values', () => {
const file = {
name: 'test.stl',
};
const maxLength = 10;
const minLength = 0;
expect(fileMatchNameLength(maxLength, minLength, file)).toBe(true);
});
it('should return false if File name length is exceed maximum value', () => {
const file = {
name: 'test.stl',
};
const maxFileSize = 5;
const minFileSize = 0;
expect(fileMatchNameLength(maxFileSize, minFileSize, file)).toBe(false);
});
it('should return false if File name length is fail to reach minimum value', () => {
const file = {
name: 'test',
};
const maxFileSize = 10;
const minFileSize = 5;
expect(fileMatchNameLength(maxFileSize, minFileSize, file)).toBe(false);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment