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 memoize = (func) => { | |
| const results = {}; | |
| return (...args) => { | |
| const argsKey = JSON.stringify(args); | |
| if (!results[argsKey]) { | |
| results[argsKey] = func(...args); | |
| } | |
| return results[argsKey]; | |
| }; | |
| }; |
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
| // https://jrsinclair.com/articles/2019/functional-js-traversing-trees-with-recursive-reduce/ | |
| const menu1 = [ | |
| { | |
| type: 'title', | |
| text: 'Works of George Macdonald', | |
| }, | |
| { | |
| type: 'link', | |
| href: '/books', |
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
| // https://zellwk.com/blog/async-await-in-loops/ | |
| const fruitBasket = { | |
| apple: 27, | |
| grape: 0, | |
| pear: 14, | |
| }; | |
| const sleep = (ms) => { | |
| return new Promise((resolve) => setTimeout(resolve, ms)); |
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 assert = (val: boolean) => { | |
| if (!val) { | |
| throw new Error('false'); | |
| } | |
| return true; | |
| }; | |
| const mergeArray = <T>(key: keyof T, ...arrays: T[][]): T[] => { | |
| return Object.values( | |
| arrays.flat().reduce((acc, curr) => { |
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 total = (arr) => arr.reduce((acc, v) => acc + v); | |
| console.log(total([1, 2, 3])); // 6 | |
| /**====================================================================== */ | |
| const stringConcat = (arr) => arr.reduce((acc, v) => `${acc}${v}`); | |
| console.log(stringConcat([1, 2, 3])); // "123" | |
| /**====================================================================== */ | |
| const totalVotes = (arr) => { | |
| return arr.reduce((acc, curr) => (curr.voted ? acc + 1 : acc), 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 pubSub() { | |
| const subscribers = {}; | |
| function publish(eventName, data) { | |
| if (!Array.isArray(subscribers[eventName])) { | |
| return; | |
| } | |
| subscribers[eventName].forEach((callback) => { | |
| callback(data); | |
| }); |
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 ReactDOMServer = require('react-dom/server'); | |
| const render = (reactComponent) => { | |
| return new Promise((resolve, reject) => { | |
| const body = []; | |
| const bodyStream = ReactDOMServer.renderToNodeStream(reactComponent); | |
| bodyStream.on('data', (chunk) => { | |
| body.push(chunk.toString()); | |
| }); | |
| bodyStream.on('error', (err) => { |
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 numbers = [10, 20, 30, 40]; | |
| const numbersTimes10 = numbers.map((v) => v * 10); | |
| numbersTimes10; | |
| const people = [ | |
| { | |
| first: 'Jane', | |
| last: 'Doe', | |
| address: '123 Main St', | |
| city: 'New York', |