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 onLocationChange = (() => { | |
const subscribers = [] | |
const fireEvent = () => subscribers.forEach(sub => sub()) | |
const { pushState, replaceState } = window.history | |
window.history.pushState = function(...args) { | |
pushState.apply(window.history, args) | |
fireEvent() | |
} |
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 randomBetween = (min, max) => | |
Math.floor(Math.random() * (max - min + 1) + 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
const observe = (el, cb, options = { childList: true }) => { | |
const func = mutations => cb(mutations) === false && mo.disconnect() | |
const mo = new MutationObserver(func) | |
mo.observe(el, options) | |
} |
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
function keyBy(collection, key) { | |
const result = {} | |
collection.forEach(item => result[item[key]] = item) | |
return result | |
} |
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 const classes = (...args) => { | |
return args | |
.map((arg) => { | |
if (arg === null || arg === undefined) return null | |
return typeof arg === 'string' | |
? arg | |
: Object.keys(arg) | |
.filter((key) => arg[key]) | |
.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
export const newDate = (date, updates) => { | |
date = new Date(date) | |
Object.keys(updates).forEach((method) => { | |
const methodName = 'set' + method.replace(/^\w/, (c) => c.toUpperCase()) | |
if (!date[methodName]) throw new Error(`Key '${method}' is not supported!`) | |
date[methodName](updates[method]) | |
}) | |
return date | |
} |
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
async function asyncForEach(array, callback) { | |
for (let index = 0; index < array.length; index++) { | |
await callback(array[index], index, array) | |
} | |
} | |
const asyncMap = async (arr, cb) => | |
Promise.all(arr.map(async (a, i) => await cb(a, i))) |
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 createEventBus = () => { | |
const subscribers = {} | |
let counter = 0 | |
const on = (name, cb) => { | |
if (!subscribers[name]) subscribers[name] = {} | |
subscribers[name][++counter] = cb | |
return btoa(name + '.' + counter) | |
} |
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 audioPlayer = (function () { | |
const [state, updateState, watchState] = useState({ | |
name: 'Ivan', | |
}) | |
watchState({ | |
name: (name) => { | |
console.log('name updated to: ' + name) | |
}, | |
}) |
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 store = { | |
actions: { | |
play: (dispatch, payload, actions) => { | |
// do some action | |
// payload --> 'abc' | |
dispatch('play', 'arg1', 'arg2') | |
} | |
}, | |
reducer: { | |
play: (_) => ({ ..._ }) |
OlderNewer