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
/** | |
* Performs a deep merge of objects and returns a new object. | |
* Does not modify objects (immutable) and concatenates arrays. | |
* @function merge | |
* @param {...object} objects - Objects to merge | |
* @returns {object} New object with a combined key/value | |
*/ | |
const merge = function (...objects) { | |
const isObject = (obj) => obj && typeof obj === 'object'; |
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
import { isValidJsonObject } from 'https://gist.github.com/sgromkov/bd65afb17cb26f70def920a33ace2ada#file-jsonvalidator-js'; | |
/** | |
* Wrapper above the Storage object | |
* which can be used to access the current origin's local storage space. | |
* | |
* It provides all the methods and properties of localStorage, | |
* but extends them by adding expiration date for keys. | |
* | |
* Use it instead of Cookie if you need to save a value that should be destroyed |
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
/** | |
* Call callback when DOMContentLoaded is fired, | |
* even it has already been fired. | |
* | |
* @see https://stackoverflow.com/a/35996985 | |
* | |
* @function callAfterDomContentLoaded | |
* @param {function} callback | |
* A function that should be fired | |
*/ |
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
/** | |
* Cross-browser event, also works in IE11 | |
* @see https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events#The_old-fashioned_way | |
* @use window.dispatchEvent(safetyEvent('resize')); | |
* @param {string} type | |
* @return event object | |
*/ | |
const safetyEvent = function (type) { | |
let event; |
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
const paramsReducer = function paramsReducer(acc, param) { | |
const splittedParam = param.split('='); | |
const key = splittedParam[0]; | |
const value = splittedParam[1]; | |
return Object.assign({}, acc, { [key]: value }); | |
}; | |
/** | |
* Returns the url with the pasted parameters |
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 domain name's part by given hostname and level | |
* @param {string} hostname | |
* @param {Number} level | |
* @returns {string} domain name's part | |
*/ | |
export default function getDomainByHostname(hostname = "", level = 0) { | |
return hostname.split('.').slice(-level).join('.'); | |
} |
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 the argument for compliance with valid json | |
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch#The_exception_identifier | |
* @param {*} text | |
* @returns {boolean} | |
*/ | |
export function isValidJson(text) { | |
try { | |
JSON.parse(text); | |
return true; |
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
import numberFormat from './numberFormat'; | |
/** | |
* Returns price in format '1 000 000 000' | |
* @param {Number|String} price | |
* @returns {String} | |
*/ | |
export default function getPriceInRU(price) { | |
return numberFormat(price, 0, ',', ' ') | |
} |
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
export default { | |
get: jest.fn(() => { | |
return Promise.resolve({ data: {} }); | |
}), | |
post: jest.fn(() => { | |
return Promise.resolve({ data: {} }); | |
}), | |
defaults: {} | |
}; |