Skip to content

Instantly share code, notes, and snippets.

View sgromkov's full-sized avatar
🦆
quack-quack

Sergey Gromkov sgromkov

🦆
quack-quack
View GitHub Profile
@sgromkov
sgromkov / merge.js
Created April 2, 2021 11:51
Immutable deep merge. Covered by unit tests on Jest
/**
* 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';
@sgromkov
sgromkov / localStorager.js
Created August 25, 2020 13:01
LocalStorage wrapper with expired time for keys
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
@sgromkov
sgromkov / callAfterDomContentLoaded.js
Created July 21, 2020 17:00
Call callback when DOMContentLoaded is fired, even it has already been fired.
/**
* 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
*/
@sgromkov
sgromkov / safetyEvent.js
Created March 7, 2020 05:44
Cross-browser event
/**
* 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;
@sgromkov
sgromkov / getUrlWithParams.js
Created March 6, 2020 19:13
Stick the url with parameters. Covered by unit tests on Jest
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
@sgromkov
sgromkov / getDomainByHostname.js
Last active March 6, 2020 18:49
Getting part of a domain name by host name. Covered by unit tests on Jest
/**
* 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('.');
}
@sgromkov
sgromkov / jsonValidator.js
Last active February 5, 2023 22:17
Validate JSON String. Covered by unit tests on Jest
/**
* 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;
@sgromkov
sgromkov / cookie.js
Last active March 6, 2020 19:13
Simple functions for working with browser's cookie. Covered by unit tests on Jest
@sgromkov
sgromkov / getPriceInRU.js
Last active March 6, 2020 18:25
Cross-Platform price formatter in 'ru-RU' locale. Covered by unit tests on Jest
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, ',', ' ')
}
@sgromkov
sgromkov / axios.mock.js
Created March 6, 2020 14:32
Mocked axios with Jest
export default {
get: jest.fn(() => {
return Promise.resolve({ data: {} });
}),
post: jest.fn(() => {
return Promise.resolve({ data: {} });
}),
defaults: {}
};