Skip to content

Instantly share code, notes, and snippets.

View mogelbrod's full-sized avatar

Victor Hallberg mogelbrod

View GitHub Profile
@mogelbrod
mogelbrod / babel.config.js
Last active December 5, 2023 22:18
Prevent core-js polyfills of APIs that should be supported by IE11 for general use (incomplete list)
module.exports = {
presets: [
['@babel/preset-env', {
useBuiltIns: 'usage',
debug: false,
loose: true,
corejs: {
version: 3,
proposals: true,
},
@mogelbrod
mogelbrod / keybindings.json
Last active April 13, 2023 11:47
VSCode settings
[
// Focus previous/next split using cmd (mac) or ctrl (others) + left/right
{
"key": "ctrl+left",
"command": "workbench.action.focusPreviousGroup",
"when": "!isMac && editorTextFocus && vim.active && vim.mode != 'Insert'",
},
{
"key": "cmd+left",
"command": "workbench.action.focusPreviousGroup",
@mogelbrod
mogelbrod / document-element.js
Last active July 13, 2022 21:43
useVirtualScrollParent() hook using useVirtual() from react-virtual
const html = document.documentElement
const body = document.body
/**
Pseudo HTML element which behaves like a regular element in terms of scrolling.
*/
const documentElement = {
set scrollLeft(x) { html.scrollLeft = x },
get scrollLeft() { return window.scrollX || window.pageXOffset },
set scrollTop(x) { html.scrollTop = x },
@mogelbrod
mogelbrod / .eslintrc.js
Last active November 1, 2021 12:52
[@graphql-eslint] Specifying schema via HTTP + local graphql file
module.exports = {
root: true,
overrides: [
{
files: ['*.graphql'],
plugins: ['@graphql-eslint'],
parser: '@graphql-eslint/eslint-plugin',
parserOptions: {
operations: './query.graphql',
},
@mogelbrod
mogelbrod / weighted-choice.js
Created February 16, 2016 09:39
Weighted random choice in JavaScript, takes an array of non-negative weights
function weightedChoice(weights) {
const weightSum = weights.reduce((sum, w) => sum + w)
let choice = Math.floor(Math.random() * weightSum) + 1
let idx = weights.length - 1
while ((choice -= weights[idx]) > 0) {
idx -= 1
}
return idx
}
@mogelbrod
mogelbrod / input.scss
Created May 24, 2021 16:05
Generated by SassMeister.com.
$breakpoints: 0 400px 600px 720px 1000px 1280px 1600px !default;
$scale-names: c xs s m l xl hd !default;
$predefined-spacing: 2px 4px 8px 12px 16px 32px 48px;
@function breakpoint($input, $subtract: false) {
$value: $input;
@if type-of($value) == string {
$value: index($scale-names, $input);
@mogelbrod
mogelbrod / .postcssrc
Created March 27, 2021 15:53
[Parcel v2 & autoprefixer] reproduction of failing builds
{
"modules": true,
"plugins": {
"autoprefixer": {}
}
}
@mogelbrod
mogelbrod / portal.js
Last active May 22, 2020 10:23
Non-bubbling React Portals
import PropTypes from 'prop-types'
import React from 'react'
import ReactDOM from 'react-dom'
function portalContainer() {
return document.getElementById('portal-container')
}
// Taken from https://reactjs.org/docs/events.html
@mogelbrod
mogelbrod / caesar-shift.js
Created October 4, 2016 19:56
Caesar cipher in JavaScript (ES6)
/**
* Applies a Caesar shift to all alphabetical characters in a string.
* Characters are converted to uppercase in the returned string.
* @param {int} key - Positions to shift (0-25).
* @param {string} str - The string to shift.
* @return {string} The shifted string.
*/
function caesarShift(str, key) {
return str.toUpperCase().replace(/[A-Z]/g, c => String.fromCharCode((c.charCodeAt(0)-65+key)%26+65))
}
@mogelbrod
mogelbrod / index.ts
Created December 30, 2019 11:12
vim/ycm long completion popup squished by long menu
// Put cursor v there and wait until YCM+tsserver has initialized
const x = [].