stackpress
Version:
Incept is a content management framework.
234 lines (233 loc) • 6.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.oneof = exports.pattern = void 0;
exports.safeValue = safeValue;
exports.required = required;
exports.notempty = notempty;
exports.eq = eq;
exports.ne = ne;
exports.option = option;
exports.starting = starting;
exports.ending = ending;
exports.regex = regex;
exports.date = date;
exports.future = future;
exports.past = past;
exports.present = present;
exports.gt = gt;
exports.ge = ge;
exports.lt = lt;
exports.le = le;
exports.ceq = ceq;
exports.cgt = cgt;
exports.cge = cge;
exports.clt = clt;
exports.cle = cle;
exports.weq = weq;
exports.wgt = wgt;
exports.wge = wge;
exports.wlt = wlt;
exports.wle = wle;
exports.cc = cc;
exports.color = color;
exports.email = email;
exports.hex = hex;
exports.price = price;
exports.url = url;
exports.boolean = boolean;
exports.string = string;
exports.number = number;
exports.float = float;
exports.integer = integer;
exports.object = object;
exports.array = array;
function safeValue(value) {
return typeof value !== 'undefined' ? value : '';
}
;
function required(value) {
return value !== null && typeof value !== 'undefined';
}
;
function notempty(value) {
if (Array.isArray(value)) {
return value.length > 0;
}
else if (typeof value === 'object') {
return Object.keys(value).length > 0;
}
else if (typeof value === 'number') {
return value !== 0;
}
return safeValue(value).toString().length > 0;
}
;
function eq(value, compare) {
return value == compare;
}
;
function ne(value, compare) {
return value != compare;
}
;
function option(value, options) {
return options.includes(value);
}
;
function starting(value, start) {
return safeValue(value).toString().startsWith(start);
}
;
function ending(value, start) {
return safeValue(value).toString().endsWith(start);
}
;
function regex(value, regex) {
return new RegExp(regex).test(safeValue(value).toString());
}
;
function date(value) {
if (value instanceof Date) {
return true;
}
return new Date(value).toString() !== 'Invalid Date';
}
;
function future(value) {
return assert.date(value) && new Date(value || 0) > new Date();
}
;
function past(value) {
return assert.date(value) && new Date(value || 0) < new Date();
}
;
function present(value) {
return assert.date(value)
&& new Date(value || 0).toDateString() === new Date().toDateString();
}
;
function gt(value, compare) {
return (Number(value) || 0) > compare;
}
;
function ge(value, compare) {
return (Number(value) || 0) >= compare;
}
;
function lt(value, compare) {
return (Number(value) || 0) < compare;
}
;
function le(value, compare) {
return (Number(value) || 0) <= compare;
}
;
function ceq(value, compare) {
return eq(safeValue(value).toString().length, compare);
}
;
function cgt(value, compare) {
return gt(safeValue(value).toString().length, compare);
}
;
function cge(value, compare) {
return ge(safeValue(value).toString().length, compare);
}
;
function clt(value, compare) {
return lt(safeValue(value).toString().length, compare);
}
;
function cle(value, compare) {
return le(safeValue(value).toString().length, compare);
}
;
function weq(value, compare) {
return eq(safeValue(value).toString().split(' ').length, compare);
}
;
function wgt(value, compare) {
return gt(safeValue(value).toString().split(' ').length, compare);
}
;
function wge(value, compare) {
return ge(safeValue(value).toString().split(' ').length, compare);
}
;
function wlt(value, compare) {
return lt(safeValue(value).toString().split(' ').length, compare);
}
;
function wle(value, compare) {
return le(safeValue(value).toString().split(' ').length, compare);
}
;
function cc(value) {
return regex(value, /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/);
}
;
function color(value) {
return regex(value, /^#?([a-f0-9]{6}|[a-f0-9]{3})$/);
}
;
function email(value) {
return regex(value, /^(?:(?:(?:[^@,"\[\]\x5c\x00-\x20\x7f-\xff\.]|\x5c(?=[@,"\[\]\x5c\x00-\x20\x7f-\xff]))(?:[^@,"\[\]\x5c\x00-\x20\x7f-\xff\.]|(?<=\x5c)[@,"\[\]\x5c\x00-\x20\x7f-\xff]|\x5c(?=[@,"\[\]\x5c\x00-\x20\x7f-\xff])|\.(?=[^\.])){1,62}(?:[^@,"\[\]\x5c\x00-\x20\x7f-\xff\.]|(?<=\x5c)[@,"\[\]\x5c\x00-\x20\x7f-\xff])|[^@,"\[\]\x5c\x00-\x20\x7f-\xff\.]{1,2})|"(?:[^"]|(?<=\x5c)"){1,62}")@(?:(?!.{64})(?:[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.?|[a-zA-Z0-9]\.?)+\.(?:xn--[a-zA-Z0-9]+|[a-zA-Z]{2,6})|\[(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])(?:\.(?:[0-1]?\d?\d|2[0-4]\d|25[0-5])){3}\])$/);
}
;
function hex(value) {
return regex(value, /^[a-f0-9]+$/);
}
;
function price(value) {
return regex(value.toString(), /^(\d*(\.\d{2}){0, 1})|(\d+)$/);
}
;
function url(value) {
return regex(value, /^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?\/?/i);
}
;
function boolean(value) {
return typeof value === 'boolean';
}
;
function string(value) {
return typeof value === 'string';
}
;
function number(value) {
return typeof value === 'number' || regex(value.toString(), /^\d+(\.\d+)*$/);
}
;
function float(value) {
return typeof value === 'number' || regex(value.toString(), /^\d+\.\d+$/);
}
;
function integer(value) {
return typeof value === 'number' || regex(value.toString(), /^\d+$/);
}
;
function object(value) {
var _a;
return value !== null
&& !Array.isArray(value)
&& ((_a = value === null || value === void 0 ? void 0 : value.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Object';
}
;
function array(values, validator, ...args) {
return Array.isArray(values) && values.every(value => assert[validator].apply(assert, [value, ...args]));
}
exports.pattern = regex;
exports.oneof = option;
const assert = {
required, notempty, eq, ne,
option, regex, date, future,
past, present, gt, ge,
lt, le, ceq, cgt,
cge, clt, cle, weq,
wgt, wge, wlt, wle,
cc, color, email, hex,
price, url, boolean, string,
number, float, integer, object,
array, pattern: exports.pattern, oneof: exports.oneof
};
exports.default = assert;