Skip to content

Commit

Permalink
Added Typescript extra info
Browse files Browse the repository at this point in the history
  • Loading branch information
normancarcamo committed Feb 6, 2020
1 parent f4e0a17 commit 2aedb04
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# is
A data type checker and validator.
A data type checker and validator for node (typescript compatible).

- [Installation](#installation)
- [Test](#test)
Expand Down
62 changes: 31 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type tBetween = number | string | Date | any;

class Is {
object(value: any): boolean {
object(value?: any): boolean {
if (Object.prototype.toString.call(value) === '[object Object]') {
return true;
} else {
Expand All @@ -17,15 +17,15 @@ class Is {
}
}

null(value: any): boolean {
null(value?: any): boolean {
return value === null && typeof value === 'object';
}

undefined(value: any): boolean {
undefined(value?: any): boolean {
return value === undefined && typeof value === 'undefined';
}

number(value: any, parse?: boolean): boolean {
number(value?: any, parse?: boolean): boolean {
if (parse) {
return (
typeof value === 'number' ||
Expand All @@ -36,22 +36,22 @@ class Is {
}
}

array(value: any): boolean {
array(value?: any): boolean {
const prototype: string = Object.prototype.toString.call(value);
return prototype === '[object Array]' || Array.isArray(value);
}

string(value: any): boolean {
string(value?: any): boolean {
return typeof value === 'string';
}

boolean(value: any, parse?: boolean): boolean {
boolean(value?: any, parse?: boolean): boolean {
return (
typeof value === 'boolean' || !!(parse && /^(false|true)/.test(value))
);
}

uuid(value: any, version?: string): boolean {
uuid(value?: any, version?: string): boolean {
const versions = [
/^[0-9A-F]{8}-[0-9A-F]{4}-[1][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, // v1
/^[0-9A-F]{8}-[0-9A-F]{4}-[2][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i, // v2
Expand Down Expand Up @@ -87,42 +87,42 @@ class Is {
return false;
}

integer(value: any): boolean {
integer(value?: any): boolean {
return /^([0-9]|\-[0-9])+$/g.test(value as string);
}

float(value: any): boolean {
float(value?: any): boolean {
return /(^[0-9]+|^\-[0-9]+)?\.+[0-9]+/.test(value as string);
}

positive(value: any, parse?: boolean): boolean {
positive(value?: any, parse?: boolean): boolean {
return this.number(value, parse) && value > 0;
}

negative(value: any, parse?: boolean): boolean {
negative(value?: any, parse?: boolean): boolean {
return this.number(value, parse) && value < 0;
}

zero(value: any, parse?: boolean): boolean {
zero(value?: any, parse?: boolean): boolean {
return this.number(value, parse) && Number(value) === 0;
}

alpha(value: any): boolean {
alpha(value?: any): boolean {
if (!!value && typeof value === 'string') {
return /^([a-z]|[0-9])+([a-z]|[0-9])?$/gim.test(value);
}
return false;
}

digit(value: any): boolean {
digit(value?: any): boolean {
return /^\d{1,1}$/.test(value);
}

function(value: any): boolean {
function(value?: any): boolean {
return Object.prototype.toString.call(value) === '[object Function]';
}

asyncFunction(value: any): boolean {
asyncFunction(value?: any): boolean {
if (value && !(value instanceof Promise)) {
if (value.constructor && value.constructor.name === 'AsyncFunction') {
return true;
Expand All @@ -131,15 +131,15 @@ class Is {
return false;
}

promise(value: any): boolean {
promise(value?: any): boolean {
if (value && value instanceof Promise) {
return true;
} else {
return false;
}
}

empty(value: any): boolean {
empty(value?: any): boolean {
if (this.undefined(value) || !arguments.length) {
return false;
}
Expand All @@ -165,14 +165,14 @@ class Is {
}
}

regexp(value: any): boolean {
regexp(value?: any): boolean {
return (
value instanceof RegExp ||
Object.prototype.toString.call(value) === '[object RegExp]'
);
}

ip(value: any): boolean {
ip(value?: any): boolean {
if (this.string(value)) {
const parts = value.split('.').map((x: number) => x >= 0 && x <= 255);
if (parts.length === 4) {
Expand All @@ -182,14 +182,14 @@ class Is {
return false;
}

port(value: any): boolean {
port(value?: any): boolean {
return !!value && typeof value === 'number' && value > 0 && value <= 65535;
}

between(
min: tBetween,
max: tBetween,
val: tBetween,
min?: tBetween,
max?: tBetween,
val?: tBetween,
parse?: boolean,
): boolean {
if (
Expand All @@ -211,21 +211,21 @@ class Is {
return false;
}

date(value: any): boolean {
date(value?: any): boolean {
const y: string = /([1-1][9-9][7-9][0-9]|[2-2][0-9][0-9][0-9])/.source;
const m: string = /([0-0][1-9]|[1-1][0-2])/.source;
const d: string = /([0-0][1-9]|[1-1][0-9]|[2-2][0-9]|[3-3][0-1])/.source;
return new RegExp(`^${y}(\/|\-)${m}(\/|\-)${d}$`).test(value);
}

time(value: any): boolean {
time(value?: any): boolean {
const hh: string = /([0-1][0-9]|[2-2][0-3])/.source;
const mm: string = /([0-5][0-9])/.source;
const ss: string = /([0-5][0-9])/.source;
return new RegExp(`^${hh}\:${mm}(\:${ss})?$`).test(value);
}

dateTime(value: any): boolean {
dateTime(value?: any): boolean {
const y: string = /([1-1][9-9][7-9][0-9]|[2-2][0-9][0-9][0-9])/.source;
const m: string = /([0-0][1-9]|[1-1][0-2])/.source;
const d: string = /([0-0][1-9]|[1-1][0-9]|[2-2][0-9]|[3-3][0-1])/.source;
Expand All @@ -239,7 +239,7 @@ class Is {
return new RegExp(`^${date}(\ |\T)${time}$`).test(value);
}

dateISO(value: any): boolean {
dateISO(value?: any): boolean {
const y: string = /([1-1][9-9][7-9][0-9]|[2-2][0-9][0-9][0-9])/.source;
const m: string = /([0-0][1-9]|[1-1][0-2])/.source;
const d: string = /([0-0][1-9]|[1-1][0-9]|[2-2][0-9]|[3-3][0-1])/.source;
Expand All @@ -255,13 +255,13 @@ class Is {
return new RegExp(`^${date}(\ |\T)${time}$`).test(value);
}

dateInstance(value: any): boolean {
dateInstance(value?: any): boolean {
const prototype: string = Object.prototype.toString.call(value);
const res: boolean = prototype === '[object Date]' && !isNaN(value);
return (value && res) || value instanceof Date;
}

dateValid(value: any): boolean {
dateValid(value?: any): boolean {
if (!!value && typeof value === 'string') {
return !isNaN(new Date(value).getDate());
} else {
Expand Down

0 comments on commit 2aedb04

Please sign in to comment.