Skip to content

Instantly share code, notes, and snippets.

View karolk's full-sized avatar
💭
cooking on gas

Karol K karolk

💭
cooking on gas
View GitHub Profile
@karolk
karolk / testing-tpt.md
Last active January 26, 2022 19:41
Testing TPT

Testing TPT

Electron 24/01/2022

LOGIN

  • login free
  • login premium
  • remember password
  • not remember password
@karolk
karolk / await-vs-then.md
Created September 28, 2020 20:03
await vs. then

I recently run into a scenario highlighting that await is not always a drop-in, better replacement for then. Sometimes with then it's easier to control the scope of the function that runs after the promise is settled. I have a Redux middleware-inspired example here:

https://codesandbox.io/s/eager-wildflower-1kodv?file=/src/index.js

In example 2, the next call will only happen after the promise resolves.

In example 3, the next will be called immediately.

@karolk
karolk / have-same-keys1.js
Last active June 23, 2020 15:36
Do 2 objects have the same keys - 2 ways
const haveSameKeys = (objA, objB) => {
const keysOfA = Object.keys(objA)
const keysOfB = Object.keys(objB)
return keysOfA.length === keysOfB.length &&
keysOfA.every(key => keysOfB.includes(key))
}
@karolk
karolk / letter.js
Created April 25, 2020 21:27
vanilla js implementation of letters
// @ts-check
const createRandomInteger = (limit) => Math.floor(Math.random() * limit)
const createRandomLetter = () => String.fromCharCode(97 + createRandomInteger(26));
const positionLetter = (element) => {
element.style.width = '1vw'
element.style.height = '1vw'
element.style.lineHeight = '1vw'
@karolk
karolk / espot-track.js
Last active April 26, 2019 08:54
Click and impression tracking code for espots
if (typeof handleTrackableClickEvent !== "function") {
var handleTrackableClickEvent = function(event) {
var trackableSelector = '.trackable';
if (event.target) {
// msMatches is for IE
var matches = event.target.matches || event.target.msMatches;
if (matches && matches.call(event.target, trackableSelector)) {
var et = event.target;
@karolk
karolk / primes.js
Created March 8, 2019 17:27
Finding primes brute force in finite time
const isPrime = number => {
let divisor = 2
while (divisor < number) {
if(number % divisor === 0) return false
divisor += 1
}
return true
}
const findPrime = (timeLimitMS) => {
@karolk
karolk / proto-version.js
Last active June 7, 2018 10:34
versions in prototype chain
// IDEA: start with an object using a symbol as a marker
const create = (obj, changeSet) => Object.assign(Object.create(obj || {[Symbol.for('versionstart')]: true}), changeSet);
const getPreviousState = obj => {
const maybePrev = Object.getPrototypeOf(obj);
if (!maybePrev || maybePrev[Symbol.for('versionstart')]) {
return null;
}
return maybePrev;
}
@karolk
karolk / graph-by.js
Last active June 6, 2018 13:02
Graph data by sequential prop
// Given a list with data containing sequential property such as year of birth, age, size
// Create a graph containing elements groupped by that property with ids of next nodes
// Output example
{
'1': {list: [{size: 1}, {size: 1}], previous: undefined, next: '3'},
'3': {list: [{size: 3}], previous: '1', next: '4'},
'4': {list: [{size: 4}], previous: '3', next: undefined},
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
/* these are just to set up environment, you don't need them */
html, body {
height: 100%;
@karolk
karolk / euler1.js
Created October 12, 2017 20:15
Euler 1
Array(999)
.fill(1)
.map((n, index) => n + index)
.filter(n => n % 3 === 0 || n % 5 === 0)
.reduce((a, b) => a + b)