Skip to content

Commit

Permalink
convert the whole codebase so that it uses types ;-)
Browse files Browse the repository at this point in the history
  • Loading branch information
drkameleon committed Sep 12, 2024
1 parent 839821d commit 09b6f1e
Show file tree
Hide file tree
Showing 11 changed files with 566 additions and 558 deletions.
65 changes: 31 additions & 34 deletions src/plugins/base58.art
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,39 @@
; @file: src/plugins/base58.art
;==========================================================

;------------------
; parameters
;------------------
params: #[]

;------------------
; built-in data
;------------------
data: #[
; See:
; https://github.com/validatorjs/validator.js/blob/master/src/lib/isBase58.js
isBase58: {/^[A-HJ-NP-Za-km-z1-9]+$/}
]
define :base58Validator is :validator [

;------------------
; built-in data
;------------------

isBase58: {/^[A-HJ-NP-Za-km-z1-9]+$/} ; See:
; https://github.com/validatorjs/validator.js/blob/master/src/lib/isBase58.js

;------------------
; main action
;------------------
action: function [str, params, data][
match? str data\isBase58
]

;------------------
; tests
;------------------
test: #[
valid: [
"AbCdEfGH"
"123456789"
"16EiHXyy6VtL3z6VSjkmfuqpfAKDyekA93"
"5K6CcnN9FauX6oE3dHgqk6JjpXgDvGP7iySR8jEvfJELTgweikh"
;------------------
; methods
;------------------

action: method [str, opts][
match? str \isBase58
]

invalid: [
""
"*&*#@&^#*&@^#"
"ABCDEFGHJO"
"16EiHXyy 6VtL3z6V Sjkmfuqpf AKDyekA93"
test: method [][
#[
valid: [
"AbCdEfGH"
"123456789"
"16EiHXyy6VtL3z6VSjkmfuqpfAKDyekA93"
"5K6CcnN9FauX6oE3dHgqk6JjpXgDvGP7iySR8jEvfJELTgweikh"
]

invalid: [
""
"*&*#@&^#*&@^#"
"ABCDEFGHJO"
"16EiHXyy 6VtL3z6V Sjkmfuqpf AKDyekA93"
]
]
]
]
]
147 changes: 74 additions & 73 deletions src/plugins/card.art
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
; @file: src/plugins/card.art
;==========================================================

;------------------
; parameters
;------------------
params: #[]

;------------------
; built-in data
;------------------
data: #[
define :cardValidator is :validator [

;------------------
; built-in data
;------------------

; for the regexes, see:
; https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js

Expand All @@ -36,12 +33,11 @@ data: #[
union: {/^(6[27][0-9]{14}|^(81[0-9]{14,17}))$/}
visa: {/^(?:4[0-9]{12})(?:[0-9]{3,6})?$/}
]
]

;------------------
; main action
;------------------
action: function [str, params, data][
;------------------
; helpers
;------------------

luhn?: function [n][
; see:
; https://en.wikipedia.org/wiki/Luhn_algorithm
Expand All @@ -62,69 +58,74 @@ action: function [str, params, data][
return (last ds) = (10 - s % 10) % 10
]

sanitized: join split replace str {/[\s]+/} ""
;------------------
; methods
;------------------

unless some? values data\emitters 'rx [
match? sanitized rx
] -> return false
action: method [str, opts][
sanitized: join split replace str {/[\s]+/} ""

return luhn? to :integer sanitized
]
unless some? values \emitters 'rx [
match? sanitized rx
] -> return false

;------------------
; tests
;------------------
test: #[
valid: [
"378282246310005" ; American Express
"371449635398431"
"378734493671000"
"3787 3449 3671 000"

"30569309025904" ; Diners Club
"38520000023237"
"385 200 000 232 37"

"6011111111111117" ; Discover
"6011000990139424"
"6011 0009 9013 9424"

"3530111333300000" ; JCB
"3566002020360505"
"3566 0020 2036 0505"

"5555555555554444" ; Mastercard
"5105105105105100"
"5105 1051 0510 5100"

"4111111111111111" ; Visa
"4012888888881881"
"4012 8888 8888 1881"
"4222222222222"
return \luhn? to :integer sanitized
]

invalid: [
""
"&*@^#*&@^#"
"3782822463100051"
"371449635398432"
"378734493671005"
"3787 3449 3671 900"
"30569309025903"
"38520000023231"
"385 200 000 202 37"
"60111111111111X7"
"6011000990139424Y"
"6011 0009 9013 9324"
"3530111333300880"
"3566002020360555"
"3566 0020 2036 2505"
"5555555555554447"
"5105105105105108"
"5105 1051 0510 9100"
"41111111111111110"
"40128888888818810"
"4012 8888 8888 18081"
"42222222222220"
test: method [][
#[
valid: [
"378282246310005" ; American Express
"371449635398431"
"378734493671000"
"3787 3449 3671 000"

"30569309025904" ; Diners Club
"38520000023237"
"385 200 000 232 37"

"6011111111111117" ; Discover
"6011000990139424"
"6011 0009 9013 9424"

"3530111333300000" ; JCB
"3566002020360505"
"3566 0020 2036 0505"

"5555555555554444" ; Mastercard
"5105105105105100"
"5105 1051 0510 5100"

"4111111111111111" ; Visa
"4012888888881881"
"4012 8888 8888 1881"
"4222222222222"
]

invalid: [
""
"&*@^#*&@^#"
"3782822463100051"
"371449635398432"
"378734493671005"
"3787 3449 3671 900"
"30569309025903"
"38520000023231"
"385 200 000 202 37"
"60111111111111X7"
"6011000990139424Y"
"6011 0009 9013 9324"
"3530111333300880"
"3566002020360555"
"3566 0020 2036 2505"
"5555555555554447"
"5105105105105108"
"5105 1051 0510 9100"
"41111111111111110"
"40128888888818810"
"4012 8888 8888 18081"
"42222222222220"
]
]
]
]
75 changes: 36 additions & 39 deletions src/plugins/email.art
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,49 @@
; @file: src/plugins/email.art
;==========================================================

;------------------
; parameters
;------------------
params: #[]
define :emailValidator is :validator [

;------------------
; built-in data
;------------------

;------------------
; built-in data
;------------------
data: #[
; For the regex, see:
; https://stackoverflow.com/a/201378
isEmail: {/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/}
]

;------------------
; main action
;------------------
action: function [str, params, data][
match? str data\isEmail
]
;------------------
; methods
;------------------

;------------------
; tests
;------------------
test: #[
valid: [
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
action: method [str, opts][
match? str \isEmail
]

invalid: [
"drkameleongmail.com"
"foobar"
"user@"
"@domain.com"
"user#domain.com"
"[email protected]"
"test.sth.com"
"test(sth\"very)weird]com"
"test\"quotes\"instring.com"
test: method [][
#[
valid: [
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
"[email protected]"
]

invalid: [
"drkameleongmail.com"
"foobar"
"user@"
"@domain.com"
"user#domain.com"
"[email protected]"
"test.sth.com"
"test(sth\"very)weird]com"
"test\"quotes\"instring.com"
]
]
]
]
Loading

0 comments on commit 09b6f1e

Please sign in to comment.