ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
39 lines • 2.48 kB
JavaScript
import { isMatch, isValid, parseISO } from 'date-fns';
export const isNumeric = (value) => !isNaN(parseFloat(value)) && isFinite(value);
export const valuesAreNumeric = (values) => values.every(isNumeric);
export const isInteger = (value) => Number.isInteger(value) || !isNaN(parseInt(value));
export const valuesAreInteger = (values) => values.every(isInteger);
export const isBoolean = (value) => typeof value === 'boolean';
export const valuesAreBoolean = (values) => values.every(isBoolean);
export const isBooleanString = (value) => ['true', 'false'].includes(value.toString().toLowerCase());
export const valuesAreBooleanString = (values) => values.every(isBooleanString);
export const isString = (value) => typeof value === 'string';
export const valuesAreString = (values) => values.every(isString);
const HtmlRegexp = /<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)<\/\1>/i;
export const isHtml = (value) => !value || HtmlRegexp.test(value);
export const valuesAreHtml = (values) => values.every(isHtml);
const UrlRegexp = /http(s*):\/\/.*/i;
export const isUrl = (value) => !value || UrlRegexp.test(value);
export const valuesAreUrl = (values) => values.every(isUrl);
const ImageUrlRegexp = /^http(s*):\/\/.*\.(jpeg|jpg|jfif|pjpeg|pjp|png|svg|gif|webp|apng|bmp|ico|cur|tif|tiff)/i;
export const isImageUrl = (value) => !value || ImageUrlRegexp.test(value);
export const valuesAreImageUrl = (values) => values.every(isImageUrl);
// This is a very simple regex to find emails
// It is NOT meant to validate emails as the spec is way more complicated but is
// enough for our inference needs
const EmailRegexp = /@{1}/;
export const isEmail = (value) => !value || EmailRegexp.test(value);
export const valuesAreEmail = (values) => values.every(isEmail);
export const isArray = (value) => Array.isArray(value);
export const valuesAreArray = (values) => values.every(isArray);
export const isDate = (value) => !value || value instanceof Date;
export const valuesAreDate = (values) => values.every(isDate);
export const isDateString = (value) => !value ||
(typeof value === 'string' &&
(isMatch(value, 'MM/dd/yyyy') ||
isMatch(value, 'MM/dd/yy') ||
isValid(parseISO(value))));
export const valuesAreDateString = (values) => values.every(isDateString);
export const isObject = (value) => Object.prototype.toString.call(value) === '[object Object]';
export const valuesAreObject = (values) => values.every(isObject);
//# sourceMappingURL=assertions.js.map