Created
March 11, 2020 04:24
-
-
Save hscstudio/8528665e479e8c3cd27ab6d95f3cd2f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Check { | |
constructor(value) { | |
this.value = value | |
this.result = false | |
} | |
match(pattern) { | |
const patterns = { | |
alpha: /^[a-z\s]+$/i, | |
alnum: /^[a-z0-9\s]+$/i, | |
num: /^[0-9]+$/, | |
phone: /^[+]?[(]?[0-9]{3,4}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{1,8}$/im, | |
email: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ | |
} | |
this.result = types.hasOwnProperty(pattern) | |
? patterns[pattern].test(this.value) | |
: pattern.test(this.value) | |
return this | |
} | |
is(type) { | |
const types = { | |
string: 'string', | |
number: 'number', | |
boolean: 'boolean', | |
object: 'object', | |
array: 'object' | |
} | |
if (types.hasOwnProperty(type)) { | |
this.result = typeof this.value === types[type] | |
if (type === 'array') { | |
this.result = Array.isArray(this.value) | |
} | |
} | |
return this | |
} | |
in(list) { | |
if (this.isArray(list)) { | |
this.result = list.includes(this.value) | |
} else if (typeof list === 'object') { | |
this.result = list.hasOwnProperty(this.value) | |
} | |
return this | |
} | |
equal(value) { | |
if (typeof this.value === 'number') { | |
this.result = this.value === value | |
} else if (typeof this.value === 'string') { | |
this.result = this.value.localeCompare(value) === 0 | |
} | |
return result | |
} | |
lower(value) { | |
if (typeof this.value === 'number') { | |
this.result = this.value < value | |
} else if (typeof this.value === 'string') { | |
this.result = this.value.length < value | |
} | |
return this | |
} | |
greater(value) { | |
if (typeof this.value === 'number') { | |
this.result = this.value > value | |
} else if (typeof this.value === 'string') { | |
this.result = this.value.length > value | |
} | |
return this | |
} | |
} | |
/* cara pakai */ | |
console.log((new Check(10)).lower(5).result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment