Skip to content

Instantly share code, notes, and snippets.

@everdimension
everdimension / parse_search.js
Created December 3, 2015 14:59
A function to parse search string params into javascript key-value object.
// sample data for trying
var paramsString = "?lunch=sandwich&dinner=stirfry";
console.log(parseParams(paramsString));
//
// main function
//
function parseParams(str) {
return str
.replace(/(^\?)/, '')
@everdimension
everdimension / redux-api_failing_test.js
Last active January 28, 2016 15:49
Test code demonstrating that two stores created using redux-api fail to listen to the same action.
// DrawerApi.js
// ** code from demo ommitted **
//
// AnotherApi.js
import Api from '../src/redux-apis';
export default class AnotherApi extends Api {
constructor(state = { drawerIsOpened: false }) {
super(state);
class Store {
constructor(reducer, initialState) {
this.reducer = reducer;
this.state = this.reducer(initialState, {});
console.log('set state', this.state);
this.listeners = [];
}
getState() {
return this.state;
@everdimension
everdimension / svg-base-fix.js
Last active February 15, 2016 12:27
An angular directive making svg's work when <base href="/path"> tag is present.
angular.module('app.directives.svgBaseFix', [])
.directive('svgBaseFix', function($rootScope) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var attr = 'xlinkHref';
var initialUrl = attrs[attr];
var parsingNode = document.createElement('a');
@everdimension
everdimension / accessibility_outline.js
Created November 11, 2016 22:38
A simple trick (subject to improvement) to hide outline until the user starts interacting with the site with the keyboard.
/* global document */
import { KEY_CODES } from './data/constants';
export default function initTabSpy() {
document.body.classList.add('isUsingPointer');
document.addEventListener('keydown', (evt) => {
if (evt.which === KEY_CODES.TAB) { // or may be any keydown?
document.body.classList.remove('isUsingPointer');
}
});
@everdimension
everdimension / react_component.sh
Created February 9, 2017 09:40
A shell command for creating a folder with with files for a self-contained component.
#!/bin/bash
# Creates the following structure
# | - ComponentName
# | - ComponentName.js
# | - ComponentName.css
# | - index.js
createComponent() {
mkdir $1

Keybase proof

I hereby claim:

  • I am everdimension on github.
  • I am everdimension (https://keybase.io/everdimension) on keybase.
  • I have a public key ASDZ-VkHtJ_900M-VqWOsuxU_ZJPPpICcOXL6n6qnecOhQo

To claim this, I am signing this object:

@everdimension
everdimension / isNumericValue.js
Created September 8, 2017 17:44
Robust way to check if a value (number or string, usually from user input) can be considered a number.
function isNumericValue(n) {
return !isNaN(Number(n) - parseFloat(n));
}
const ActuallyUsedComponent = () => (
<div>
/* some layout and markup that doesn't depend on global state */
<Connect mapStateToProps={mapStateToProps} mapPropsToActions={actions}>
/* some markup where the props from connect are actually needed */
</Connect>
<Route
path="..."
component={() => (
<div>
@everdimension
everdimension / sumOfCombinations.js
Created January 18, 2018 22:50
A function that finds all combinations of numbers in an array the sum of which equals a certain number
function arraySum(arr) {
return arr.reduce((sum, next) => sum + next, 0);
}
function padLeft(string, size) {
let res = string;
while (res.length < size) {
res = `0${res}`;
}
return res;