UNPKG

@cedoor/utils

Version:

Some JavaScript util functions.

3 lines (2 loc) 1.75 kB
// https://github.com/cedoor/utils v0.4.0 Copyright 2019 Omar Desogus. !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).utils=e.utils||{})}(this,function(e){"use strict";function t(e){if("string"!=typeof e)throw TypeError("Parameter must be a string.");return e.charAt(0).toUpperCase()+e.toLowerCase().slice(1)}e.capitalize=t,e.capitalizeList=function(e){if(!Array.isArray(e)||!e.every(function(e){return"string"==typeof e}))throw TypeError("Parameter must be an array of strings.");return e.map(t)},e.download=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"text/json",n=document.createElement("a"),o=new window.Blob([t],{type:r});n.href=window.URL.createObjectURL(o),n.download=e,n.dataset.downloadurl=[r,n.download,n.href].join(":"),n.style.display="none",document.body.appendChild(n),n.click(),document.body.removeChild(n)},e.get=function(e,t){return new Promise(function(r,n){var o=new window.XMLHttpRequest,a=function(e){try{return JSON.parse(e)}catch(t){return e}};o.onreadystatechange=function(){4===o.readyState&&(200===o.status?r(a(o.responseText)):n(a(o.responseText)))},o.onerror=n,o.open("GET",e,!0),o.send(t)})},e.parseURL=function(e){if("string"!=typeof e)throw TypeError("Parameter must be a string.");return{hostname:(e=new URL(e)).hostname,port:e.port,protocol:e.protocol.replace(":",""),params:e.searchParams.entries()}},e.sum=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];if(t.some(function(e){return"number"!=typeof e}))throw TypeError("Parameters must be numbers.");return t.reduce(function(e,t){return e+t},0)},Object.defineProperty(e,"__esModule",{value:!0})});