Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <georgelemon@protonmail.com>
  • Loading branch information
georgelemon committed Aug 23, 2023
1 parent 7ce9c54 commit 0ce88e2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 47 deletions.
95 changes: 70 additions & 25 deletions src/valido.nim
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
import std/[macros, os, strutils]

macro importFilters() =
let filtersPath = currentSourcePath().parentDir / "valido"
var
filters = newTree(nnkBracket)
procs = newTree(nnkStmtList)
exports = newTree(nnkExportStmt)
for filter in walkDir(filtersPath):
if not filter.path.endsWith(".nim"):
continue
let filterName = filter.path.extractFilename()[0 .. ^5]
filters.add ident filterName
exports.add ident filterName
result = newStmtList()
result.add(
nnkImportStmt.newTree(
nnkInfix.newTree(
ident "/",
ident "valido",
filters
)
),
exports
)

importFilters()
when defined napibuild:
import denim
import pkg/valido/[base32, base58, base64, domain, email, ean, ip, md5]

const errident = "Valido"

init proc(module: Module) =
module.registerFn(1, "isEmail"):
if Env.expect(args, errident, ("input", napi_string)):
return %* isEmail(args[0].getStr)

module.registerFn(2, "isIP4"):
echo args.len
if Env.expect(args, errident, ("input", napi_string), ("?allowLoopback", napi_boolean)):
return %* isIP4(args[0].getStr, args[1].getBool)

module.registerFn(1, "isDomain"):
if Env.expect(args, errident, ("input", napi_string)):
return %* isDomain(args[0].getStr)

module.registerFn(1, "isBase32"):
if Env.expect(args, errident, ("input", napi_string)):
return %* isBase32(args[0].getStr)

module.registerFn(1, "isBase58"):
if Env.expect(args, errident, ("input", napi_string)):
return %* isBase58(args[0].getStr)

module.registerFn(1, "isMD5"):
if Env.expect(args, errident, ("input", napi_string)):
return %* isMD5(args[0].getStr)

module.registerFn(1, "isEAN13"):
if Env.expect(args, errident, ("input", napi_string)):
return %* isEAN13(args[0].getStr)

module.registerFn(1, "isEAN8"):
if Env.expect(args, errident, ("input", napi_string)):
return %* isEAN8(args[0].getStr)

module.registerFn(1, "isEAN"):
if Env.expect(args, errident, ("input", napi_string)):
return %* isEAN(args[0].getStr).status

else:
macro importFilters() =
let filtersPath = currentSourcePath().parentDir / "valido"
var
filters = newTree(nnkBracket)
procs = newTree(nnkStmtList)
exports = newTree(nnkExportStmt)
for filter in walkDir(filtersPath):
if not filter.path.endsWith(".nim"):
continue
let filterName = filter.path.extractFilename()[0 .. ^5]
filters.add ident filterName
exports.add ident filterName
result = newStmtList()
result.add(
nnkImportStmt.newTree(
nnkInfix.newTree(
ident "/",
ident "valido",
filters
)
),
exports
)

importFilters()
2 changes: 1 addition & 1 deletion src/valido/card.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ proc isMaestro*(input: string): bool =
if cc.len notin 12..19: return
if isCard(cc):
let i = parseInt(cc[0..2])
result = i in {500..509, 560..589, 600..699}
return i in 500..509 or i in 560..589 or i in 600..699 # seems like {500..509, 560..589, ...} does not work with nim 2.0

proc isAmericanExpress*(input: string): bool =
var cc = input.multiReplace((" ", ""), ("-", ""))
Expand Down
8 changes: 4 additions & 4 deletions src/valido/date.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ proc isDate*(input: string, format = "yyyy-MM-dd"): bool =
try:
discard parse(input, format)
result = true
except TimeParseError, TimeFormatParseError, Defect:
except TimeParseError, TimeFormatParseError:
discard

proc isPastDate*(input: string, format = "yyyy-MM-dd"): bool =
Expand All @@ -14,7 +14,7 @@ proc isPastDate*(input: string, format = "yyyy-MM-dd"): bool =
let d = parse(input, format, utc())
if now() > d:
result = true
except TimeParseError, TimeFormatParseError, Defect:
except TimeParseError, TimeFormatParseError:
discard

proc isFutureDate*(input: string, format = "yyyy-MM-dd"): bool =
Expand All @@ -23,7 +23,7 @@ proc isFutureDate*(input: string, format = "yyyy-MM-dd"): bool =
let d = parse(input, format, utc())
if d > now():
result = true
except TimeParseError, TimeFormatParseError, Defect:
except TimeParseError, TimeFormatParseError:
discard

proc isToday*(input: string, format = "yyyy-MM-dd"): bool =
Expand All @@ -32,5 +32,5 @@ proc isToday*(input: string, format = "yyyy-MM-dd"): bool =
let d = parse(input, format, utc())
if d == now():
result = true
except TimeParseError, TimeFormatParseError, Defect:
except TimeParseError, TimeFormatParseError:
discard
10 changes: 3 additions & 7 deletions src/valido/domain.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ proc isDomain*(input: string): bool =
return false
var
i = 0
domainName: string
tld: string

(domainName, tld) = input.split(".")
v = input.split(".")
domainName = v[0]
tld = v[1]

let
hyphenSep = {'-'}
Expand All @@ -28,7 +27,4 @@ proc isDomain*(input: string): bool =
if input[0] in hyphenSep or input[^1] in hyphenSep: return false
# accept only valid Top Level Domains
if toUpperAscii(tld) notin validTLDs: return false

# while i < domainLen:
# inc i
result = true
6 changes: 4 additions & 2 deletions src/valido/email.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ proc isEmail*(input: string, allowSpecialChars = false): bool =
if not input.contains("@"): return false
if input.count("@") != 1: return false

var usernameInput, domainInput: string
(usernameInput, domainInput) = input.split("@")
var
v = input.split("@")
usernameInput = v[0]
domainInput = v[1]
let
userLen = usernameInput.len
domainLen = domainInput.len
Expand Down
13 changes: 9 additions & 4 deletions src/valido/uuid.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Made by Humans from OpenPeep
# https://github.com/openpeep/valido

from std/strutils import split, count, isAlphaAscii, toLowerAscii, Digits
import std/strutils

type
UUIDVersion* = enum
Expand All @@ -14,9 +14,14 @@ proc isUUID*(input: string, version: UUIDVersion = Any): bool =
## Validate given input string as UUID
## https://en.wikipedia.org/wiki/Universally_unique_identifier
if input.count("-") != 4: return false

var timeLow, timeMid, timeHigh, clockSeq, node: string
(timeLow, timeMid, timeHigh, clockSeq, node) = input.toLowerAscii.split("-")
let v = input.toLowerAscii.split("-")
if v.len != 5: return
var
timeLow = v[0]
timeMid = v[1]
timeHigh = v[2]
clockSeq = v[3]
node = v[4]

if timeLow.len != 8 or timeMid.len != 4 or timeHigh.len != 4 or
clockSeq.len != 4 or node.len != 12: return false
Expand Down
6 changes: 2 additions & 4 deletions tests/test1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,11 @@ test "isMD5":
test "isPastDate / isDate / isToday / isFutureDate (valid)":
check isPastDate("2022-01-03") == true
check isDate("2023-01-05") == true
check isFutureDate("2023-03-03") == true
check isFutureDate("2030-03-03") == true

check isPastDate("2022-01-03 14:00", "yyyy-MM-dd hh:mm") == true
check isDate("2023-01-05 2:30pm", "yyyy-MM-dd h:mtt") == true
check isFutureDate("2023-03-03 17:00", "yyyy-MM-dd hh:mm") == true

# check isToday("2023-02-02") == true
check isFutureDate("2030-03-03 17:00", "yyyy-MM-dd hh:mm") == true

test "isCard (valid)":
check isCard("4101891773067337") == true
Expand Down

0 comments on commit 0ce88e2

Please sign in to comment.