Skip to content

Instantly share code, notes, and snippets.

@rolandcoops
rolandcoops / roundUpBaseInRange.js
Last active November 16, 2017 18:37
Calculate ‘nice’ graph domain by rounding up number through a base-10 range. (ES6)
// Round up to nearest base, in base 10. Works for both negative/positive numbers and numbers -1 < n < 1.
// Useful for calculating a sensible/readible graph domain max if you input the max value of your data.
export const roundUpBaseInRange = (n, bases = [1, 2, 4, 5, 6, 8, 10]) => {
// Store the sign, so we can restore it for the result lateron
const sign = Math.sign(n)
// Get modulus (remove sign) for next steps in calculation
const modulus = Math.abs(n)
// Calculate base10 exponent (i.e. amount of digits as float instead of integer)
// quick idea to reduce an array by automatically merging result of iteratee(current, i, src) onto acc
const reduceMerge = (arr, iteratee) =>
arr.reduce((acc, cur, i, src) => {
const val = iteratee(cur, i, src)
// allow falsy returns to result in noop for this iteration
if (!val) {
return acc
} else if (val.constructor === Object) {
return Object.assign(acc, val)
}
@rolandcoops
rolandcoops / speech-arrow-mixin.scss
Last active December 10, 2018 17:43
Speech arrow mixin
@mixin with-speech-arrow (
$align: left,
$size: 20px,
$border-color: #eee
) {
&:before,
&:after {
content: '';
position: absolute;
border-color: transparent;
@rolandcoops
rolandcoops / webpack.config.js
Last active May 6, 2019 10:50
Idea for explicitly splitting out chunks for specific groups of vendor modules
const { configureCacheGroups } = require('./webpack.utils')
const cacheGroups = configureCacheGroups({
'react': ['react', 'react-dom'],
'lodash': ['lodash'],
'redux': ['redux', 'react-redux'],
'semantic-ui': ['semantic-ui-react'],
'core-js': ['core-js'],
})
/**
* Add an inset border (in a single direction) to an element
* Examples:
* @include inset-border(bottom)
* @include inset-border(right, 3px, red)
*/
@mixin inset-border ($direction, $width: 1px, $color: black, $opacity: 0.05) {
$y: if($direction == bottom, -$width, if($direction == top, $width, 0));
$x: if($direction == right, -$width, if($direction == left, $width, 0));
box-shadow: inset #{$x} #{$y} 0 0 rgba($color, $opacity);
class WeightedQuickUnionFind {
constructor (ids) {
this._map = new Map() // id-to-component map
this._sizes = new Map() // subtree size map
this.length = ids.length // component count
ids.forEach((id) => {
this._map.set(id, id)
this._sizes.set(id, 1)
})
@rolandcoops
rolandcoops / Item.jsx
Last active August 19, 2019 09:52
Normalized redux structure example (see https://stackoverflow.com/a/37266130/8602065)
import React from 'react'
import { connect } from 'react-redux'
/**
* Item Component
*/
const Item = ({ item }) => (
<li className="item">
<div>
@rolandcoops
rolandcoops / mergeSort.js
Created September 24, 2019 07:39
Merge sort implementation example (adapted from “Algorithms, Part I” course)
// Adapted from “Algorithms, Part I” course by “Princeton University” on coursera.org
const defaultCompare = (a, b) =>
a < b ? -1 : 1
const merge = (arr, aux, compare, low, mid, high) => {
// left and right half scan indices
let l = low, r = mid + 1
// copy into auxillary array
@rolandcoops
rolandcoops / hexify.js
Last active September 24, 2019 09:04
Convert a string to css hex values suitable for use in css “content” prop
/**
* convert a string to css hex values suitable for use in css “content” prop
* @example
* hexify('foo@bar') // '\0066\006f\006f\0040\0062\0061\0072'
*/
const hexify = (string) =>
[...string]
.map((c) => `\\${Number(c.charCodeAt()).toString(16).padStart(4, '0')}`)
.join('')
@rolandcoops
rolandcoops / TypeScriptRedux.ts
Last active October 22, 2019 09:02
Simple TypeScript implementation of Redux
/**
* Types & Interfaces
*/
type State = { [key: string]: any }
type Reducer = (currentState: State, action: Action) => State;
type Listener = (currentState?: State) => void;