/node_modules/q/q.patterns.js
JavaScript | 56 lines | 23 code | 8 blank | 25 comment | 0 complexity | 67f89e2eda8015d3c55126da0a6e1dc0 MD5 | raw file
Possible License(s): Unlicense
- (function() {
- /*
- INFO: This is the rudimentary start of a general use pattern matching library
-
- components will be broken down into various sections:
-
- isa() - already supported by Q, isa will remain the same - only determining the [[class]] of an object
-
- String.prototype.likea(obj) - returns truthy if the string is like (possible match) whatever is passed in
-
- "123-23-1827".likea("ssn") == true
- "12:30pm".likea("time") == true
- "asdfasdf".likea("upc") == false
-
- String.prototype.asa(obj) - returns a value formatted as the obj passed in
-
- "5125478475".asa("phonenumber") == "(512) 547-8475";
- "1800".asa("stdtime") == "6:00pm";
-
- References:
-
- */
-
- var email_formats = {
- //http://blog.gerv.net/2011/05/html5_email_address_regexp/
- html5_spec : (/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/)
- };
-
- var phone_formats = {
- //Steven Levithan - http://blog.stevenlevithan.com/archives/validate-phone-number
- nanp_fuzzy : (/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/),
- nanp_strict : (/^\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/),
- nanp_longdistance_fuzzy : (/^(?:\+?1[-. ]?)?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/),
- nanp_short_fuzzy : (/^(?:\(?([0-9]{3})\)?[-. ]?)?([0-9]{3})[-. ]?([0-9]{4})$/)
- };
-
- String.prototype.likea = function(locale) {
- locale = q.def(locale, 'nanp');
- return phone_formats[locale].test(this);
- };
-
- String.prototype.formatAs = function() {
- var self = this;
-
- self.phoneNumber = function(locale) {
- locale = q.def(locale, 'nanp');
- return self.replace(phone_formats[locale], "($1) $2-$3");
- };
-
- return this;
-
- };
-
- //alert("5129683585".formatAs().phoneNumber());
- //alert("15129683585".formatAs().phoneNumber());
- })();