PageRenderTime 106ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/q/q.patterns.js

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