This file contains hidden or 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 machine = { | |
| empty: { | |
| SELECT: { | |
| to: 'loading', | |
| action: (state, dispatch) => { | |
| interval.stop() | |
| loadBook().then((book) => dispatch('SELECT_SUCCESS', book)) | |
| return { ...state, title: null, progress: 0 } | |
| }, | |
| }, |
This file contains hidden or 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 usePrevious(value) { | |
| const ref = useRef() | |
| useEffect(() => { | |
| ref.current = value | |
| }, [value]) | |
| return ref.current | |
| } |
This file contains hidden or 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 blockOne = (funcName) => { | |
| if (funcName === 'one') { | |
| return false | |
| } | |
| } | |
| const emitEvent = (funcName) => { | |
| console.log('[event] ' + funcName) | |
| } |
This file contains hidden or 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: (_) => ({ ..._ }) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | |
| } |