BVB Source Codes

reactide Show formats.js Source code

Return Download reactide: download formats.js Source code - Download reactide Source code - Type:.js
  1. 'use strict';
  2.  
  3. var util = require('./util');
  4.  
  5. var DATE = /^\d\d\d\d-(\d\d)-(\d\d)$/;
  6. var DAYS = [0,31,29,31,30,31,30,31,31,30,31,30,31];
  7. var TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d:\d\d)?$/i;
  8. var HOSTNAME = /^[0-9a-z](?:(?:[-0-9a-z]{0,61})?[0-9a-z])?(\.[0-9a-z](?:(?:[-0-9a-z]{0,61})?[0-9a-z])?)*$/i;
  9. var URI = /^(?:[a-z][a-z0-9+\-.]*:)?(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@\/?]|%[0-9a-f]{2})*)?(?:\#(?:[a-z0-9\-._~!$&'()*+,;=:@\/?]|%[0-9a-f]{2})*)?$/i;
  10. var UUID = /^(?:urn\:uuid\:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
  11. var JSON_POINTER = /^(?:\/(?:[^~\/]|~0|~1)*)*$|^\#(?:\/(?:[a-z0-9_\-\.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i;
  12. var RELATIVE_JSON_POINTER = /^(?:0|[1-9][0-9]*)(?:\#|(?:\/(?:[^~\/]|~0|~1)*)*)$/;
  13.  
  14.  
  15. module.exports = formats;
  16.  
  17. function formats(mode) {
  18.   mode = mode == 'full' ? 'full' : 'fast';
  19.   var formatDefs = util.copy(formats[mode]);
  20.   for (var fName in formats.compare) {
  21.     formatDefs[fName] = {
  22.       validate: formatDefs[fName],
  23.       compare: formats.compare[fName]
  24.     };
  25.   }
  26.   return formatDefs;
  27. }
  28.  
  29.  
  30. formats.fast = {
  31.   // date: http://tools.ietf.org/html/rfc3339#section-5.6
  32.   date: /^\d\d\d\d-[0-1]\d-[0-3]\d$/,
  33.   // date-time: http://tools.ietf.org/html/rfc3339#section-5.6
  34.   time: /^[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:z|[+-]\d\d:\d\d)?$/i,
  35.   'date-time': /^\d\d\d\d-[0-1]\d-[0-3]\d[t\s][0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?(?:z|[+-]\d\d:\d\d)$/i,
  36.   // uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js
  37.   uri: /^(?:[a-z][a-z0-9+-.]*)?(?:\:|\/)\/?[^\s]*$/i,
  38.   // email (sources from jsen validator):
  39.   // http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363
  40.   // http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'willful violation')
  41.   email: /^[a-z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i,
  42.   hostname: HOSTNAME,
  43.   // optimized https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9780596802837/ch07s16.html
  44.   ipv4: /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/,
  45.   // optimized http://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
  46.   ipv6: /^\s*(?:(?:(?:[0-9a-f]{1,4}:){7}(?:[0-9a-f]{1,4}|:))|(?:(?:[0-9a-f]{1,4}:){6}(?::[0-9a-f]{1,4}|(?:(?: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})|:))|(?:(?:[0-9a-f]{1,4}:){5}(?:(?:(?::[0-9a-f]{1,4}){1,2})|:(?:(?: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})|:))|(?:(?:[0-9a-f]{1,4}:){4}(?:(?:(?::[0-9a-f]{1,4}){1,3})|(?:(?::[0-9a-f]{1,4})?:(?:(?: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}))|:))|(?:(?:[0-9a-f]{1,4}:){3}(?:(?:(?::[0-9a-f]{1,4}){1,4})|(?:(?::[0-9a-f]{1,4}){0,2}:(?:(?: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}))|:))|(?:(?:[0-9a-f]{1,4}:){2}(?:(?:(?::[0-9a-f]{1,4}){1,5})|(?:(?::[0-9a-f]{1,4}){0,3}:(?:(?: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}))|:))|(?:(?:[0-9a-f]{1,4}:){1}(?:(?:(?::[0-9a-f]{1,4}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?: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}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?: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}))|:)))(?:%.+)?\s*$/i,
  47.   regex: regex,
  48.   // uuid: http://tools.ietf.org/html/rfc4122
  49.   uuid: UUID,
  50.   // JSON-pointer: https://tools.ietf.org/html/rfc6901
  51.   // uri fragment: https://tools.ietf.org/html/rfc3986#appendix-A
  52.   'json-pointer': JSON_POINTER,
  53.   // relative JSON-pointer: http://tools.ietf.org/html/draft-luff-relative-json-pointer-00
  54.   'relative-json-pointer': RELATIVE_JSON_POINTER
  55. };
  56.  
  57.  
  58. formats.full = {
  59.   date: date,
  60.   time: time,
  61.   'date-time': date_time,
  62.   uri: uri,
  63.   email: /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$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}){1,6})|(?:(?::[0-9a-f]{1,4}){0,4}:(?:(?: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}))|:))|(?::(?:(?:(?::[0-9a-f]{1,4}){1,7})|(?:(?::[0-9a-f]{1,4}){0,5}:(?:(?: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}))|:)))(?:%.+)?\s*$/i,
  67.   regex: regex,
  68.   uuid: UUID,
  69.   'json-pointer': JSON_POINTER,
  70.   'relative-json-pointer': RELATIVE_JSON_POINTER
  71. };
  72.  
  73.  
  74. formats.compare = {
  75.   date: compareDate,
  76.   time: compareTime,
  77.   'date-time': compareDateTime
  78. };
  79.  
  80.  
  81. function date(str) {
  82.   // full-date from http://tools.ietf.org/html/rfc3339#section-5.6
  83.   var matches = str.match(DATE);
  84.   if (!matches) return false;
  85.  
  86.   var month = +matches[1];
  87.   var day = +matches[2];
  88.   return month >= 1 && month <= 12 && day >= 1 && day <= DAYS[month];
  89. }
  90.  
  91.  
  92. function time(str, full) {
  93.   var matches = str.match(TIME);
  94.   if (!matches) return false;
  95.  
  96.   var hour = matches[1];
  97.   var minute = matches[2];
  98.   var second = matches[3];
  99.   var timeZone = matches[5];
  100.   return hour <= 23 && minute <= 59 && second <= 59 && (!full || timeZone);
  101. }
  102.  
  103.  
  104. var DATE_TIME_SEPARATOR = /t|\s/i;
  105. function date_time(str) {
  106.   // http://tools.ietf.org/html/rfc3339#section-5.6
  107.   var dateTime = str.split(DATE_TIME_SEPARATOR);
  108.   return dateTime.length == 2 && date(dateTime[0]) && time(dateTime[1], true);
  109. }
  110.  
  111.  
  112. function hostname(str) {
  113.   // https://tools.ietf.org/html/rfc1034#section-3.5
  114.   // https://tools.ietf.org/html/rfc1123#section-2
  115.   return str.length <= 255 && HOSTNAME.test(str);
  116. }
  117.  
  118.  
  119. var NOT_URI_FRAGMENT = /\/|\:/;
  120. function uri(str) {
  121.   // http://jmrware.com/articles/2009/uri_regexp/URI_regex.html + optional protocol + required "."
  122.   return NOT_URI_FRAGMENT.test(str) && URI.test(str);
  123. }
  124.  
  125.  
  126. function regex(str) {
  127.   try {
  128.     new RegExp(str);
  129.     return true;
  130.   } catch(e) {
  131.     return false;
  132.   }
  133. }
  134.  
  135.  
  136. function compareDate(d1, d2) {
  137.   if (!(d1 && d2)) return;
  138.   if (d1 > d2) return 1;
  139.   if (d1 < d2) return -1;
  140.   if (d1 === d2) return 0;
  141. }
  142.  
  143.  
  144. function compareTime(t1, t2) {
  145.   if (!(t1 && t2)) return;
  146.   t1 = t1.match(TIME);
  147.   t2 = t2.match(TIME);
  148.   if (!(t1 && t2)) return;
  149.   t1 = t1[1] + t1[2] + t1[3] + (t1[4]||'');
  150.   t2 = t2[1] + t2[2] + t2[3] + (t2[4]||'');
  151.   if (t1 > t2) return 1;
  152.   if (t1 < t2) return -1;
  153.   if (t1 === t2) return 0;
  154. }
  155.  
  156.  
  157. function compareDateTime(dt1, dt2) {
  158.   if (!(dt1 && dt2)) return;
  159.   dt1 = dt1.split(DATE_TIME_SEPARATOR);
  160.   dt2 = dt2.split(DATE_TIME_SEPARATOR);
  161.   var res = compareDate(dt1[0], dt2[0]);
  162.   if (res === undefined) return;
  163.   return res || compareTime(dt1[1], dt2[1]);
  164. }
  165.  
downloadformats.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top