Code coverage report for univ/index.js

Statements: 93.12% (433 / 465)      Branches: 91.7% (265 / 289)      Functions: 98.48% (65 / 66)      Lines: 93.62% (411 / 439)      Ignored: none     

All files » univ/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928  1 1   1   7                         41       1                                                                   1 15491     1 988     1 7610     1 3432     1 6982     1 921     1 1090   1 50     1 2868           1 3501 2868 2868 2868         1 970 946 946         1 1241 1241 1241 673 1086 1086   673   568     1 25 25 50 50   50 350 350           25     1 19   1 24                       1 946 502 139     444 134             1 108   108 108     1 89 89 89 89 59 30     30     89   89         1 71 71 71 71 71             71 71         1 1163 1163   1163   1163 507 2     507 54     453 775 605           507 507   5   507     1   658 658 9 3     9 9 22 13       9 9   9   9     1 363 363   5 5 9     5     1 361 361   8 8 17     8     1 316 316   316 316     1 320 320   320 320     1 1 78 78 78     1 42 14         14 14 14     1 36 14         14 14 14       1 1 111 111 111     1 58 19         19 19 19 19     1 49 19         19 19 19 19     1 1 49 49 36   49     1 25   6 2   2   4 4 4     1 21   7 2   2   5 5 5         1 1 184 184 116   184     1 90     34         34 34 34     1 72     34         34 34 34         1 1 184 184 116   184     1 90     34         34 34 34     1 72     34         34 34 34       1 1 49 49 31   49     1 26     9         9   9 9     1 26     9         9   9 9         1 1 39 39 39       1 1 42 42 42       1 1 48 48 48       1 1 13 13 13       1 1 22 22 22     1 921   921   13 13           13 13   13     1 358 358 613   358     1 673   25           25 25 19         1                                     1 11 11 11             1 56 27 31 11   31 7 24   11     9 9     1 46 8 8 8   1 41 9 9 9     1                           1   946 946                             946   946   273 135 135 135     273 273     673   363             310               673           673     79       79     32       32     92       92     92       92     59       59     26       26     42       42     38 38     33 33     12       12     19 19     43       43                               673   6 3 3 3     6   6 4 4       673   673   667 8 8     667 8004 279 279       667   667   667 667   6           1 970 970 970 970     1   970 970   970   946 946 946       970 966 966     970     1 3880 3880     1 416     416   416     1 1 1   1 1     1    
 
var moment = require("moment");
var events = require("events").EventEmitter;
 
var BUILD_IN_RULE = {
  isEmail: function(email){
    return verifyIsEmail(email, {});
  },
  isUrl: verifyIsUrl,
  isNumber: verifyIsNumber,
  isTel: verifyIsTel,
  isColor: verifyIsColor,
  isDate: verifyIsDate,
  isDateTime: verifyIsDateTime,
  isDateTimeLocal: verifyIsDateTimeLocal,
  isMonth: verifyIsMonth,
  isWeek: verifyIsWeek,
  isTime: verifyIsTime,
  isMobile: function(mobile){
    return verifyIsMobile(mobile, {});
  }
};
 
var RULE_TYPES = {
  "text": "text",
  "password": "password",
  "email": "email",
  "radio": "radio",
  "checkbox": "checkbox",
  "select-one": "select-one",
  "select-multiple": "select-multiple",
  "hidden": "hidden",
  "search": "search",
  "textarea": "textarea",
  "file": "file",
  "number": "number",
  "range": "range",
  "date": "date",
  "week": "week",
  "month": "month",
  "time": "time",
  "datetime": "datetime",
  "datetime-local": "datetime-local",
  "url": "url",
  "tel": "tel",
  "color": "color",
  "submit": "submit",
  "button": "button",
  "reset": "reset",
  "image": "image",
  "fieldset": "fieldset",
  "legend": "legend"
};
 
 
// @param {Object} object.
// @param {String} type, like `Array`, `RegExp`, etc.
function typeOf(object, type){
  return Object.prototype.toString.call(object) === "[object " + type + "]";
}
 
function isString(object){
  return typeOf(object, "String");
}
 
function isBoolean(object){
  return typeOf(object, "Boolean");
}
 
function isArray(object){
  return typeOf(object, "Array");
}
 
function isNumber(object){
  return !isNaN(object) && typeOf(object, "Number");
}
 
function isRegExp(object){
  return typeOf(object, "RegExp");
}
 
function isFunction(object){
  return typeOf(object, "Function");
}
function isObject(object){
  return null!==object && typeOf(object, "Object");
}
 
function trim(string){
  return String(string).replace(/^\s+/, "").replace(/\s+$/, "");
}
 
// @param {Object} object.
// @return {Number} return number if object can convert to number,
//                  else return NaN.
function toNumber(object){
  if(isNumber(object)){return object;}
  object = trim(object);
  Iif("" === object){return NaN;}
  return Number(object);
}
 
// @param {Object} rules
// @param {Function} handler
function eachRules(rules, handler){
  for(var ruleName in rules){
    Eif(rules.hasOwnProperty(ruleName)){
      handler.call(rules, ruleName, rules[ruleName]);
    }
  }
}
 
function eachValues(handler, values /* ,... */){
  var certified = true;
  var args = Array.prototype.slice.call(arguments, 0).slice(1);
  if(isArray(values)){
    for(var i=0,l=values.length; i<l; i++){
      args[0] = values[i];
      certified = certified && handler.apply(null, args);
    }
    return certified;
  }
  return handler.apply(null, args);
}
 
function merge(/* ... */){
  var result = {};
  for(var i=0,object,l=arguments.length; i<l; i++){
    object = arguments[i];
    Iif(!isObject(object)){continue;}
 
    for(var key in object){
      Eif(object.hasOwnProperty(key)){
        result[key] = object[key]
      }
    }
 
  }
 
  return result;
}
 
function startsWith(string, prefix){
  return isString(string) && string.indexOf(prefix) === 0;
}
function endsWith(string, suffix) {
  return isString(string) &&
    string.indexOf(suffix, string.length - suffix.length) !== -1;
}
 
 
// 通常情况下的 required 校验。
// @param {Boolean,Undefined} required, is rule required?
// @param {String,Object} values, validation data.
// @return {Boolean,Undefined}
//        if !values and required, return false;
//        if !values and not-required, return true;
//        if values, validate passed, and continue next, return undefined.
function verifyRequired(required, values){
  if(isArray(values)){
    if(!verifyMinLimit(1, values)){
      return !isBoolean(required) || !required;
    }
  }else{
    if("undefined"===typeof values || null===values || ""===values){
      return !isBoolean(required) || !required;
    }
  }
  //!return undefined;
}
 
 
function verifyIsNumber(value, validity){
  var certified = /^[+-]?\d+(?:[eE][+-]?\d+)?$/.test(value) ||
    /^[+-]?(?:\d+)?\.\d+(?:[eE][+-]?\d+)?$/.test(value);
  validity.typeMismatch = !certified;
  return certified;
}
 
function verifyMin(value, min, validity){
  var certified;
  value = toNumber(value);
  min = toNumber(min);
  if(!isNumber(min)){
    certified = true;
  } else Iif(!isNumber(value)){
    certified = false;
  } else {
    certified = value >= min;
  }
 
  validity.rangeUnderflow = !certified;
 
  return certified;
 
  //return !isNumber(min) || isNumber(value) && Number(value) >= Number(min);
}
 
function verifyMax(value, max, validity){
  var certified;
  value = toNumber(value);
  max = toNumber(max);
  Eif(!isNumber(max)){
    certified = true;
  } else if(!isNumber(value)){
    certified = false;
  } else {
    certified = value <= max;
  }
  // XXX: Non-Effect.
  validity.rangeOverflow = !certified;
  return certified;
 
  //return !isNumber(max) || isNumber(value) && Number(value) <= Number(max);
}
 
function verifyMinLimit(minlimit, values, validity){
  var certified;
  var length = 0;
 
  minlimit = toNumber(minlimit);
 
  if(!isNumber(minlimit)){return true;}
  if(!isArray(values)){
    values = [ values ];
  }
 
  if(values.length < minlimit){
    certified = false;
  } else {
 
    for(var i=0,l=values.length; i<l; i++){
      if("undefined"!==typeof values[i] && null!==values[i] && ""!==values[i]){
        length++;
      }
    }
 
  }
 
  certified = length >= minlimit;
  if (validity) {
    // XXX: tooShort? no. rangeUnderflow? no. is tooFew.
    validity.customError = !certified;
  }
  return certified;
}
 
function verifyMaxLimit(maxlimit, values, validity){
 
  maxlimit = toNumber(maxlimit);
  if(!isNumber(maxlimit)){return true;}
  if(!isArray(values)){
    values = [ values ];
  }
 
  var length = 0;
  for(var i=0,l=values.length; i<l; i++){
    if("undefined"!==typeof values[i] && null!==values[i] && ""!==values[i]){
      length++;
    }
  }
 
  var certified = length <= maxlimit;
  Eif (validity) {
    // XXX: tooLong? no. rangeOverflow? no. is tooMuch.
    validity.customError = !certified;
  }
  return certified;
}
 
function verifyMinLengthList(minlength, values, validity){
  minlength = toNumber(minlength);
  if(!isNumber(minlength)){return true;}
 
  var certified = true;
  for(var i=0,l=values.length; i<l; i++){
    certified = certified && verifyMinLength(minlength, values[i], validity);
  }
 
  return certified;
}
 
function verifyMaxLengthList(maxlength, values, validity){
  maxlength = toNumber(maxlength);
  if(!isNumber(maxlength)){return true;}
 
  var certified = true;
  for(var i=0,l=values.length; i<l; i++){
    certified = certified && verifyMaxLength(maxlength, values[i], validity);
  }
 
  return certified;
}
 
function verifyMinLength(minlength, value, validity){
  minlength = toNumber(minlength);
  var certified = !isNumber(minlength) ||
         (isString(value) && value.length >= minlength);
  validity.tooShort = !certified;
  return certified;
}
 
function verifyMaxLength(maxlength, value, validity){
  maxlength = toNumber(maxlength);
  var certified = !isNumber(maxlength) ||
         (isString(value) && value.length <= maxlength);
  validity.tooLong = !certified;
  return certified;
}
 
var RE_MONTH = /^\d{4,}\-\d{2}$/;
function verifyIsMonth(value, validity){
  var certified = RE_MONTH.test(value) && moment(value).isValid();
  validity.typeMismatch = !certified;
  return certified;
}
 
function verifyMinMonth(value, min, instance_context, validity){
  if(!min){return true;}
  Iif(!verifyIsMonth(min, validity)){
    instance_context._evt.emit("error",
      new TypeError('[type=month][min='+min+'] is invalid month.'));
    return true;
  }
  var certified = moment(value) >= moment(min);
  validity.rangeUnderflow = !certified;
  return certified;
}
 
function verifyMaxMonth(value, max, instance_context, validity){
  if(!max){return true;}
  Iif(!verifyIsMonth(max, validity)){
    instance_context._evt.emit("error",
      new TypeError('[type=month][max='+max+'] is invalid month.'));
    return true;
  }
  var certified = moment(value) <= moment(max);
  validity.rangeOverflow = !certified;
  return certified;
}
 
// TODO: #4, remove moment.
var RE_TIME = /^\d{2}:\d{2}:\d{2}$/;
function verifyIsTime(value, validity){
  var certified = RE_TIME.test(value) && moment("2014-01-01 " + value).isValid();
  validity.typeMismatch = !certified;
  return certified;
}
 
function verifyMinTime(value, min, instance_context, validity){
  if(!min){return true;}
  Iif(!verifyIsTime(min, validity)){
    instance_context._evt.emit("error",
      new TypeError('[type=time][min='+min+'] is invalid time.'));
    return true;
  }
  var date = '2014-01-01T';
  var certified = moment(date+value) >= moment(date+min);
  validity.rangeUnderflow = !certified;
  return certified;
}
 
function verifyMaxTime(value, max, instance_context, validity){
  if(!max){return true;}
  Iif(!verifyIsTime(max, validity)){
    instance_context._evt.emit("error",
      new TypeError('[type=time][max='+max+'] is invalid time.'));
    return true;
  }
  var date = '2014-01-01T';
  var certified = moment(date+value) <= moment(date+max);
  validity.rangeOverflow = !certified;
  return certified;
}
 
var RE_DATE = /^\d{4,}\-\d{2}\-\d{2}$/;
function verifyIsDate(value, validity){
  var certified = RE_DATE.test(value) && moment(value).isValid();
  if (validity){
    validity.typeMismatch = !certified;
  }
  return certified;
}
 
function verifyMinDate(value, min, instance_context, validity){
  if(!min){return true;}
  // Do't change validity on verify `min` rule.
  if(!verifyIsDate(min /* , validity */)){
    instance_context._evt.emit("error",
      new TypeError('[type=date][min='+min+'] is invalid date.'));
    return true;
  }
  var certified = moment(value) >= moment(min);
  validity.rangeUnderflow = !certified;
  return certified;
}
 
function verifyMaxDate(value, max, instance_context, validity){
  if(!max){return true;}
  // Do't change validity on verify `max` rule.
  if(!verifyIsDate(max/* , validity */)){
    instance_context._evt.emit("error",
      new TypeError('[type=date][max='+max+'] is invalid date.'));
    return true;
  }
  var certified = moment(value) <= moment(max);
  validity.rangeOverflow = !certified;
  return certified;
}
 
 
// http://www.w3.org/TR/html-markup/input.datetime.html
var RE_DATETIME = /^\d{4,}\-\d\d\-\d\dT\d\d:\d\d:\d\d(?:[+-]\d\d:\d\d)?Z?$/;
function verifyIsDateTime(value, validity){
  var certified = RE_DATETIME.test(value) && moment(value).isValid();
  if (validity) {
    validity.typeMismatch = !certified;
  }
  return certified;
}
 
function verifyMinDateTime(value, min, instance_context, validity){
  if(!min){return true;}
  // Do't change validity on verify `min` rule.
  // TODO: test cases.
  Iif(!verifyIsDateTime(min /* , validity */)){
    instance_context._evt.emit("error",
      new TypeError('[type=datetime][min='+min+'] is invalid datetime.'));
    return true;
  }
  var certified = moment(value) >= moment(min);
  validity.rangeUnderflow = !certified;
  return certified;
}
 
function verifyMaxDateTime(value, max, instance_context, validity){
  if(!max){return true;}
  // Do't change validity on verify `max` rule.
  // TODO: test cases.
  Iif(!verifyIsDateTime(max /* , validity */)){
    instance_context._evt.emit("error",
      new TypeError('[type=datetime][max='+max+'] is invalid datetime.'));
    return true;
  }
  var certified = moment(value) <= moment(max);
  validity.rangeOverflow = !certified;
  return certified;
}
 
 
// [input=type=datetime-local](http://www.w3.org/TR/html-markup/input.datetime-local.html)
var RE_DATETIME_LOCAL = /^\d{4,}\-\d\d\-\d\dT\d\d:\d\d:\d\d(?:[+-]\d\d:\d\d)?Z?$/;
function verifyIsDateTimeLocal(value, validity){
  var certified = RE_DATETIME_LOCAL.test(value) && moment(value).isValid();
  if (validity) {
    validity.typeMismatch = !certified;
  }
  return certified;
}
 
function verifyMinDateTimeLocal(value, min, instance_context, validity){
  if(!min){return true;}
  // Do't change validity on verify `min` rule.
  // TODO: test cases.
  Iif(!verifyIsDateTimeLocal(min /* , validity */)){
    instance_context._evt.emit("error",
      new TypeError('[type=datetime-local][min='+min+'] is invalid datetime.'));
    return true;
  }
  var certified = moment(value) >= moment(min);
  validity.rangeUnderflow = !certified;
  return certified;
}
 
function verifyMaxDateTimeLocal(value, max, instance_context, validity){
  if(!max){return true;}
  // Do't change validity on verify `max` rule.
  // TODO: test cases.
  Iif(!verifyIsDateTimeLocal(max /* , validity */)){
    instance_context._evt.emit("error",
      new TypeError('[type=datetime-local][max='+max+'] is invalid datetime.'));
    return true;
  }
  var certified = moment(value) <= moment(max);
  validity.rangeOverflow = !certified;
  return certified;
}
 
 
var RE_WEEK = /^\d{4,}-W\d{2}$/;
function verifyIsWeek(value, validity){
  var certified = RE_WEEK.test(value) && moment(value).isValid();
  if (validity) {
    validity.typeMismatch = !certified;
  }
  return certified;
}
 
function verifyMinWeek(value, min, instance_context, validity){
  if(!min){return true;}
  // Do't change validity on verify `min` rule.
  // TODO: test cases.
  Iif(!verifyIsWeek(min /* , validity */)){
    instance_context._evt.emit("error",
      new TypeError('[type=week][min='+min+'] is invalid week.'));
    return true;
  }
  var certified = moment(value) >= moment(min);
  // XXX: Non-Effect.
  validity.rangeUnderflow = !certified;
  return certified;
}
 
function verifyMaxWeek(value, max, instance_context, validity){
  if(!max){return true;}
  // Do't change validity on verify `max` rule.
  // TODO: test cases.
  Iif(!verifyIsWeek(max /* , validity */)){
    instance_context._evt.emit("error",
      new TypeError('[type=week][max='+max+'] is invalid week.'));
    return true;
  }
  var certified = moment(value) <= moment(max);
  // XXX: Non-Effect.
  validity.rangeOverflow = !certified;
  return certified;
}
 
 
// [RFC1738](http://www.faqs.org/rfcs/rfc1738.html)
var RE_URL = /^https?:\/\/(?:[\w.-]*(?::[^@]+)?@)?(?:[\w-]+\.){1,3}[\w]+(?::\d+)?(?:\/.*)?$/;
function verifyIsUrl(value, validity){
  var certified = RE_URL.test(value);
  validity.typeMismatch = !certified;
  return certified;
}
 
 
var RE_EMAIL = /^\w+(?:[\._+\-]\w+)*@[\w_-]+(?:\.[\w_-]+)+$/;
function verifyIsEmail(value, validity){
  var certified = RE_EMAIL.test(value);
  validity.typeMismatch = !certified;
  return certified;
}
 
 
var RE_MOBILE = /^(?:13[0-9]|14[57]|15[0-35-9]|17[0678]|18[0-9])\d{8}$/;
function verifyIsMobile(value, validity){
  var certified = RE_MOBILE.test(value);
  validity.typeMismatch = !certified;
  return certified;
}
 
 
var RE_TEL = /^(?:\(\+\d{2}\))?\d{3,4}\-\d{7,8}$/;
function verifyIsTel(value, validity){
  var certified = RE_TEL.test(value);
  validity.typeMismatch = !certified;
  return certified;
}
 
 
var RE_COLOR = /^#[0-9a-fA-F]{6}$/;
function verifyIsColor(value, validity){
  var certified = RE_COLOR.test(value);
  validity.typeMismatch = !certified;
  return certified;
}
 
function verifyPattern(pattern, value, instance_context, validity){
  Eif(!isRegExp(pattern)){
 
    if(!isString(pattern)){return true;}
 
    try{
      pattern = new RegExp(pattern);
    }catch(ex){
      instance_context._evt.emit("error", ex);
      return true;
    }
  }
  var certified = pattern.test(value);
  validity.patternMismatch = !certified;
 
  return certified;
}
 
function verifyPatternList(pattern, values, instance_context, validity){
  var certified = true;
  for(var i=0,l=values.length; i<l; i++){
    certified = certified && verifyPattern(pattern, values[i], instance_context, validity);
  }
  return certified;
}
 
function verifyFunction(ruleFunction, value, datas, certifiedCallback){
  if(!isFunction(ruleFunction)){return true;}
 
  var build_in_rule = merge(BUILD_IN_RULE, {
    data: function(key){
      return datas[key];
    }
  });
 
  var result = ruleFunction.call(build_in_rule, value, certifiedCallback);
  if("undefined" !== typeof result){
    return result;
  }
 
}
 
var MIME_TYPE = {
  "txt": "text/plain",
  "htm": "text/html",
  "html": "text/html",
  "js": "application/javascript",
  "css": "text/css",
  "csv": "text/csv",
  "xml": "text/xml",
  "jpg": "image/jpeg",
  "jpeg": "image/jpeg",
  "png": "image/png",
  "gif": "image/gif",
  "pdf": "application/pdf",
  "doc": "application/msword",
  "docx": "application/msword",
  "zip": "application/zip",
  "mp3": "audio/mpeg",
  "ogg": "audio/ogg"
};
function typeByName(fileName){
  var ext = fileName.split(".").slice(-1);
  Eif(MIME_TYPE.hasOwnProperty(ext)){
    return MIME_TYPE[ext];
  }
}
 
// @param {Array} accept.
// @param {File} file.
// @return {Boolean}
function verifyFileType(file, accept, validity){
  if(!isArray(accept) || !file || !file.name){return true;}
  for(var i=0,l=accept.length; i<l; i++){
    if(!file.type){
      file.type = typeByName(file.name);
    }
    if(accept[i] === file.type){
      return true;
    }else if(endsWith(accept[i], "/*") &&
        startsWith(file.type, accept[i].replace(/\*$/, "")) ){
      return true;
    }
  }
  validity.typeMismatch = true;
  return false;
}
 
function verifyMinFileSize(file, min, validity){
  if(!isNumber(min) || !isNumber(file.size)){return true;}
  var certified = file.size >= min;
  validity.tooShort = !certified;
  return certified;
}
function verifyMaxFileSize(file, max, validity){
  if(!isNumber(max) || !isNumber(file.size)){return true;}
  var certified = file.size <= max;
  validity.tooLong = !certified;
  return certified;
}
 
var ValidityState = {
  customError: "customError",
  patternMismatch: "patternMismatch",
  rangeOverflow: "rangeOverflow",
  rangeUnderflow: "rangeUnderflow",
  stepMismatch: "stepMismatch",
  tooLong: "tooLong",
  tooShort: "tooShort",
  typeMismatch: "typeMismatch",
  valueMissing: "valueMissing",
  badInput: "badInput",
  valid: "valid"
};
 
function verify(ruleName, rule, values, datas, instance_context){
 
  var certified = true;
  var validity = {
    customError: false,
    patternMismatch: false,
    rangeOverflow: false,
    rangeUnderflow: false,
    stepMismatch: false,
    tooLong: false,
    tooShort: false,
    typeMismatch: false,
    valueMissing: false,
    badInput: false,
    valid: true,
    validationMessage: ValidityState.valid
  };
 
  var resultRequired = verifyRequired(rule.required, values);
  // fast return if required rule not match.
  if("undefined" !== typeof resultRequired){
 
    if (resultRequired === false) {
      validity.valueMissing = true;
      validity.valid = false;
      validity.validationMessage = ValidityState.valueMissing;
    }
 
    instance_context._evt.emit(resultRequired ? "valid":"invalid", ruleName, values, validity);
    return resultRequired;
  }
 
  if(isArray(values)){
 
    certified = certified &&
      verifyMinLengthList(rule.minlength, values, validity) &&
      verifyMaxLengthList(rule.maxlength, values, validity) &&
      verifyPatternList(rule.pattern, values, instance_context, validity);
 
  }else{
 
    certified = certified &&
      verifyMinLength(rule.minlength, values, validity) &&
      verifyMaxLength(rule.maxlength, values, validity) &&
      verifyPattern(rule.pattern, values, instance_context, validity);
 
  }
 
  // FIXME: validity.
  certified = certified &&
    verifyMinLimit(rule.minlimit, values, validity) &&
    verifyMaxLimit(rule.maxlimit, values, validity);
 
 
  // rule: type, min, max.
  switch(rule.type){
  case RULE_TYPES.number:
  case RULE_TYPES.range:
    certified = certified &&
      eachValues(verifyIsNumber, values, validity) &&
      eachValues(verifyMin, values, rule.min, validity) &&
      eachValues(verifyMax, values, rule.max, validity);
    break;
 
  case RULE_TYPES.date:
    certified = certified &&
      eachValues(verifyIsDate, values, validity) &&
      eachValues(verifyMinDate, values, rule.min, instance_context, validity) &&
      eachValues(verifyMaxDate, values, rule.max, instance_context, validity);
    break;
 
  case RULE_TYPES.datetime:
    certified = certified &&
      eachValues(verifyIsDateTime, values, validity) &&
      eachValues(verifyMinDateTime, values, rule.min, instance_context, validity) &&
      eachValues(verifyMaxDateTime, values, rule.max, instance_context, validity);
    break;
 
  case RULE_TYPES["datetime-local"]:
    certified = certified &&
      eachValues(verifyIsDateTimeLocal, values, validity) &&
      eachValues(verifyMinDateTimeLocal, values, rule.min, instance_context, validity) &&
      eachValues(verifyMaxDateTimeLocal, values, rule.max, instance_context, validity);
    break;
 
  case RULE_TYPES.time:
    certified = certified &&
      eachValues(verifyIsTime, values, validity) &&
      eachValues(verifyMinTime, values, rule.min, instance_context, validity) &&
      eachValues(verifyMaxTime, values, rule.max, instance_context, validity);
    break;
 
  case RULE_TYPES.week:
    certified = certified &&
      eachValues(verifyIsWeek, values, validity) &&
      eachValues(verifyMinWeek, values, rule.min, instance_context, validity) &&
      eachValues(verifyMaxWeek, values, rule.max, instance_context, validity);
    break;
 
  case RULE_TYPES.month:
    certified = certified &&
      eachValues(verifyIsMonth, values, validity) &&
      eachValues(verifyMinMonth, values, rule.min, instance_context, validity) &&
      eachValues(verifyMaxMonth, values, rule.max, instance_context, validity);
    break;
 
  case RULE_TYPES.url:
    certified = certified && eachValues(verifyIsUrl, values, validity);
    break;
 
  case RULE_TYPES.email:
    certified = certified && eachValues(verifyIsEmail, values, validity);
    break;
 
  case RULE_TYPES.tel:
    certified = certified && (
        eachValues(verifyIsTel, values, validity) ||
        eachValues(verifyIsMobile, values, validity)
      );
    break;
 
  case RULE_TYPES.color:
    certified = certified && eachValues(verifyIsColor, values, validity);
    break;
 
  case RULE_TYPES.file:
    certified = certified &&
      eachValues(verifyFileType, values, rule.accept, validity) &&
      eachValues(verifyMinFileSize, values, rule.min, validity) &&
      eachValues(verifyMaxFileSize, values, rule.max, validity);
    break;
 
  //case RULE_TYPES.select-one:
  //case RULE_TYPES.radio:
  //case RULE_TYPES.text:
  //case RULE_TYPES.search:
  //case RULE_TYPES.textarea:
  //case RULE_TYPES.checkbox:
  //case RULE_TYPES["select-multiple"]:
  //case RULE_TYPES.password:
  //default:
    //break;
  }
 
  //! NOTE: Do't each loop values by verifyFunction,
  //        each loop values in user custom function if need.
  var result = verifyFunction(rule.custom, values, datas, function(certified){
 
    if (!certified) {
      validity.customError = true;
      validity.valid = false;
      validity.validationMessage = ValidityState.customError;
    }
 
    instance_context._evt.emit(certified ? "valid":"invalid", ruleName, values, validity);
 
    if(--instance_context._pending === 0){
      instance_context._evt.emit("complete", instance_context._certified && certified);
      instance_context._certified = true;
    }
  });
 
  instance_context._certified = certified;
 
  if(typeof result !== "undefined"){
 
    if (!result) {
      validity.customError = true;
      validity.valid = false;
    }
 
    for(var key in validity){
      if (validity.hasOwnProperty(key) && key !== "valid" && isBoolean(validity[key]) && validity[key]) {
        validity.validationMessage = ValidityState[key];
        validity.valid = false;
      }
    }
 
    certified = certified && result;
 
    validity.valid = certified;
 
    instance_context._evt.emit(certified ? "valid":"invalid", ruleName, values, validity);
    return certified;
  }else{
    instance_context._pending++;
  }
 
}
 
 
var Validator = function(rules){
  this._rules = rules;
  this._evt = new events();
  this._pending = 0;
  this._certified = true;
};
 
Validator.prototype.validate = function(data){
 
  var certified = true;
  var ME = this;
 
  eachRules(this._rules, function(ruleName, rule){
 
    var values = data[ruleName];
    var result = verify(ruleName, rule, values, data, ME);
    certified = certified && result;
 
  });
 
  if(this._pending === 0){
    ME._evt.emit("complete", certified);
    ME._certified = true;
  }
 
  return this;
};
 
Validator.prototype.on = function(eventName, handler){
  this._evt.on(eventName, handler);
  return this;
};
 
Validator.prototype.off = function(eventName, handler){
  Iif(isFunction(handler)){
    this._evt.removeListener(eventName, handler);
  }else{
    this._evt.removeAllListeners(eventName);
  }
  return this;
};
 
Validator.rule = function(ruleName, validation){
  Iif(!isString(ruleName)){return false;}
  Iif(!isFunction(validation)){return BUILD_IN_RULE[ruleName];}
 
  BUILD_IN_RULE[ruleName] = validation;
  return true;
};
 
module.exports = Validator;