carbon-react
Version:
A library of reusable React components for easily building user interfaces.
2 lines (1 loc) • 2.26 kB
JavaScript
;var t=require("date-fns/fp/format"),e=require("date-fns/fp/formatISO"),n=require("date-fns/fp/isMatch"),r=require("date-fns/fp/isValid"),i=require("date-fns/fp/parse"),s=require("date-fns/fp/parseWithOptions"),u=require("date-fns/fp/parseISO");function o(t,e){if(e&&t)return i.parse(new Date,t,e)}function a(t){return t&&"Invalid Date"!==t.toString()}function f(t,e){return t.map(((n,r)=>e.substring(t[r],t[r+1])))}function l(t,e){return t.every(((r,i)=>function(t,e,r,i){return t.includes("d")?t.length===e.length&&n.isMatch(r.join("."),i.join(".")):t.length===e.length&&n.isMatch(t,e)}(r,e[i],t,e)))}const c=t=>["",".",",","-","/"," ",":"].slice(1).find((e=>t.includes(e)))||"";function p(t){return u.parseISO(t)}function g(t){if(10!==t.length||"Invalid Date"===p(t).toString())return!1;const e=t.split("-");return 3===e.length&&4===e[0].length&&2===e[1].length&&2===e[2].length}exports.additionalYears=function(t,e){if(t.split("y").length-1!=2)return[t,e];const n=t.startsWith("yy"),r=n?0:e.length-2,i=n?2:e.length,s=n?i:0,u=n?e.length:e.length-2;let o=e.substring(r,i);const a=e.substring(s,u),f=Number(o);return o=String(f<69?2e3+f:1900+f),n?[`yyyy${t.substring(2,t.length)}`,`${o}${a}`]:[`${t.substring(0,t.length-2)}yyyy`,`${a}${o}`]},exports.checkISOFormatAndLength=g,exports.findMatchedFormatAndValue=function(t,e){if(!t)return["",""];const n=c(t);return e.filter((e=>e.length===t.length&&c(e)===n)).reduce(((e,r)=>{const i=c(r);if(""===n&&""===i){const e=function(t,e){const n=e.split("").reduce(((t,n,r)=>0===r||n!==e[r-1]?[...t,r]:t),[]),r=f(n,e),i=f(n,t);return l(r,i)?[r.join(" "),i.join(" ")]:null}(t,r);if(e)return e}if(n&&i&&n===i){const e=function(t,e,n){return l(e.split(n),t.split(n))?[e,t]:null}(t,r,n);if(e)return e}return e}),[])},exports.formatToISO=function(t,n){const r=o(t,n);return r&&a(r)?e.formatISO(r).split("T")[0]:null},exports.formattedValue=function(e,n){return n?t.format(e,n):""},exports.getDisabledDays=function(t="",e=""){const n=[];if(t||e)return g(t)&&n.push({before:p(t)}),g(e)&&n.push({after:p(e)}),n},exports.getSeparator=c,exports.isDateValid=a,exports.isValidLocaleDate=function(t,e){const n=s.parseWithOptions({locale:e})(new Date,"P",t);return r.isValid(n)},exports.parseDate=o,exports.parseISODate=p;