- 1
- 2
- 3
- 4
- 3
- 2
- 5
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 getCaller() { | |
| const stack = getStack(); | |
| stack.shift(); // getStack | |
| stack.shift(); // getCaller | |
| // Return caller's caller | |
| return stack; | |
| } |
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
| class ArrayLazy extends Array { | |
| _lazyQueue = []; | |
| map(callback) { | |
| this._lazyQueue.push({ type: "map", callback }); | |
| return this; | |
| } | |
| filter(callback) { | |
| this._lazyQueue.push({ type: "filter", callback }); | |
| return this; | |
| } |
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 getStack() { | |
| // Save original Error.prepareStackTrace | |
| const origPrepareStackTrace = Error.prepareStackTrace; | |
| // Override with function that just returns `stack` | |
| Error.prepareStackTrace = (_, stack) => stack; | |
| // Create a new `Error`, which automatically gets `stack` | |
| // Evaluate `err.stack`, which calls our new `Error.prepareStackTrace` | |
| const { stack } = new Error(); |
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
| // @flow | |
| function match<I: [*] | [*, *], O>( | |
| cb: (test: (predicate?: (...a: I) => any) => string) => { [string]: O | ((...a: I) => O) } | |
| ): (...a: I) => ?O { | |
| const predicates = {}; | |
| function test(predicate: (...a: I) => any = () => true): string { | |
| const predicateName = Math.random().toFixed(5); | |
| predicates[predicateName] = predicate; | |
| return predicateName; |
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 validation = store => next => action => { | |
| switch(action.type){ | |
| case loginFormSubmit.type: { | |
| const isValid = validate(action.payload); | |
| if (isValid) return next(action) | |
| // else | |
| store.dispatch(loginFormIvalid()) | |
| } | |
| default: return next(action) | |
| } |
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
| import { createStore as createReduxStore } from 'redux' | |
| import { devToolsEnhancer } from 'redux-devtools-extension' | |
| const redux = createReduxStore((state, { payload }) => payload, devToolsEnhancer()) | |
| export function useReduxDevTool(store) { | |
| store.watch(payload => redux.dispatch({ type: 'CHANGE', payload })) | |
| } |
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 createMapById<PropName extends string>( | |
| $list: Store<{ [key in PropName]: any }[]>, | |
| propName: PropName, | |
| id: string | |
| ) { | |
| return $list.map(list => | |
| list.reduce( | |
| (propByIdMap, element) => ( | |
| (propByIdMap[element[id]] = element[propName]), propByIdMap |
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
| window.onerror = function (message, source, lineno, colno, error) { | |
| if (typeof error === 'object' && error instanceof Error) { | |
| try { | |
| error = JSON.stringify( | |
| error, | |
| // по умолчанию поля ошибок не парсятся | |
| Object.getOwnPropertyNames(error), | |
| ); | |
| } catch (_) { | |
| error = `${error.name}\n${error.message}\n${error.stack}`; |
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 { performance } = require('perf_hooks'); | |
| let counter = 0; | |
| let log = []; | |
| function logPure(log, value) { | |
| log.push(value); | |
| } | |
| function logDirty() { |