Skip to content

Instantly share code, notes, and snippets.

@edulan
edulan / index.html
Created July 3, 2020 17:41
Sticky scroll spy example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sticky scroll spy example</title>
<style>
html {
scroll-behavior: smooth;
@edulan
edulan / index.js
Created May 18, 2020 23:54
Custom base32 generator
const ALPHABET = "RKYM5C4H07P9EGWJ1BZQ8V3S62FXNTDA"
const BASE_LENGTH = ALPHABET.length
const SHORT_CODE_LENGTH = 6
function toBase (input) {
let number = input
if (number === 0) {
return ALPHABET[0]
}
#!/usr/bin/env node
const readline = require("readline");
const lines = [];
function sortBooks(a, b) {
if (a.score > b.score) return 1;
if (a.score < b.score) return -1;
return 0;
}
@edulan
edulan / solve.js
Last active February 19, 2020 20:20
#!/usr/bin/env node
require("lodash.combinations");
const { flatMap, combinations } = require("lodash");
const readline = require("readline");
const lines = [];
function processInput(lines) {
const [firstLine, lastLine] = lines;
@edulan
edulan / primes.js
Created July 19, 2018 09:10
Sieve of Eratosthenes
function * generateMultiplesOf (number) {
for (let factor = 2;; factor++) {
yield number * factor
}
}
function * generatePrimes (maxNumber = 1000) {
const firstPrime = 2
const nonPrimeNumbers = new Set()
@edulan
edulan / rx_photobooth.js
Last active April 19, 2018 15:32
Rx photobooth, take 4 photos with a 5 seconds countdown between them
const takePhoto = () => new Promise((resolve) => (setTimeout(resolve, Math.random() * 1E3 + 5E2, 'photo')))
const makeCountdown = (seconds) =>
Rx.Observable
.concat(
Rx.Observable
.interval(1E3)
.take(seconds),
Rx.Observable.defer(() => takePhoto())
)
@edulan
edulan / rx_take_latest.js
Created November 28, 2017 18:07
Rx take latest
const simulateTask = () => new Promise((resolve) => (setTimeout(resolve, Math.random() * 1E3 + 500)))
const click$ = Rx.Observable
.fromEvent(document, 'click')
.scan((clickCount) => clickCount + 1, 0)
click$.switchMap(
() => Rx.Observable.fromPromise(simulateTask()),
(clickCount) => clickCount
)
@edulan
edulan / app.js
Last active October 22, 2017 22:28
App component
class App extends PureComponent {
constructor (props) {
super(props)
this.state = {
term: props.term,
items: [],
errorMessage: null
}
@edulan
edulan / idle_timer.js
Last active October 2, 2017 16:03
Idle timer
function createIdleTimer (delay = 5E3) {
const IDLE_POLL_TIMEOUT = 1E2
const events = ['mousemove', 'mousedown', 'keydown', 'touchstart', 'scroll']
return new Promise((resolve) => {
let idle = true
let timeout = null
let lastTimestamp = null
let elapsedTimeInMillis = 0
@edulan
edulan / sliceBy.js
Last active August 1, 2017 10:15
Slice a string by given chunk sizes
function sliceBy(str, [ chunkSize = -1, ...rest ] = []) {
// Guard against infinite recursion
if (chunkSize < 1) {
return [str]
}
if (str.length <= chunkSize) {
return [str]
}