UNPKG

@cedoor/utils

Version:

Some JavaScript util functions.

141 lines (123 loc) 3.51 kB
// https://github.com/cedoor/utils v0.4.0 Copyright 2019 Omar Desogus. (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = global || self, factory(global.utils = global.utils || {})); }(this, function (exports) { 'use strict'; /** * Download a file with the the text passed as parameter. * @param filename * @param content * @param contentType */ function download(filename, content) { var contentType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'text/json'; var element = document.createElement('a'); var blob = new window.Blob([content], { type: contentType }); element.href = window.URL.createObjectURL(blob); element.download = filename; element.dataset.downloadurl = [contentType, element.download, element.href].join(':'); element.style.display = 'none'; document.body.appendChild(element); element.click(); document.body.removeChild(element); } /** * Send a GET HTTP request. * @param url * @param body * @returns {Promise<unknown>} */ function get(url, body) { return new Promise(function (resolve, reject) { var http = new window.XMLHttpRequest(); var json = function json(string) { try { return JSON.parse(string); } catch (error) { return string; } }; http.onreadystatechange = function () { if (http.readyState === 4) { if (http.status === 200) { resolve(json(http.responseText)); } else { reject(json(http.responseText)); } } }; http.onerror = reject; http.open('GET', url, true); http.send(body); }); } /** * Get numbers and return their sum. * @param numbers * @returns {number} */ function sum() { for (var _len = arguments.length, numbers = new Array(_len), _key = 0; _key < _len; _key++) { numbers[_key] = arguments[_key]; } if (numbers.some(function (number) { return typeof number !== 'number'; })) { throw TypeError('Parameters must be numbers.'); } return numbers.reduce(function (a, b) { return a + b; }, 0); } /** * Return all parameters of an URL. * @param url * @returns {{hostname: *, protocol: *, port: *, params: *}} */ function parseURL(url) { if (typeof url !== 'string') { throw TypeError('Parameter must be a string.'); } url = new URL(url); return { hostname: url.hostname, port: url.port, protocol: url.protocol.replace(':', ''), params: url.searchParams.entries() }; } /** * Return the string with the first letter capitalized. * @param string * @returns {string} */ function capitalize(string) { if (typeof string !== 'string') { throw TypeError('Parameter must be a string.'); } return string.charAt(0).toUpperCase() + string.toLowerCase().slice(1); } /** * Return an array of strings with first letter capitalized. * @param list * @returns {string[]} */ function capitalizeList(list) { if (!Array.isArray(list) || !list.every(function (string) { return typeof string === 'string'; })) { throw TypeError('Parameter must be an array of strings.'); } return list.map(capitalize); } exports.capitalize = capitalize; exports.capitalizeList = capitalizeList; exports.download = download; exports.get = get; exports.parseURL = parseURL; exports.sum = sum; Object.defineProperty(exports, '__esModule', { value: true }); }));