Skip to content

Instantly share code, notes, and snippets.

View jasonrhodes's full-sized avatar

Jason Rhodes jasonrhodes

View GitHub Profile
@jasonrhodes
jasonrhodes / Libraries.jsx
Last active April 10, 2019 09:49
Include the minified version at the top of any of our .jsx script files and then you can include files from adobe_scripts/lib just by calling Libraries.include("subdir/myscript")
// Assumes that your script is somewhere within a folder called "adobe_scripts" and
// that you store your includable files relative to `adobe_scripts/lib/`
// indexOf polyfill from https://gist.github.com/atk/1034425
[].indexOf||(Array.prototype.indexOf=function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1;});
var Libraries = (function (libPath) {
return {
include: function (path) {
if (!path.match(/\.jsx$/i)) {
type Neverize<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type ExclusiveOr<T, U> = (T | U) extends object
? (Neverize<T, U> & U) | (Neverize<U, T> & T)
: T | U;
interface S {
value: string;
string: string;
}
@jasonrhodes
jasonrhodes / debug-diff.js
Last active November 30, 2018 04:01
Compares two objects and gives you detailed diff results about added, removed, changed, and ref-only changes
/**
* flattens an object into a single dimension with dot-separated key paths
*
* e.g.
* const test = { a: 'hello', b: { xx: 'cool', yy: 'yeah' }, c: [1, 2] }
* flatten(test) => { 'a': 'hello', 'b.xx': 'cool', 'b.yy': 'yeah', 'c.0': 1, 'c.1': 2 }
*/
function flatten(o, path = []) {
return Object.entries(o).reduce((result, [key, value]) => {
const localPath = [...path, key];
@jasonrhodes
jasonrhodes / kibana-build-flow.md
Last active November 26, 2018 18:44
Schoolhouse Rock: How a Kibana becomes a build (specifically client-side webpack)
@jasonrhodes
jasonrhodes / why.js
Created November 2, 2018 14:44
implicit function naming quirk?
// file1a.js
const CoolName = () => 'cool'
export { CoolName }
// file2a.js
import { CoolName } from './file1a'
console.log(CoolName.name) // -> "CoolName"
/**
#!/bin/sh
#
# Suggested name for this script: git-clean-stale-branches
#
# This script will help to remove "stale" branches from a remote
# repository (by default the "origin" repository). Stale branches
# are any branches that does not exist in the local repository.
#
# This script should be run in the local repository. It will print
# out a git command to remove all branches from the remote repository
@jasonrhodes
jasonrhodes / allSettled.js
Last active October 14, 2018 17:41
Native promise implementation of something like q.allSettled
const captureSuccess = (result) => ({ state: 'succeeded', result })
const captureFail = (error) => ({ state: 'rejected', error })
module.exports.allSettled = (promises, { only }) => {
const settled = Promise.all(promises.map((p) => p.then(captureSuccess).catch(captureFail)))
return only
? settled.then((results) => results.filter((r) => r.state === only).map((r) => r.result))
: settled
}
// these examples use thunks but the problem applies to most other redux async strategies
// action creator 1: get user
const getUser = async (id) => (dispatch) => {
dispatch({ type: 'GET_USER_PENDING', payload: id })
try {
const result = await doAsyncThing(id)
return dispatch({ type: 'GET_USER_SUCCESS', payload: result })
} catch (error) {
return dispatch({ type: 'GET_USER_FAILED', payload: error })
@jasonrhodes
jasonrhodes / render-props.jsx
Last active May 17, 2018 16:00
React HOCs and "Render Props" ... Note: If we're going to call it the "render prop" pattern, can we stop using props.children as the function?
// higher order component creator
const withFun = (Component) => (props) => <Component {...props} fun='Injected Secret Value' />
// using the higher order component (with this pattern, you have to create a component, THEN wrap it in the HOC creator function)
const _MyComponent = (props) => <div><h1>{props.fun}</h1></div>
const MyComponent = withFun(_MyComponent)
// "render prop" using children
@jasonrhodes
jasonrhodes / self.static.php
Created June 5, 2012 20:49
Self vs Static in PHP
<?php
class A
{
public static $var = "I'm set in A!<br>";
public static function getStatic() {
echo static::$var;
}
public static function getSelf() {