UNPKG

cutls

Version:

Some JavaScript util functions.

73 lines (61 loc) • 1.94 kB
// https://github.com/cedoor/cutls v0.1.6 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.cutls = global.cutls || {})); }(this, function (exports) { 'use strict'; /** * 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); } var index = { capitalize: capitalize, capitalizeList: capitalizeList }; /** * 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 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); } var index$1 = { download: download }; exports.file = index$1; exports.strings = index; Object.defineProperty(exports, '__esModule', { value: true }); }));