-
Notifications
You must be signed in to change notification settings - Fork 0
/
fieldval-dateval.min.js
1 lines (1 loc) · 3.88 KB
/
fieldval-dateval.min.js
1
"function"==typeof require&&(FieldVal=require("fieldval"),BasicVal=FieldVal.BasicVal);var DateVal=function(){"use strict";var r={errors:{invalid_date_format:function(){return{error:111,error_message:"Invalid date format."}},invalid_date:function(){return{error:112,error_message:"Invalid date."}},invalid_date_format_string:function(){return{error:114,error_message:"Invalid date format string."}}},date_format:function(e){var a=function(a,t){for(var i=[],n=0,o=!1;n<a.length&&!o;){var d=!1;for(var l in r.date_components){var s=a.substring(n,n+l.length);if(s===l){i.push(l),n+=l.length,d=!0;break}}d||(o=!0)}return o?FieldVal.create_error(r.errors.invalid_date_format_string,e):void t(i)};return e?(e.check=a,e):{check:a}},date_with_format_array:function(e,a){for(var t="",i=0;i<a.length;i++){var n=a[i],o=r.date_components[n];if(0===o)t+=n;else{var d;"yyyy"===n?d=e.getUTCFullYear():"yy"===n?d=e.getUTCFullYear().toString().substring(2):"MM"===n||"M"===n?d=e.getUTCMonth()+1:"dd"===n||"d"===n?d=e.getUTCDate():"hh"===n||"h"===n?d=e.getUTCHours():"mm"===n||"m"===n?d=e.getUTCMinutes():("ss"===n||"s"===n)&&(d=e.getUTCSeconds()),t+=r.pad_to_valid(d.toString(),o)}}return t},pad_to_valid:function(r,e){var a,t=BasicVal.integer({parse:!0}).check(r,function(r){a=r});if(t||0>a)return r;for(var i=0;i<e.length;i++){var n=e[i];if(r.length<=n){for(var o=n-r.length,d=0;o>d;d++)r="0"+r;return r}}return r},date:function(e,a){a=a||{};var t,i=r.date_format().check(e,function(r){t=r});i&&console.error&&console.error(i.error_message);var n=function(e,i){for(var n={},o=[],d=0,l=null,s=null,u=-1,f=!1;d<e.length&&!f;)if(u++,l=t[u],s=r.date_components[l],0!==s){for(var _=s[0],v=s[s.length-1],c="",h=0;v>h;h++){var m=e[d+h];if(void 0===m)break;var y=m.charCodeAt(0);if(48>y||y>57){if(h===_)break;f=!0;break}c+=m}if(d+=h,f)break;var g=parseInt(c);o.push(c),"yyyy"===l||"yy"===l?n.year=g:"MM"===l||"M"===l?n.month=g:"dd"===l||"d"===l?n.day=g:"hh"===l||"h"===l?n.hour=g:"mm"===l||"m"===l?n.minute=g:("ss"===l||"s"===l)&&(n.second=g)}else{if(e[d]!==l){f=!0;break}o.push(null),d++}if(f||u<t.length-1)return FieldVal.create_error(r.errors.invalid_date_format,a);if(void 0!==n.hour&&(n.hour<0||n.hour>23))return FieldVal.create_error(r.errors.invalid_date,a);if(void 0!==n.minute&&(n.minute<0||n.minute>59))return FieldVal.create_error(r.errors.invalid_date,a);if(void 0!==n.second&&(n.second<0||n.second>59))return FieldVal.create_error(r.errors.invalid_date,a);if(void 0!==n.month){var T=n.month;if(T>12)return FieldVal.create_error(r.errors.invalid_date,a);if(1>T)return FieldVal.create_error(r.errors.invalid_date,a);if(n.day){var V=n.day;if(1>V)return FieldVal.create_error(r.errors.invalid_date,a);if(n.year){var M=n.year;if(2==T)if(M%400===0||M%100!==0&&M%4===0){if(V>29)return FieldVal.create_error(r.errors.invalid_date,a)}else if(V>28)return FieldVal.create_error(r.errors.invalid_date,a)}if(4===T||6===T||9===T||11===T){if(V>30)return FieldVal.create_error(r.errors.invalid_date,a)}else if(2===T){if(V>29)return FieldVal.create_error(r.errors.invalid_date,a)}else if(V>31)return FieldVal.create_error(r.errors.invalid_date,a)}}else if(n.day){if(n.day>31)return FieldVal.create_error(r.errors.invalid_date,a);if(n.day<1)return FieldVal.create_error(r.errors.invalid_date,a)}if(a.emit)if(a.emit===r.EMIT_COMPONENT_ARRAY)i(o);else if(a.emit===r.EMIT_OBJECT)i(n);else if(a.emit===r.EMIT_DATE){var F=new Date(0);F.setUTCFullYear(0),void 0!==n.year&&F.setYear(n.year),void 0!==n.month&&F.setUTCMonth(n.month-1),void 0!==n.day&&F.setUTCDate(n.day),void 0!==n.hour&&F.setUTCHours(n.hour),void 0!==n.minute&&F.setUTCMinutes(n.minute),void 0!==n.second&&F.setUTCSeconds(n.second),i(F)}};return a?(a.check=n,a):{check:n}}};return r.EMIT_COMPONENT_ARRAY={},r.EMIT_DATE={},r.EMIT_OBJECT={},r.date_components={yyyy:[4],yy:[2],MM:[2],M:[1,2],dd:[2],d:[1,2],hh:[2],h:[1,2],mm:[2],m:[1,2],ss:[2],s:[1,2]," ":0,"-":0,"/":0,":":0},r}.call();"undefined"!=typeof module&&(module.exports=DateVal);