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
import { createQueue } from './queue.js'; | |
const fakeAsyncCall = (i: number) => | |
new Promise<void>((resolve) => | |
setTimeout(() => { | |
console.log(i); | |
resolve(); | |
}, 1000) | |
); |
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 classNames = ( | |
...classes: Array<string | string[] | Record<string]: boolean }> | |
): string | undefined => classes | |
.map(cls => cls?.constructor.name === 'Object' ? Object.keys(cls).filter(key => cls[key]) : cls) | |
.flat() | |
.filter(Boolean) | |
.join(' ') || undefined; |
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
if (!document.elementsFromPoint) { | |
document.elementsFromPoint = | |
document.msElementsFromPoint || | |
function elementsFromPoint(x, y) { | |
var stack = []; | |
var element = document.elementFromPoint(x, y); | |
try { | |
while (element !== null) { | |
stack.push({ |
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
/** | |
* Generate a random UUIDv4 (rfc4122 compliant) | |
*/ | |
export function uuid(): string { | |
const uuid = [8, 4, 4, 4, 12].map((segmentLength: number) => { | |
let segment = Array(segmentLength); | |
for (let i = 0; i < segmentLength; i++) | |
// ToUint32 http://www.ecma-international.org/ecma-262/5.1/#sec-11.7.3 | |
segment[i] = (Math.random() * 0xf) >>> 0; |
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
{ | |
"trailingComma": "none", | |
"tabWidth": 4, | |
"semi": true, | |
"singleQuote": true, | |
"printWidth": 120 | |
} |
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
# editorconfig.org | |
root = true | |
[*] | |
indent_style = space | |
indent_size = 4 | |
end_of_line = lf | |
charset = utf-8 | |
trim_trailing_whitespace = true | |
insert_final_newline = true |
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 fibonacci(n) { | |
const delta = Math.sqrt(5); | |
return ( | |
(Math.pow(1 + delta, n) - Math.pow(1 - delta, n)) / (Math.pow(2, n) * delta) | |
); | |
} |
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
{ | |
"server": { | |
"maxConnections": 100, | |
"host": "0.0.0.0", | |
"port": 10000 | |
}, | |
"chat": { | |
"messageLength": 150, | |
"messages": { | |
"welcome": "Welcome to chat server. Press CTRL+C to leave.", |
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 flat = arr => { | |
// Create a copy of the original array | |
const result = [...arr]; | |
for (let i = 0; i < result.length; i++) { | |
while (Array.isArray(result[i])) { | |
// insert in the current position a copy of the original item | |
result.splice(i, 1, ...result[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 encodeNumber = (n, precision) => { | |
// Multiple number by 10^precision to round the result | |
n = Math.round(n * +`1e${precision}`); | |
/** | |
* Number is already in two's complement so just shift left | |
* by 1-bit and negate if and only if number is negative | |
*/ | |
n = n < 0 ? ~(n << 1) : n << 1; |
NewerOlder