const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n)
minN([1, 2, 3]) // [1]
minN([1, 2, 3], 2) // [1,2]
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n)
maxN([1, 2, 3]) // [3]
maxN([1, 2, 3], 2) // [3,2]
const radsToDegrees = (rad) => (rad * 180.0) / Math.PI
radsToDegrees(Math.PI / 2) // 90
const degreesToRads = (deg) => (deg * Math.PI) / 180.0
degreesToRads(90.0) // ~1.5708
const randomHexColorCode = () => {
let n = (Math.random() * 0xfffff * 1000000).toString(16)
return '#' + n.slice(0, 6)
}
randomHexColorCode() // "#e34155"
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
randomIntegerInRange(0, 5) // 3
Read a file by getting an array of lines from a file.
const fs = require('fs')
const readFileLines = (filename) => fs.readFileSync(filename).toString('UTF8').split('\n')
let arr = readFileLines('test.txt')
console.log(arr) // ['line1', 'line2', 'line3']
const reverseString = (str) => [...str].reverse().join('')
reverseString('foobar') // 'raboof'
const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`)
round(1.005, 2) // 1.01
const runPromisesInSeries = (ps) => ps.reduce((p, next) => p.then(next), Promise.resolve())
const delay = (d) => new Promise((r) => setTimeout(r, d))
runPromisesInSeries([() => delay(1000), () => delay(2000)])
// Executes each promise sequentially, taking a total of 3 seconds to complete
const scrollToTop = () => {
const c = document.documentElement.scrollTop || document.body.scrollTop
if (c > 0) {
window.requestAnimationFrame(scrollToTop)
window.scrollTo(0, c - c / 8)
}
}
scrollToTop()
const smoothScroll = (element) =>
document.querySelector(element).scrollIntoView({
behavior: 'smooth',
})
smoothScroll('#fooBar') // scrolls smoothly to the element with the id fooBar
smoothScroll('.fooBar') // scrolls smoothly to the first element with a class of fooBar
const shuffle = ([...arr]) => {
let m = arr.length
while (m) {
const i = Math.floor(Math.random() * m--)
;[arr[m], arr[i]] = [arr[i], arr[m]]
}
return arr
}
const foo = [1, 2, 3]
shuffle(foo) // [2, 3, 1], foo = [1, 2, 3]
const similarity = (arr, values) => arr.filter((v) => values.includes(v))
similarity([1, 2, 3], [1, 2, 4]) // [1, 2]
const sortCharactersInString = (str) => [...str].sort((a, b) => a.localeCompare(b)).join('')
sortCharactersInString('cabbage') // 'aabbceg'
const uniqueElements = (arr) => [...new Set(arr)]
uniqueElements([1, 2, 2, 3, 4, 4, 5]) // [1, 2, 3, 4, 5]
const union = (a, b) => Array.from(new Set([...a, ...b]))
union([1, 2, 3], [4, 3, 2]) // [1,2,3,4]
const validateNumber = (n) => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n
validateNumber('10') // true
const words = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean)
words('I love javaScript!!') // ["I", "love", "javaScript"]
words('python, javaScript & coffee') // ["python", "javaScript", "coffee"]
const bottomVisible = () =>
document.documentElement.clientHeight + window.scrollY >=
(document.documentElement.scrollHeight || document.documentElement.clientHeight)
bottomVisible() // true
const capitalize = ([first, ...rest]) => first.toUpperCase() + rest.join('')
capitalize('fooBar') // 'FooBar'
capitalize('fooBar', true) // 'FooBar'
const decapitalize = ([first, ...rest]) => first.toLowerCase() + rest.join('')
decapitalize('FooBar') // 'fooBar'
decapitalize('FooBar') // 'fooBar'
const capitalizeEveryWord = (str) => str.replace(/\b[a-z]/g, (char) => char.toUpperCase())
capitalizeEveryWord('hello world!') // 'Hello World!'
const compact = (arr) => arr.filter(Boolean)
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34])
// [ 1, 2, 3, 'a', 's', 34 ]
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0)
countOccurrences([1, 1, 2, 1, 2, 3], 1) // 3
const dayOfYear = (date) => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24)
dayOfYear(new Date()) // 272
const deepFlatten = (arr) => [].concat(...arr.map((v) => (Array.isArray(v) ? deepFlatten(v) : v)))
deepFlatten([1, [2], [[3], 4], 5]) // [1,2,3,4,5]
const difference = (a, b) => {
const s = new Set(b)
return a.filter((x) => !s.has(x))
}
difference([1, 2, 3], [1, 2, 4]) // [3]
const digitize = (n) => [...`${n}`].map((i) => parseInt(i))
digitize(431) // [4, 3, 1]
const drop = (arr, n = 1) => arr.slice(n)
drop([1, 2, 3]) // [2,3]
drop([1, 2, 3], 2) // [3]
drop([1, 2, 3], 42) // []
const dropRight = (arr, n = 1) => arr.slice(0, -n)
dropRight([1, 2, 3]) // [1,2]
dropRight([1, 2, 3], 2) // [1]
dropRight([1, 2, 3], 42) // []
const elementContains = (parent, child) => parent !== child && parent.contains(child)
elementContains(document.querySelector('head'), document.querySelector('title')) // true
elementContains(document.querySelector('body'), document.querySelector('body')) // false
const filterNonUnique = arr => [ …new Set(arr)];
filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5]
const getColonTimeFromDate = (date) => date.toTimeString().slice(0, 8)
getColonTimeFromDate(new Date()) // "08:38:00"
const getDaysDiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateInitial) / (1000 * 3600 * 24)
getDaysDiffBetweenDates(new Date('2019-01-13'), new Date('2019-01-15')) // 2
const getType = (v) => (v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase())
getType(new Set([1, 2, 3])) // 'set'
const indexOfAll = (arr, val) => arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), [])
indexOfAll([1, 2, 3, 1, 2, 3], 1) // [0,3]
indexOfAll([1, 2, 3], 4) // []
const intersection = (a, b) => {
const s = new Set(b)
return a.filter((x) => s.has(x))
}
intersection([1, 2, 3], [4, 3, 2]) // [2, 3]
const is = (type, val) => ![, null].includes(val) && val.constructor === type
is(Array, [1]) // true
is(ArrayBuffer, new ArrayBuffer()) // true
is(Map, new Map()) // true
is(RegExp, /./g) // true
is(Set, new Set()) // true
is(WeakMap, new WeakMap()) // true
is(WeakSet, new WeakSet()) // true
is(String, '') // true
is(String, new String('')) // true
is(Number, 1) // true
is(Number, new Number(1)) // true
is(Boolean, true) // true
is(Boolean, new Boolean(true)) // true
const isBrowser = () => ![typeof window, typeof document].includes('undefined')
isBrowser() // true (browser)
isBrowser() // false (Node)
const isBrowserTabFocused = () => !document.hidden
isBrowserTabFocused() // true
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n)
}
isNumber('1') // false
isNumber(1) // true
- Ayush 🙂