Skip to content

Instantly share code, notes, and snippets.

@goldhand
goldhand / pseudoStyles.js
Last active March 23, 2017 19:48
Format pseudo styles for a react component
/**
* Pseudo styles
* @param {Object} pseudoStates - state object with keys matching pseudo attributes
* @returns {Object} use(styles)
*
* @example
* const styles = {color: '#000', hover: {color: 'red'}, active: {color: 'green'}};
* const state = {hover: true, active: true};
* pseudoStyles(state)
* .use(styles.button)
@goldhand
goldhand / immutableMove.js
Created March 8, 2017 00:52
Immutable move item in an array
/**
* Immutable move item
*/
const move = (arr, from, to) => {
const clone = [...arr];
Array.prototype.splice.call(clone, to, 0,
Array.prototype.splice.call(clone, from, 1)[0]
);
return clone;
};
@goldhand
goldhand / withKeys.js
Created February 21, 2017 23:46
Add an object key to a child object
/**
* Assigns object keys to the assigned child object
* @returns {Object} keyed object
*/
const withKeys = (obj, uid = 'uid') => {
return Object.keys(obj).reduce((keyObj, key) => {
let keyed;
if (typeof obj[key] === 'object') {
keyed = {...obj[key], [uid]: key};
} else {
@goldhand
goldhand / array-destructure-assignment.js
Created February 15, 2017 17:32
Examples of es6 array destructure assignments
// Array Destructure Assignment Examples
const myArray = ['a', ['b', ['c']], ['d']];
const oldWayA = myArray[0]; // 'a'
const [newWayA] = myArray; // 'a'
const oldWayB = myArray[1][0][0]; // 'b'
@goldhand
goldhand / get-package-dependencies.js
Created February 10, 2017 22:19
Get a bunch of dependencies from multiple package.json formated for npm install command
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const args = process.argv.slice(2);
const targetDependencies = args.filter(arg => arg.includes('ependencies')).length
? args.filter(arg => arg.includes('ependencies'))
: ['peerDependencies', 'devDependencies', 'dependencies'];
const packageFiles = args.filter(arg => arg.includes('json')).length
@goldhand
goldhand / controlled-vs-uncontrolled.jsx
Created December 30, 2016 17:19
Compare controlled and uncontrolled components
/**
* Uncontrolled component
* DOM controls state and is source of truth.
*/
const EditHeader = ({
style,
value,
}) => {
let elem;
@goldhand
goldhand / parseHtmlStyles.js
Last active March 18, 2023 16:28
Parse html style string for a react component
/**
* @function parseStyles
* Parses a string of inline styles into a javascript object with casing for react
*
* @param {string} styles
* @returns {Object}
*/
const parseStyles = styles => styles
.split(';')
.filter(style => style.split(':')[0] && style.split(':')[1])
@goldhand
goldhand / Async - Promise - Callback hell .js
Created November 18, 2016 07:47
Compare Async / Promise / Callback hell javascript code examples of same end
/**
* fsExists promise
* @param {string} fp - filepath to check exists
* @returns {Promise} fsExists
*/
const fsExists = (fp) => new Promise(
resolve => fs.access(fp, err => resolve(!err))
);
const filepath = path.resolve(__dirname, 'tmp/service-worker.js');
@goldhand
goldhand / example.html
Created November 8, 2016 21:47
Cool pseudo class using the css content attribute. Will before= and after= attributes before and after the element.
<p class="pseudo-content" before="&#8220;" after="&#8221;">You can quote me on this</p>
<!-- will render as:
"You can quote me on this"
-->
@goldhand
goldhand / localStorage.js
Created September 8, 2016 18:19
React redux local storage
import localforage from 'localforage';
import throttle from 'lodash/throttle';
const forageConfig = {
name: 'myproject',
storeName: 'myproject-data',
};
const appStore = localforage.createInstance(forageConfig);