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 createEnum(labels, startIndex = 0) { | |
return Object.freeze(labels.reduce((enu, label, i) => {enu[label] = i + startIndex; return enu;}, {})); | |
} | |
const ERRORS = util.createEnum(['OK', 'UNKNOWN_ERROR', 'SERVER_ERROR']); | |
console.log(ERRORS.OK); |
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 var1 = "string1"; | |
const var2 = 7; | |
console.log({var1, var2}); // is much better than | |
console.log(var1, var2); | |
// because it outputs both the name of variables and their values. |
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
// There is a limit for uploading content in axios library by default. | |
// To avoid such a "feature", and be able to upload large files add to options: | |
options.maxBodyLength = Infinity; | |
options.maxContentLength = Infinity; |
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
process.stdin.setRawMode(true); | |
process.stdin.resume(); | |
process.stdin.on('data', process.exit.bind(process, 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
package main | |
import "fmt" | |
func main() { | |
const ( | |
C8 = iota * 2 + 8 | |
C10 | |
C12 | |
C14 |
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
#!/bin/bash | |
cmds=(cat tac head tail tee more less echo wc grep find sort comm tr cut split sed awk seq) | |
for cmd in $cmds; do | |
man $cmd|sed -n '4p'|sed -E 's/^\s+//;s/ - /\n /' | |
done |
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
// Brute-force solution of the Zebra Puzzle | |
// https://code.energy/solving-zebra-puzzle/ | |
// | |
// Result: | |
// Norwegian from 1 house drinks water. | |
// Japanese from 5 house owns zebra. | |
package main | |
import ( |
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
// Test task: check if brackets string balanced | |
const bracketsMatch = { | |
'{': '}', | |
'[': ']', | |
'(': ')', | |
} | |
function isBalanced(brackets) { | |
const stack = [] |
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
#!/usr/bin/env node | |
function createFizzBuzzGenerator() { | |
const values = Array(16).fill(null).map((_, i) => { | |
const x = i + 1 | |
if (x % 3 !== 0 && x % 5 !== 0) | |
return x | |
let result = '' | |
if (x % 3 === 0) | |
result += 'Fizz' |
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
// Nullish coalescing | |
// Нулевое слияние | |
// if data is undefined | |
const getUserName = (user) => { | |
return user?.name ?? "Anonymous"; | |
}; | |
// will display `fallbackData` | |
// if data is false-ish (e.g., null, undefined, '', 0, etc.) | |
console.log(data || fallbackData); |
OlderNewer