UNPKG

vue-luxon

Version:

Easy use of DateTime formatting & parsing in Vue using Luxon

2 lines (1 loc) 4.22 kB
"use strict";var t=require("luxon"),e=function(){for(var t=arguments,o={},r=0,a=arguments.length;r<a;++r){var u=t[r];if(u)for(var n in u)u.hasOwnProperty(n)&&u[n]&&("[object Object]"!==Object.prototype.toString.call(u[n])?o[n]=u[n]:o[n]=e(o[n],u[n]))}return o},o={full:t.DateTime.DATETIME_FULL,fulls:t.DateTime.DATETIME_FULL_WITH_SECONDS,huge:t.DateTime.DATETIME_HUGE,huges:t.DateTime.DATETIME_HUGE_WITH_SECONDS,med:t.DateTime.DATETIME_MED,meds:t.DateTime.DATETIME_MED_WITH_SECONDS,short:t.DateTime.DATETIME_SHORT,shorts:t.DateTime.DATETIME_SHORT_WITH_SECONDS,date_full:t.DateTime.DATE_FULL,date_huge:t.DateTime.DATE_HUGE,date_med:t.DateTime.DATE_MED,date_medd:t.DateTime.DATE_MED_WITH_WEEKDAY,date_short:t.DateTime.DATE_SHORT,time24:t.DateTime.TIME_24_SIMPLE,time24longoffset:t.DateTime.TIME_24_WITH_LONG_OFFSET,time24s:t.DateTime.TIME_24_WITH_SECONDS,time:t.DateTime.TIME_SIMPLE,times:t.DateTime.TIME_WITH_SECONDS},r={relative:{format:"relative"},server:{zone:"utc",format:"iso"},client:{zone:"local",format:"med"},inputdate:{zone:"client",format:"yyyy-MM-dd"}};Object.keys(o).forEach((function(t){r[t]={format:o[t]}}));var a={templates:{},input:{zone:"utc",format:"iso"},output:{zone:"local",format:{year:"numeric",month:"long",day:"numeric"},locale:null,relative:{round:!0,unit:null}},parse:!1};function u(e,o){if("object"!=typeof o.input)throw"`input` is not an object, but: "+o.input;if("object"!=typeof o.output)throw"`output` is not an object, but: "+o.output;var r=function(e,o,r){if(!e)return"never";var a=e,u=o,n=r;switch(u.toLowerCase()){case"sql":return t.DateTime.fromSQL(a,{zone:n});case"iso":return t.DateTime.fromISO(a,{zone:n});case"http":return t.DateTime.fromHTTP(a,{zone:n});case"jsdate":return t.DateTime.fromJSDate(a,{zone:n});case"rfc2822":return t.DateTime.fromRFC2822(a,{zone:n});case"millis":return t.DateTime.fromMillis(a,{zone:n});case"seconds":case"unix":return t.DateTime.fromSeconds(a,{zone:n});default:return t.DateTime.fromFormat(a,u,{zone:n})}}(e,o.input.format,o.input.zone);if(0==r.isValid)throw r.invalidReason;var a=e,u=o.output.format,n=o.output.zone,i=function(t){return t&&"locale"!=t&&"local"!=t?t:null}(o.output.locale?o.output.locale:null);if("never"==r)return null;if(o.parse&&(r=o.parse.call(r)),r=(r="locale"!=n&&"local"!=n?r.setZone(n):r.setZone("local")).setLocale(i),"object"==typeof u)return r.toLocaleString(u);switch(u.toLowerCase()){case"relative":return r.toRelative(o.output.relative);case"sql":return r.toSQL(a);case"iso":return r.toISO(a);case"http":return r.toHTTP(a);case"jsdate":return r.toJSDate(a);case"rfc":case"rfc2822":return r.toRFC2822(a);case"millis":return r.toMillis(a);case"unix":case"seconds":return r.toSeconds(a);default:return r.toFormat(u)}}var n={vueluxon:function(t){return function(o,n,i){if(o&&"object"==typeof o){if(n=o,!o.value)throw"The first parameter should be a string, or the object should contain a `value` property";o=o.value}return"string"==typeof n&&(n={output:{format:n}}),u(o,function(t){if("object"!=typeof t)throw"formatSettings.js: `settings` is not an object";var o=e(r,t.templates);if("string"==typeof t.input){var u=t.input.toLowerCase();o[u]?t.input=o[u]:t.input={format:t.input}}if("string"==typeof t.output){var n=t.output.toLowerCase();o[n]?t.output=o[n]:t.output={format:t.output}}t.relative&&(t.output.relative=t.relative),t.locale&&(t.output.locale=t.locale);var i=function(t){if("object"==typeof t)return t;var e=t.toLowerCase();return o[e]?o[e].format:t},l=function(t){var e=t.toLowerCase();return o[e]?o[e].zone:t};return(t=e(a,t)).input.format=i(t.input.format),t.output.format=i(t.output.format),t.input.zone=l(t.input.zone),t.output.zone=l(t.output.zone),t.output.relative=function(t){if("object"==typeof t)return t;var e=t.toLowerCase();return o[e]?o[e].relative:t}(t.output.relative),t}(e(t,n,i)))}},install:function(t,e){var o=this.vueluxon(e),r=e&&e.methodName?e.methodName:"$luxon";t.prototype[r]=o,t.filter("luxon",(function(){return"string"==typeof arguments[1]?o(arguments[0],arguments[2],{output:{format:arguments[1]}}):o(arguments[0],arguments[1])}));t.filter("luxonRelative",(function(){return o(arguments[0],arguments[2],{output:{format:"relative",relative:arguments[1]}})}))}};module.exports=n;