forked from kodie/moment-holiday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moment-holiday.min.js
7 lines (7 loc) · 6.62 KB
/
moment-holiday.min.js
1
2
3
4
5
6
7
//! moment-holiday.js
//! version : 1.5.1
//! author : Kodie Grantham
//! license : MIT
//! https://github.com/kodie/moment-holiday
(function(){var _=require("moment"),x=[],v=function(t,e,r){for(var n,a=[],i=0;i<x.length;i++){var o=x[i](t,e);(o||!1===o)&&(n=o)}if(!1===n)return!1;if("string"==typeof n?e=n:n&&(a=n),!_.isMoment(a)&&!a.length&&e.charAt(0).match(/[0-9(]/)){var s=!1,h=e.split("|");1<h.length&&(s=!0),2<h.length&&(h=[h[0],h[1]]);for(i=0;i<h.length;i++){var l=_(t),f=h[i].split("/");if(1===f.length||2===f.length&&"("!==f[1].charAt(0)&&4===f[1].length){var d=h[i];i=-1,h=[];for(var c=1;c<13;c++)h.push(c+"/"+d)}else if(2<f.length&&l.year(parseInt(f[2])),l.month(parseInt(f[0])-1),"("===f[1].charAt(0)){var y=f[1].slice(1,-1).split(","),u=parseInt(y[0]),v=parseInt(y[1]),g=_(l).startOf("month"),p=_(l).endOf("month").diff(g,"days")+1,O=[];if(y[1]&&"["===y[1].charAt(0)){var b=!0;(v=parseInt(y[1].slice(1,-1)))<0&&(b=!1,v=parseInt(y[1].slice(2,-1))),g=_(l).date(v);for(var k=0;k<7;k++){if(g.day()===u){a.push(_(g));break}b?g.add(1,"day"):g.subtract(1,"day")}continue}for(var w=0;w<p;w++)g.day()===u&&O.push(_(g)),g.add(1,"day");if(!v){a=a.concat(O);continue}l=v<0?O[O.length+v]:O[v-1],a.push(l)}else a.push(l.date(f[1]))}if(s&&1<a.length){var m=a[1].diff(a[0],"days");if(1<m){var j=_(a[0]);a=[a[0]];for(i=0;i<m;i++)j.add(1,"day"),a.push(_(j))}}}a=P(a);for(i=0;i<a.length;i++)_.isMoment(a[i])?(r&&(0===a[i].day()&&(a[i]=a[i].add(1,"day")),6===a[i].day()&&(a[i]=a[i].subtract(1,"day"))),a[i]=a[i].startOf("day")):delete a[i];return!!a.length&&(1===a.length?a[0]:a)},g=function(t,e){var r=[];e=P(e);for(var n=0;n<e.length;n++){var a=t.match(new RegExp(e[n],"gi"));a&&(r=r.concat(a))}return r},l=function(t,e,r,n,a){var i={},o=[],s={};if(h=a||_.holidays.active,h.hasOwnProperty(e))o.push(e);else if(fk=O(e,h))o.push(fk);else{for(var l in h)if(h.hasOwnProperty(l)){if(i[l]=g(e,l.split(/[\s,.-]+/).filter(function(t){return 2<t.length})).length,h[l].keywords_n)if(g(e,h[l].keywords_n).length){i[l]=0;continue}if(h[l].keywords_y){var f=g(e,h[l].keywords_y);if(!f||f.length!==h[l].keywords_y.length){i[l]=0;continue}i[l]+=f.length}if(h[l].keywords){var d=g(e,h[l].keywords);if(!d)continue;i[l]+=d.length}}for(var c in i)i[c]&&i.hasOwnProperty(c)&&(o.length&&i[c]!==i[o[0]]?i[c]>i[o[0]]&&(o=[c]):o.push(c))}if(!o.length)return!1;if(!1===n)return o;for(var y=0;y<o.length;y++){var u=v(t,h[o[y]].date,r);u&&(s[o[y]]=u)}return!!Object.keys(s).length&&s},f=function(t,e,r,n,a){var i=[];n&&(i={}),e=P(e),a||(a=_.holidays.active);for(var o=0;o<e.length;o++){var s=l(t,e[o],r,n,a);s&&(i=n?c(i,s):i.concat(s))}return i},p=function(t,e){var r=_.holidays.active,n={};for(var a in r)r.hasOwnProperty(a)&&(td=v(t,r[a].date,e))&&(n[a]=td);return n},d=function(t){if(regions=t.split("/"),t=regions[0].toLowerCase().replace(" ","_"),regions.shift(),!_.holidays[t])try{var e="./locale/";"build"==__dirname.split("/").slice(-1).pop()&&(e="."+e),require(e+t)}catch(t){}return!!_.holidays[t]&&(regions.length?function(t,e){var r=_.holidays[t],n={};if(r)for(var a=0;a<e.length;a++){var i=e[a].toLowerCase(),o=_.holidays[t+"/"+i];for(var s in o={},r)if(r.hasOwnProperty(s)){var h=r[s].regions||[],l=r[s].regions_n||[];h.length&&h.join().toLowerCase().split(),l.length&&l.join().toLowerCase().split(),(!h.length&&!l.length||h.length&&~h.indexOf(i)||l.length&&!~l.indexOf(i))&&(o[s]=r[s])}o&&(n=c(n,o))}return!!Object.keys(n).length&&n}(t,regions):_.holidays[t])},r=function(t,e,r,n){e||(e=1);for(var a=p(t,n),i=_(t),o=t.year(),s=[],h=0;h<e;h++)for(var l=_(i);;){var f=!1;if(r?l.add(1,"day"):l.subtract(1,"day"),l.year()!==o&&(a=p(l,n),o=l.year()),!Object.keys(a).length){f=!0;break}for(var d in a)if(a.hasOwnProperty(d)){for(var c=!1,y=P(a[d]),u=0;u<y.length;u++)if(l.isSame(y[u],"day")){s.push(y[u]),i=_(l),c=!0;break}if(c){f=!0;break}}if(f)break}return!!s.length&&(1===s.length?s[0]:s)},P=function(t){return t&&t.constructor!==Array?[t]:t},O=function(t,e){e.constructor===Object&&(e=Object.keys(e));for(var r=0;r<e.length;r++)if(t.toLowerCase()===e[r].toLowerCase())return e[r];return!1},c=function(t,e){return Object.assign({},t,e)};_.fn.holiday=function(t,e){_.holidays.active;var r={},n=!1;if(t){t.constructor!==Array&&(n=!0,t=[t]);for(var a=0;a<t.length;a++)(td=l(this,t[a],e))&&(r=Object.assign({},r,td))}else r=p(this,e);var i=Object.keys(r);return!!i.length&&(1===i.length&&n?r[i[0]]:r)},_.fn.holidays=function(t,e){return this.holiday(t,e)},_.fn.isHoliday=function(t,e){var r,n,a=[];if(n=t?(t=P(t),r=this.holiday(t,e),!1):(r=p(this,e),!0),!r)return!1;for(var i in r)if(r.hasOwnProperty(i))for(var o=P(r[i]),s=0;s<o.length;s++)if(this.isSame(o[s],"day")){if(!n)return!0;a.push(i)}return!!a.length&&(1===a.length?a[0]:a)},_.fn.previousHoliday=function(t,e){return r(this,t,!1,e)},_.fn.previousHolidays=function(t,e){return this.previousHoliday(t,e)},_.fn.nextHoliday=function(t,e){return r(this,t,!0,e)},_.fn.nextHolidays=function(t,e){return this.nextHoliday(t,e)},_.fn.holidaysBetween=function(t,e){t||(t=new Date),t=_(t).subtract(1,"day");for(var r=p(this,e),n=_(this),a=n.year(),i=[],o=0;o<t.diff(this,"days")&&(n.add(1,"day"),n.year()!==a&&(r=p(n,e),a=n.year()),Object.keys(r).length);o++)for(var s in r){for(var h=!1,l=P(r[s]),f=0;f<l.length;f++)if(n.isSame(l[f],"day")){i.push(l[f]),h=!0;break}if(h)break}return!!i.length&&i},_.holidays={active:{},active_last:{}},_.modifyHolidays={set:function(t,e){var r={};if(t.constructor===Array){for(var n=[],a=0;a<t.length;a++){var i=l(this,t[a],null,!1);i&&(n=n.concat(i))}if(n.length)for(var o in r=c(_.holidays.active))r.hasOwnProperty(o)&&(~n.indexOf(o)||delete r[o])}else if("string"==typeof t){var s=d(t);if(s)if(e){var h=f(this,e,!1,!1,s);for(a=0;a<h.length;a++)r[h[a]]=c(s[h[a]])}else r=c(s)}else r=t;return!Object.keys(r).length&&t!==r||Object.is(_.holidays.active,r)||(_.holidays.active_last=c(_.holidays.active),_.holidays.active=r),this},add:function(t,e){if("string"==typeof t){var r=d(t);if(t={},r)if(e)for(var n=f(this,e,!1,!1,r),a=0;a<n.length;a++)t[n[a]]=c(r[n[a]]);else t=r}return Object.keys(t).length&&(_.holidays.active_last=c(_.holidays.active),_.holidays.active=c(_.holidays.active,t)),this},remove:function(t){t=P(t);var e=f(this,t,!1,!1),r=c(_.holidays.active);if(e)for(var n=0;n<e.length;n++)delete r[e[n]];return Object.is(_.holidays.active,r)||(_.holidays.active_last=c(_.holidays.active),_.holidays.active=r),this},undo:function(){var t=c(_.holidays.active);return _.holidays.active=c(_.holidays.active_last),_.holidays.active_last=t,this},load:function(t){t=P(t);for(var e=0;e<t.length;e++)d(t[e]);return this},extendParser:function(t){return x.push(t),this}},null!=("undefined"!=typeof module&&null!==module?module.exports:void 0)&&(module.exports=_)}).call(this);
//# sourceMappingURL=moment-holiday.min.js.map