Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
styling
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkingshott committed Oct 23, 2021
1 parent 80f68de commit 0702dc8
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 465 deletions.
32 changes: 0 additions & 32 deletions .github/workflows/style.yml

This file was deleted.

178 changes: 75 additions & 103 deletions src/iodine.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class Iodine {

this.messages = this._defaultMessages();

this.defaultFieldName = "Value";
this.defaultFieldName = 'Value';
}

/**
Expand All @@ -29,12 +29,12 @@ export class Iodine {

if (!this.isDate(second) && !this.isInteger(second)) return false;

second = typeof second === "number" ? second : second.getTime();
second = typeof second === 'number' ? second : second.getTime();

if (type === "less" && equals) return first.getTime() <= second;
if (type === "less" && !equals) return first.getTime() < second;
if (type === "more" && equals) return first.getTime() >= second;
if (type === "more" && !equals) return first.getTime() > second;
if (type === 'less' && equals) return first.getTime() <= second;
if (type === 'less' && !equals) return first.getTime() < second;
if (type === 'more' && equals) return first.getTime() >= second;
if (type === 'more' && !equals) return first.getTime() > second;
}

/**
Expand All @@ -43,35 +43,35 @@ export class Iodine {
*/
_defaultMessages() {
return {
after: `The date must be after: '[PARAM]'`,
afterOrEqual: `The date must be after or equal to: '[PARAM]'`,
array: `[FIELD] must be an array`,
before: `The date must be before: '[PARAM]'`,
beforeOrEqual: `The date must be before or equal to: '[PARAM]'`,
boolean: `[FIELD] must be true or false`,
date: `[FIELD] must be a date`,
different: `[FIELD] must be different to '[PARAM]'`,
endingWith: `[FIELD] must end with '[PARAM]'`,
email: `[FIELD] must be a valid email address`,
falsy: `[FIELD] must be a falsy value (false, 'false', 0 or '0')`,
in: `[FIELD] must be one of the following options: [PARAM]`,
integer: `[FIELD] must be an integer`,
json: `[FIELD] must be a parsable JSON object string`,
max: `[FIELD] must be less than or equal to [PARAM]`,
min: `[FIELD] must be greater than or equal to [PARAM]`,
maxLength: `[FIELD] must not be greater than '[PARAM]' in character length`,
minLength: `[FIELD] must not be less than '[PARAM]' character length`,
notIn: `[FIELD] must not be one of the following options: [PARAM]`,
numeric: `[FIELD] must be numeric`,
optional: `[FIELD] is optional`,
regexMatch: `[FIELD] must satisify the regular expression: [PARAM]`,
required: `[FIELD] must be present`,
same: `[FIELD] must be '[PARAM]'`,
startingWith: `[FIELD] must start with '[PARAM]'`,
string: `[FIELD] must be a string`,
truthy: `[FIELD] must be a truthy value (true, 'true', 1 or '1')`,
url: `[FIELD] must be a valid url`,
uuid: `[FIELD] must be a valid UUID`,
after : `The date must be after: '[PARAM]'`,
afterOrEqual : `The date must be after or equal to: '[PARAM]'`,
array : `[FIELD] must be an array`,
before : `The date must be before: '[PARAM]'`,
beforeOrEqual : `The date must be before or equal to: '[PARAM]'`,
boolean : `[FIELD] must be true or false`,
date : `[FIELD] must be a date`,
different : `[FIELD] must be different to '[PARAM]'`,
endingWith : `[FIELD] must end with '[PARAM]'`,
email : `[FIELD] must be a valid email address`,
falsy : `[FIELD] must be a falsy value (false, 'false', 0 or '0')`,
in : `[FIELD] must be one of the following options: [PARAM]`,
integer : `[FIELD] must be an integer`,
json : `[FIELD] must be a parsable JSON object string`,
max : `[FIELD] must be less than or equal to [PARAM]`,
min : `[FIELD] must be greater than or equal to [PARAM]`,
maxLength : `[FIELD] must not be greater than '[PARAM]' in character length`,
minLength : `[FIELD] must not be less than '[PARAM]' character length`,
notIn : `[FIELD] must not be one of the following options: [PARAM]`,
numeric : `[FIELD] must be numeric`,
optional : `[FIELD] is optional`,
regexMatch : `[FIELD] must satisify the regular expression: [PARAM]`,
required : `[FIELD] must be present`,
same : `[FIELD] must be '[PARAM]'`,
startingWith : `[FIELD] must start with '[PARAM]'`,
string : `[FIELD] must be a string`,
truthy : `[FIELD] must be a truthy value (true, 'true', 1 or '1')`,
url : `[FIELD] must be a valid url`,
uuid : `[FIELD] must be a valid UUID`,
};
}

Expand All @@ -82,15 +82,10 @@ export class Iodine {
_prepare(value, rules = []) {
if (!rules.length) return [];

if (rules[0] === "optional" && this.isOptional(value)) return [];
if (rules[0] === 'optional' && this.isOptional(value)) return [];

return rules
.filter((rule) => rule !== "optional")
.map((rule) => [
rule,
this._titleCase(rule.split(":").shift()),
rule.split(":").slice(1),
]);
return rules.filter(rule => rule !== 'optional')
.map(rule => [rule, this._titleCase(rule.split(':').shift()), rule.split(':').slice(1)]);
}

/**
Expand All @@ -114,13 +109,8 @@ export class Iodine {
*
*/
async asyncIs(value, rules = []) {
for (let index in (rules = this._prepare(value, rules))) {
if (
!(await this[`is${rules[index][1]}`].apply(this, [
value,
rules[index][2].join(":"),
]))
) {
for (let index in rules = this._prepare(value, rules)) {
if (!await this[`is${rules[index][1]}`].apply(this, [value, rules[index][2].join(':')])) {
return await rules[index][0];
}
}
Expand All @@ -133,57 +123,56 @@ export class Iodine {
*
*/
async asyncIsValid(value, rules = []) {
return (await this.asyncIs(value, rules)) === true;
return await this.asyncIs(value, rules) === true;
}

/**
* Retrieve an error message for the given rule.
*
*/
getErrorMessage(rule, args = undefined) {
let { param, field } =
typeof args === "object" ? args : { param: args, field: undefined };
let { param, field } = typeof args === 'object' ? args : { param: args, field: undefined };

const chunks = rule.split(":");
const chunks = rule.split(':');

let key = chunks.shift();

param = param || chunks.join(":");
param = param || chunks.join(':');

if (["after", "afterOrEqual", "before", "beforeOrEqual"].includes(key)) {
if (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) {
param = new Date(parseInt(param)).toLocaleTimeString(this.locale, {
year: "numeric",
month: "short",
day: "numeric",
hour: "2-digit",
minute: "numeric",
hour12: false,
year : 'numeric',
month : 'short',
day : 'numeric',
hour : '2-digit',
minute : 'numeric',
hour12 : false,
});
}

let message = [null, undefined, ""].includes(param)
let message = [null, undefined, ''].includes(param)
? this.messages[key]
: this.messages[key].replace("[PARAM]", param);
: this.messages[key].replace('[PARAM]', param);

return [null, undefined, ""].includes(field)
? message.replace("[FIELD]", this.defaultFieldName)
: message.replace("[FIELD]", field);
return [null, undefined, ''].includes(field)
? message.replace('[FIELD]', this.defaultFieldName)
: message.replace('[FIELD]', field);
}

/**
* Determine if the given date is after another given date.
*
*/
isAfter(value, after) {
return this._dateCompare(value, after, "more", false);
return this._dateCompare(value, after, 'more', false);
}

/**
* Determine if the given date is after or equal to another given date.
*
*/
isAfterOrEqual(value, after) {
return this._dateCompare(value, after, "more", true);
return this._dateCompare(value, after, 'more', true);
}

/**
Expand All @@ -199,15 +188,15 @@ export class Iodine {
*
*/
isBefore(value, before) {
return this._dateCompare(value, before, "less", false);
return this._dateCompare(value, before, 'less', false);
}

/**
* Determine if the given date is before or equal to another given date.
*
*/
isBeforeOrEqual(value, before) {
return this._dateCompare(value, before, "less", true);
return this._dateCompare(value, before, 'less', true);
}

/**
Expand All @@ -223,11 +212,7 @@ export class Iodine {
*
*/
isDate(value) {
return (
value &&
Object.prototype.toString.call(value) === "[object Date]" &&
!isNaN(value)
);
return (value && Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value));
}

/**
Expand All @@ -251,37 +236,31 @@ export class Iodine {
*
*/
isEmail(value) {
return new RegExp("^\\S+@\\S+[\\.][0-9a-z]+$").test(
String(value).toLowerCase()
);
return new RegExp("^\\S+@\\S+[\\.][0-9a-z]+$").test(String(value).toLowerCase());
}

/**
* Determine if the given value is falsy.
*
*/
isFalsy(value) {
return [0, "0", false, "false"].includes(value);
return [0, '0', false, 'false'].includes(value);
}

/**
* Determine if the given value is within the given array of options.
*
*/
isIn(value, options) {
return (
typeof options === "string" ? options.split(",") : options
).includes(value);
return (typeof options === 'string' ? options.split(',') : options).includes(value);
}

/**
* Determine if the given value is an integer.
*
*/
isInteger(value) {
return (
Number.isInteger(value) && parseInt(value).toString() === value.toString()
);
return Number.isInteger(value) && parseInt(value).toString() === value.toString();
}

/**
Expand All @@ -290,7 +269,7 @@ export class Iodine {
*/
isJson(value) {
try {
return typeof JSON.parse(value) === "object";
return typeof JSON.parse(value) === 'object';
} catch (e) {
return false;
}
Expand All @@ -317,15 +296,15 @@ export class Iodine {
*
*/
isMaxLength(value, limit) {
return typeof value === "string" ? value.length <= limit : false;
return typeof value === 'string' ? value.length <= limit : false;
}

/**
* Determine if the given value string length is greater than or equal to the minimum limit.
*
*/
isMinLength(value, limit) {
return typeof value === "string" ? value.length >= limit : false;
return typeof value === 'string' ? value.length >= limit : false;
}

/**
Expand All @@ -349,7 +328,7 @@ export class Iodine {
*
*/
isOptional(value) {
return [null, undefined, ""].includes(value);
return [null, undefined, ''].includes(value);
}

/**
Expand Down Expand Up @@ -389,24 +368,23 @@ export class Iodine {
*
*/
isString(value) {
return typeof value === "string";
return typeof value === 'string';
}

/**
* Determine if the given value is truthy.
*
*/
isTruthy(value) {
return [1, "1", true, "true"].includes(value);
return [1, '1', true, 'true'].includes(value);
}

/**
* Determine if the given value is a valid URL.
*
*/
isUrl(value) {
let regex =
"^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$";
let regex = "^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$";

return new RegExp(regex).test(String(value).toLowerCase());
}
Expand All @@ -416,8 +394,7 @@ export class Iodine {
*
*/
isUuid(value) {
let regex =
"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$";
let regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$";

return new RegExp(regex).test(String(value).toLowerCase());
}
Expand All @@ -427,13 +404,8 @@ export class Iodine {
*
*/
is(value, rules = []) {
for (let index in (rules = this._prepare(value, rules))) {
if (
!this[`is${rules[index][1]}`].apply(this, [
value,
rules[index][2].join(":"),
])
) {
for (let index in rules = this._prepare(value, rules)) {
if (!this[`is${rules[index][1]}`].apply(this, [value, rules[index][2].join(':')])) {
return rules[index][0];
}
}
Expand Down
Loading

0 comments on commit 0702dc8

Please sign in to comment.