Skip to content

Instantly share code, notes, and snippets.

const store = {
actions: {
play: (dispatch, payload, actions) => {
// do some action
// payload --> 'abc'
dispatch('play', 'arg1', 'arg2')
}
},
reducer: {
play: (_) => ({ ..._ })
const blockOne = (funcName) => {
if (funcName === 'one') {
return false
}
}
const emitEvent = (funcName) => {
console.log('[event] ' + funcName)
}
@Ivannnnn
Ivannnnn / usePrevious.js
Last active October 31, 2021 14:09
Hooks
function usePrevious(value) {
const ref = useRef()
useEffect(() => {
ref.current = value
}, [value])
return ref.current
}
const machine = {
empty: {
SELECT: {
to: 'loading',
action: (state, dispatch) => {
interval.stop()
loadBook().then((book) => dispatch('SELECT_SUCCESS', book))
return { ...state, title: null, progress: 0 }
},
},
@Ivannnnn
Ivannnnn / prop.js
Last active December 14, 2021 19:43
Get object properties by path without error when null
function prop(value, path) {
path = path
.replace(/\[(\d)\]/g, (_, v) => '.' + v)
.replace(/^\./, '')
.split('.')
let current = value
for (let prop of path) {
if (current[prop]) current = current[prop]
else return current[prop]
}
@Ivannnnn
Ivannnnn / getGlobals.js
Last active August 15, 2022 12:26
get globals of a webpage
const getGlobals = (() => {
const diff = (a, b) => a.filter((v) => !b.includes(v))
const windowProps = JSON.parse(
'["window","self","document","name","location","customElements","history","locationbar","menubar","personalbar","scrollbars","statusbar","toolbar","status","closed","frames","length","top","opener","parent","frameElement","navigator","origin","external","screen","innerWidth","innerHeight","scrollX","pageXOffset","scrollY","pageYOffset","visualViewport","screenX","screenY","outerWidth","outerHeight","devicePixelRatio","clientInformation","screenLeft","screenTop","defaultStatus","defaultstatus","styleMedia","onsearch","isSecureContext","performance","onappinstalled","onbeforeinstallprompt","crypto","indexedDB","webkitStorageInfo","sessionStorage","localStorage","onbeforexrselect","onabort","onblur","oncancel","oncanplay","oncanplaythrough","onchange","onclick","onclose","oncontextmenu","oncuechange","ondblclick","ondrag","ondragend","ondragenter","ondragleave","ondragover","ondragstart","o
@Ivannnnn
Ivannnnn / timeTaken.js
Last active August 31, 2022 10:36
Execution time of function
const timeTaken =
(fn) =>
async (...args) => {
console.time(fn.name);
const res = await fn(...args);
console.timeEnd(fn.name);
return res;
};
@Ivannnnn
Ivannnnn / renderWithEvents.html
Last active December 12, 2021 12:12
render html with events
<div id="app"></div>
<script>
const $ = (...args) => {
if (args.length > 1) args.reverse()
const [query, parent = document] = args
return Array.from(parent.querySelectorAll(query))
}
$.create = (html) => {
@Ivannnnn
Ivannnnn / createTimer.js
Last active December 12, 2021 13:30
Timer
const createInterval = () => {
let interval = null
const start = (cb, delay) => !interval && (interval = setInterval(cb, delay))
const stop = () => {
clearInterval(interval)
interval = null
}
return { start, stop }
}
@Ivannnnn
Ivannnnn / functional.js
Last active October 12, 2022 11:13
Some functional programming functions
const SORT_STRATEGIES = {
asc: (a, b) => (a - b > 0 ? 1 : -1),
desc: (a, b) => (a - b < 0 ? 1 : -1),
};
const pipe = (...fns) =>
fns.reduce(
(f, g) =>
(...args) =>
g(f(...args))