Skip to content

Instantly share code, notes, and snippets.

View eugenserbanescu's full-sized avatar

Eugen Serbanescu eugenserbanescu

View GitHub Profile
@eugenserbanescu
eugenserbanescu / arrays.js
Last active July 30, 2022 10:27
Javascript array functions
let list = [1,2,3,4];
let forEachCalled = 0;
list.forEach(item => {
// this one does something for every item in the array
// and returns the original array
forEachCalled++;
});
const mapped = list.map(item => {
@eugenserbanescu
eugenserbanescu / async-mapping.js
Last active April 4, 2019 08:51
async mapping
async function waitForStuff(i) {
return new Promise((resolve) => {
setTimeout(() => resolve(`yay!`), 100)
})
}
async function main() {
const mapped = Promise.all([1,2,3,4].map(async (item) => {
const res = await waitForStuff()
return res
const input = [
[ 1, 2, 3 ],
[ 4, 5 ],
[ 6, 7, 8, 9 ],
[ 10 ],
[ 11, 12 ],
[ 13, 14, 15, 16, 17 ]
]
const outputLimit = 12
const chunkLimit = 2
@eugenserbanescu
eugenserbanescu / sticky-table.html
Last active November 24, 2018 13:51
A sticky table html & css. Needed for reasons - works in Edge it seems
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: sans-serif;
}
.results-wrapper {
height: 200px;
width: 300px;
@eugenserbanescu
eugenserbanescu / semicolons.js
Created July 25, 2018 12:42
Two scenarios where semicolons would prevent an error
try {
let obj = {a: 0}
let a = obj
[0,1,2].forEach(item => console.log(item))
} catch (e) {
console.log('First scenario', e) // throws TypeError: Cannot read property 'forEach' of undefined
}
try {
function getFilesInDirSync(pathToDir, files = []) {
const dirContents = fs.readdirSync(pathToDir);
return files.concat(dirContents.map(item => {
const isDir = fs.statSync(pathToDir + item).isDirectory()
return isDir ? getFilesInDirSync(`${pathToDir}/{item}`, files) : [`${pathToDir}/{item}`]
}))
})
@eugenserbanescu
eugenserbanescu / svg-path
Created July 3, 2018 16:31
X-Y svg path mover
const PAIR_MATCHER = /([A-Z]?)([^A-Za-z,]+),(.*)/
function moveSvg(path, x=0, y=0) {
return path.split(' ').map(pair => {
const matches = pair.match(PAIR_MATCHER)
return matches === null ?
pair :
`${matches[1]}${parseFloat(matches[2]) + x},${parseFloat(matches[3]) + y}`
}).join(' ')
}
<html>
<head>
<style>
.wrapper {
background: red;
position: relative;
margin: 100px;
width:100px;
height: 100px;
}
@eugenserbanescu
eugenserbanescu / fibonacci.js
Last active April 3, 2019 17:02
First stab at Fibonacci
const fibonacci = (limit, list=[1, 1]) => {
const currentIndex = list.length - 1
const newNumber = list[currentIndex - 1] + list[currentIndex]
if( newNumber >= limit) {
return list
} else {
return fibonacci(limit, list.concat([newNumber]), currentIndex + 1)
}
}
function compare(a,b) {
return a == b;
}
function compareStrict(a,b) {
return a === b;
}
let a = {a:1}