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