Created
April 17, 2022 17:47
-
-
Save SanariSan/9c34e5fa635f9a293040f505a2649310 to your computer and use it in GitHub Desktop.
JS utils
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 { appendFileSync } from 'fs'; | |
const log = (str) => console.log(str); | |
const debugLog = (p) => (process.env.NODE_ENV === 'development' ? console.log(p) : false); | |
const strError = (e, hint) => | |
`Error : ${hint} : ${e.message}\n${e.stack.split('\n').slice(1, 3).join('\n')}`; | |
const dir = (p) => console.dir(p, { depth: 10 }); | |
const debugDir = (p) => | |
process.env.NODE_ENV === 'development' | |
? console.dir(p, { depth: 2 }) | |
: process.env.NODE_ENV === 'development-v' | |
? (() => { | |
console.dir(p, { depth: 10 }); | |
appendFileSync('./debug.log', `${JSON.stringify(p)}\n`); | |
})() | |
: false; | |
const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); | |
const rndInRange = (min = 0, max = min + 1) => Math.random() * (max - min) + min; | |
const makeTimeHR = (ms) => { | |
const date = new Date(ms); | |
return `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}`; | |
}; | |
const clearScreen = () => process.stdout.write('\033c'); | |
const setTerminalTitle = (title) => | |
process.stdout.write(`${String.fromCharCode(27)}]0;${title}${String.fromCharCode(7)}`); | |
const isValidNum = (n) => n !== null && n !== undefined && !isNaN(n); | |
const makeEnum = (obj) => { | |
const shallow = { ...obj }; | |
Object.entries(shallow).forEach(([key, value]) => (shallow[value] = key)); | |
return shallow; | |
}; | |
const randomHex = (length = 16) => new Promise((resolve, reject) => { | |
randomBytes(length, (err, buffer) => { | |
if (err) reject(err); | |
resolve(buffer.toString('hex').slice(0, length)); | |
}); | |
}); | |
const randomHexSync = (length = 16) => randomBytes(length).toString('hex').slice(0, length); | |
const duplicateNTimes(n, str) => { | |
let output = ``; | |
for (let i = 0; i < n; i += 1) { output += str; } | |
return output; | |
} | |
function isPalindrome<T>(arr: readonly T[]) { | |
for (let i = 0, k = arr.length - 1; i < Math.floor(arr.length / 2); i += 1, k -= 1) { | |
if (arr[i] !== arr[k]) return false; | |
} | |
return true; | |
} | |
function getPermutations<T>(inputArrayGlobal: readonly T[]) { | |
const out: Array<Readonly<T[]>> = []; | |
function recursion(inputArrayLocal: readonly T[], accumulator: readonly T[] = []) { | |
if (accumulator.length === inputArrayGlobal.length) { | |
out.push(accumulator); | |
return; | |
} | |
inputArrayLocal.forEach((el, i) => { | |
const inputArrayLocalShallow = [...inputArrayLocal]; | |
inputArrayLocalShallow.splice(i, 1); | |
recursion(inputArrayLocalShallow, [...accumulator, el]); | |
}); | |
} | |
recursion(inputArrayGlobal); | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment