@rip-lang/api 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.rip +37 -22
- package/package.json +1 -1
package/api.rip
CHANGED
|
@@ -385,6 +385,22 @@ export isBlank = (obj) ->
|
|
|
385
385
|
|
|
386
386
|
capitalize = (str) -> str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()
|
|
387
387
|
|
|
388
|
+
toMoney = (value, half, cents) ->
|
|
389
|
+
m = value.replace(/[$, ]/g, '').match(/^([-+]?)(\d*)\.?(\d*)$/)
|
|
390
|
+
return null unless m
|
|
391
|
+
intp = m[2] or ''
|
|
392
|
+
raw = m[3] or ''
|
|
393
|
+
return null unless intp.length or raw.length
|
|
394
|
+
neg = m[1] is '-'
|
|
395
|
+
frac = raw.padEnd(3, '0')
|
|
396
|
+
c = parseInt((intp or '0') + frac[0] + frac[1], 10) or 0
|
|
397
|
+
d = parseInt(frac[2], 10) or 0
|
|
398
|
+
rest = raw.length > 3 and /[1-9]/.test(raw.slice(3))
|
|
399
|
+
up = d > 5 or (d is 5 and rest) or (d is 5 and not rest and (if half then c % 2 isnt 0 else true))
|
|
400
|
+
c += 1 if up
|
|
401
|
+
c = if neg then -c else c
|
|
402
|
+
if cents then c else c / 100
|
|
403
|
+
|
|
388
404
|
export toName = (str, type...) ->
|
|
389
405
|
s = String(str)
|
|
390
406
|
s = s.toLowerCase().replace(/\s+/g, ' ').trim()
|
|
@@ -435,23 +451,27 @@ export toPhone = (str) ->
|
|
|
435
451
|
|
|
436
452
|
export validators =
|
|
437
453
|
# Numbers & money
|
|
438
|
-
id:
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
454
|
+
id: (v) -> v[/^([1-9]\d{0,14})$/] and parseInt(_[1])
|
|
455
|
+
int: (v) -> v[/^([-+]?(?:0|[1-9]\d{0,14}))$/] and parseInt(_[1])
|
|
456
|
+
whole: (v) -> v[/^(0|[1-9]\d{0,14})$/] and parseInt(_[1])
|
|
457
|
+
float: (v) -> v[/^([-+]?\d+(?:\.\d*)?|\.\d+)$/] and parseFloat(_[1])
|
|
458
|
+
money: (v) -> v[/^([-+]? ?\$? ?(?:[\d,]+(?:\.\d*)?|\.\d+))$/] and toMoney(_[1])
|
|
459
|
+
money_even: (v) -> v[/^([-+]? ?\$? ?(?:[\d,]+(?:\.\d*)?|\.\d+))$/] and toMoney(_[1], true)
|
|
460
|
+
cents: (v) -> v[/^([-+]? ?\$? ?(?:[\d,]+(?:\.\d*)?|\.\d+))$/] and toMoney(_[1], false, true)
|
|
461
|
+
cents_even: (v) -> v[/^([-+]? ?\$? ?(?:[\d,]+(?:\.\d*)?|\.\d+))$/] and toMoney(_[1], true, true)
|
|
442
462
|
|
|
443
463
|
# Strings & formatting
|
|
444
|
-
string: (v) -> v.replace(
|
|
445
|
-
text: (v) -> v.replace(/ +/g, ' ')
|
|
464
|
+
string: (v) -> v.replace(/(?:\t+|\s{2,})/g, ' ')
|
|
465
|
+
text: (v) -> v.replace(/ +/g , ' ')
|
|
446
466
|
|
|
447
467
|
# Name/address
|
|
448
|
-
name: (v) -> v.replace(/\s+/g, ' ')
|
|
449
|
-
address: (v) -> v.replace(/\s+/g, ' ')
|
|
468
|
+
name: (v) -> v = v.replace(/\s+/g, ' ') and toName v, 'name'
|
|
469
|
+
address: (v) -> v = v.replace(/\s+/g, ' ') and toName v, 'address'
|
|
450
470
|
|
|
451
|
-
#
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
471
|
+
# Date & time
|
|
472
|
+
date: (v) -> v[/^\d{4}(-?)\d{2}\1\d{2}$/] and _[0]
|
|
473
|
+
time: (v) -> v[/^(2[0-3]|[01]?\d):([0-5]\d)(?::([0-5]\d))?$/] and _[0]
|
|
474
|
+
time12: (v) -> v[/^(1[0-2]|0?[1-9]):([0-5]\d)(?::([0-5]\d))?\s?(am|pm)$/i] and _[0].toLowerCase()
|
|
455
475
|
|
|
456
476
|
# Booleans
|
|
457
477
|
truthy: (v) -> (v =~ /^(true|t|1|yes|y|on)$/i) and true
|
|
@@ -459,7 +479,7 @@ export validators =
|
|
|
459
479
|
bool: (v) -> if v =~ /^(true|t|1|yes|y|on)$/i then true else if v =~ /^(false|f|0|no|n|off)$/i then false else null
|
|
460
480
|
|
|
461
481
|
# Contact, geo, identity
|
|
462
|
-
email: (v) -> v[/^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$/] and _[0]
|
|
482
|
+
email: (v) -> v[/^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$/i] and _[0]
|
|
463
483
|
state: (v) -> v[/^([a-z][a-z])$/i] and _[1].toUpperCase()
|
|
464
484
|
zip: (v) -> v[/^(\d{5})/] and _[1]
|
|
465
485
|
zipplus4: (v) -> v[/^(\d{5})-?(\d{4})$/] and "#{_[1]}-#{_[2]}"
|
|
@@ -470,26 +490,21 @@ export validators =
|
|
|
470
490
|
phone: (v) -> toPhone(v)
|
|
471
491
|
|
|
472
492
|
# Web & technical
|
|
473
|
-
username: (v) -> v[/^([a-zA-Z0-9_-]{3,20})$/]
|
|
474
|
-
ip: (v) -> v[/^(
|
|
493
|
+
username: (v) -> v[/^([a-zA-Z0-9_-]{3,20})$/] and _[1].toLowerCase()
|
|
494
|
+
ip: (v) -> v[/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/] and _[0]
|
|
475
495
|
mac: (v) -> v[/^([0-9a-fA-F]{2})(?:[:-]?)([0-9a-fA-F]{2})(?:[:-]?)([0-9a-fA-F]{2})(?:[:-]?)([0-9a-fA-F]{2})(?:[:-]?)([0-9a-fA-F]{2})(?:[:-]?)([0-9a-fA-F]{2})$/] and "#{_[1]}:#{_[2]}:#{_[3]}:#{_[4]}:#{_[5]}:#{_[6]}".toLowerCase()
|
|
476
|
-
url: (v) -> v[/^(https?:\/\/)
|
|
496
|
+
url: (v) -> v[/^(https?:\/\/)[^\s/$.?#].[^\s]*$/i] and _[0]
|
|
477
497
|
color: (v) -> v[/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/] and "##{_[1].toLowerCase()}"
|
|
478
498
|
uuid: (v) -> v[/^([0-9a-f]{8})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{4})-?([0-9a-f]{12})$/i] and "#{_[1]}-#{_[2]}-#{_[3]}-#{_[4]}-#{_[5]}".toLowerCase()
|
|
479
499
|
semver: (v) -> v[/^(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*))?(?:\+([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*))?$/] and _[0]
|
|
480
500
|
|
|
481
|
-
# Time formats & currency
|
|
482
|
-
time24: (v) -> v[/^([01]?[0-9]|2[0-3]):([0-5][0-9])(?::([0-5][0-9]))?$/] and _[0]
|
|
483
|
-
time12: (v) -> v[/^(1[0-2]|0?[1-9]):([0-5][0-9])\s?(am|pm)$/i] and _[0].toLowerCase()
|
|
484
|
-
currency: (v) -> v[/^\$?(\d{1,3}(?:,\d{3})*(?:\.\d{2})?)$/] and parseFloat(_[1].replace(/,/g, ''))
|
|
485
|
-
|
|
486
501
|
# Collections & structured
|
|
487
502
|
array: (v) -> Array.isArray(v) and v or null
|
|
488
503
|
hash: (v) -> (v? and typeof v is 'object' and not Array.isArray(v)) and v or null
|
|
489
504
|
json: (v) -> if typeof v is 'string' then (try JSON.parse(v) catch then null) else if typeof v is 'object' then v else null
|
|
490
505
|
|
|
491
506
|
# Slugs & ids list
|
|
492
|
-
slug: (v) -> v[/^([a-z0-9]+(?:-[a-z0-9]+)*)$/] and _[1]
|
|
507
|
+
slug: (v) -> v[/^([a-z0-9]+(?:-[a-z0-9]+)*)$/i] and _[1].toLowerCase()
|
|
493
508
|
ids: (v) ->
|
|
494
509
|
cleaned = v.replace(/[, ]+/g, ' ').trim()
|
|
495
510
|
return null unless cleaned
|