This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Changes the format of a string-date. | |
* | |
* @param {Object | String} options: it can be a String with the date, or an Object with the following properties: | |
* - {String} date: the entry date to change the format | |
* - {String} inputFormat: the format of the entry date | |
* - {String} outputFormat: the format of the output date | |
* @return {String} | |
*/ | |
const formatDate = (() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets all repeated values between two arrays | |
* | |
* @param {Array} array1: array to compare | |
* @param {Array} array2: array to compare | |
* @return {Array} | |
*/ | |
function repeatedValues(array1, array2) { | |
return array1.reduce((repeated, value) => { | |
if (~array2.indexOf(value) && !~repeated.indexOf(value)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a random integer between min and max | |
* | |
* @export | |
* @param {Number} min: minimum value (inclusive) | |
* @param {Number} max: maximum value (inclusive) | |
* @return {Number} | |
*/ | |
function randomInt(min, max) { | |
return Math.floor(Math.random() * (1 + max - min)) + min; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Merges all inner arrays into one-level depth array. | |
* | |
* @param {Array} array: the array to be flattened | |
* @return {Array} | |
*/ | |
const flatten = array => array.reduce( | |
(flattened, cv) => flattened.concat(Array.isArray(cv) ? flatten(cv) : cv), | |
[] // initial value of flattened array | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sums all the values in an array by reducing it. | |
* | |
* @param {Array} array: the collection to iterate | |
* @param {String | Number} prop: (optional) property name or array index | |
* @return {Number} | |
*/ | |
function sumValues(array, prop) { | |
if (!array || !array.length) return 0; | |
if ((/string|number/).test(typeof prop)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sorts an array and allows multiple sorting criteria. | |
* | |
* It applies the Schwartzian transform: | |
* https://en.wikipedia.org/wiki/Schwartzian_transform | |
* | |
* Author: David Rivera | |
* Github: https://github.com/jherax | |
* | |
* You can fork this project on github: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks whether a storage mechanism is available. | |
* @param {String} storageType: it can be "localStorage", "sessionStorage" or any global object that implements Web Storage interface. | |
* @return {Boolean} | |
*/ | |
function storageAvailable(storageType) { | |
var storage = window[storageType]; | |
var data = '__proxy-storage__'; | |
try { | |
storage.setItem(data, data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets the first value associated to an object's property. | |
* Can go recursively through objects and arrays. | |
* | |
* @param {Object | Array} obj: the object or array to look for the property/index value | |
* @param {String | Number} key: the object's property name or array index | |
* @return {Any} | |
*/ | |
function getValueByKey(obj, key) { | |
let value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @private | |
var isObject = (value) => | |
value != null && typeof value === 'object'; | |
/** | |
* @private | |
* Restores the one-level-depth object to the original nested object. | |
* | |
* @param {Array} names: list of keys in the object | |
* @param {any} value: the value of the object to transform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var removeEmpty = (() => { | |
/** | |
* Determines if the entry parameter is not an object or array. | |
* | |
* @private | |
* @param {Any} v: the value to test | |
* @return {Boolean} | |
*/ | |
const isNotObject = v => v === null || typeof v !== 'object'; |