Skip to content

Instantly share code, notes, and snippets.

@clooth
Created August 30, 2012 02:40
Show Gist options
  • Save clooth/3521838 to your computer and use it in GitHub Desktop.
Save clooth/3521838 to your computer and use it in GitHub Desktop.
#
# Choro - Map Layer
# Copyright (c) 2012 Nico Hämäläinen <nico@bender.fi>
# MIT Licensed
#
# Inspired by node-validator
#
net = require 'net'
# Regular expressions
Regexp =
Email: /^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/
URL: /^(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?:\w+:\w+@)?(localhost|(?:(?:[-\w\d{1-3}]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|edu|co\.uk|ac\.uk|it|fr|tv|museum|asia|local|travel|[a-z]{2}))|((\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)(\.(\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)){3}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,="'\(\)_\*]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$ |\/.,*:;=]|%[a-f\d]{2})*)?$/i
Alpha: /^[a-zA-Z]+$/
Alphanumeric: /^[a-zA-Z0-9]+$/
Numeric: /^-?[0-9]+$/
Decimal: /^(?:-?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/
Lowercase: /^[a-z0-9]+$/
Uppercase: /^[A-Z0-9]+$/
Empty: /^[\s\t\r\n]*$/
UUID: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
UUIDV3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i
UUIDV4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
CreditCard: /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/
SanitizeCreditCard: /[^0-9]+/g
Validator =
isEmail: (str) -> str.match Regexp.Email
isURL: (str) -> str.match Regexp.URL
isIP: (str) -> net.isIP(str) != 0
isAlpha: (str) -> str.match Regexp.Alpha
isAlphanumeric: (str) -> str.match Regexp.Alphanumeric
isNumeric: (str) -> str.match Regexp.Numeric
isLowercase: (str) -> str.match Regexp.Lowercase
isUppercase: (str) -> str.match Regexp.Uppercase
isInteger: (str) ->
int = parseInt str * 1, 10
not isNan(int) and parseFloat(str) is int
isDecimal: (str) -> str?.length and str.match Regexp.Decimal
isDivisibleBy: (str, n) -> not(parseFloat(str) % n)
notNull: (str) -> str?.length
isNull: (str) -> str == ""
notEmpty: (str) -> !str.match Regexp.Empty
equals: (a, b) -> a is b
contains: (str, elem) -> str.indexOf(elem) >= 0
notContains: (str, elem) -> not(Validator.contains str, elem)
regex: (str, pattern, modifiers) ->
str += ''
if Util.type(pattern) isnt 'regexp'
pattern = new RegExp(pattern, modifiers)
str.match pattern
notRegex: (str, pattern, modifiers) ->
not(validators.regex str, pattern, modifiers)
len: (str, min, max) ->
str.length >= min and (!(max?) or str.length <= max)
isUUID: (str, version) ->
if version is 3 or version is "v3"
pattern = Regexp.UUIDV3
else if version is 4 or version is 'v4'
pattern = Regexp.UUIDV4
else
pattern = Regexp.UUID
str.match pattern
isDate: (str) -> not isNaN(Date.parse(str))
isIn: (str, options) ->
validOptions = options and Util.type(options.indexOf) is 'function'
validOptions and ~options.indexOf(str);
notIn: (str, options) ->
validOptions = options and Util.type(options.indexOf) is 'function'
validOptions and options.indexOf(str) is -1
min: (str, val) -> isNaN(parseFloat(str)) or parseFloat(str) >= val
max: (str, val) -> isNaN(parseFloat(str)) or parseFloat(str) <= val
isArray: (str) -> Util.type(str) is 'array'
isCreditCard: (str) ->
sanitized = str.replace Regexp.SanitizeCreditCard ''
if sanitized.match(Regexp.CreditCard) is null
return null
sum = 0
shouldDouble = false
for i in [sanitized.length-1..0]
digit = sanitized.substring(i, (i + 1))
tmpNum = parseInt digit, 10
if shouldDouble
tmpNum *= 2
sum += if tmpNum >= 10 then (tmpNum % 10) + 1 else tmpNum
else
sum += tmpNum
shouldDouble = not shouldDouble
if sum % 10 is 0
sanitized
else
null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment