Skip to content

Instantly share code, notes, and snippets.

View tuxsudo's full-sized avatar
💭
mostly working in private codebases

Jared Anderson tuxsudo

💭
mostly working in private codebases
View GitHub Profile
// transform object's values
// given a object of functions and an object of value
// run object of values through object of functions
// matched by same prop name
export const map = (fieldFns = {} ) => obj => (
Object.keys(obj)
.reduce( (newObj, key) => {
const fn = fieldFns[ key ];
const originalValue = obj[ key ];
@tuxsudo
tuxsudo / compose.js
Created August 18, 2016 14:11
Compose & Pipe
const compose = (...fns) => (
(x) => fns.reduceRight( (val, fn) => fn( val ), x )
);
export default compose;
import Confirm from './Confirm.js';
import styles from './Confirm.css';
import test from 'tape';
import {spy} from 'sinon';
import { shallow } from 'enzyme';
test('<Confirm />', t => {
const submitHandler = spy();
const confirmHandler = spy();
import SubComponent from 'sub-component';
export default ({title}) => (
<div class="parentComponent">
<SubComponent title={title} />
</div>
);
@tuxsudo
tuxsudo / Either.js
Last active March 22, 2016 20:26
Functional Stuff
var Left = function(x) {
this.__value = x;
};
Left.of = function(x) {
return new Left(x);
};
Left.prototype.map = function(f) {
return this;
// ( (fn=>true), Array ) => { "true": [], "false": [] }
function splitArray(fn, arr) {
return arr.reduce((last, current) => {
let key = fn( current ).toString();
last[key] = last[key].concat(current);
return last;
}, { "true": [], "false": [] } );
}
export function dedupeByMatchingObjectsValues( keys , arrayOfObjects ) {
return arrayOfObjects.filter( (item, i, inArray) => (
inArray.find( target => (
[].concat(keys).reduce( (bool, key) => bool && item[ key ]===target[ key ], true)
))===item
));
}
@tuxsudo
tuxsudo / splitEvery.js
Created December 15, 2015 16:30
Split an array into N number of chunks
export default ( chunkLength = 20 , sourceArray = [] ) => (
sourceArray.reduce( (accumulated, current, index) => {
let chunkN = Math.floor( index/chunkLength );
accumulated[ chunkN ] = ( accumulated[ chunkN ]||[]).concat( current );
return accumulated;
}, [])
);
@tuxsudo
tuxsudo / formialize.js
Last active November 30, 2015 00:18
Serialize Form Contents
export default form => {
let determineValue = elm => {
switch (elm.type) {
case "file":
return elm.files;
case "checkbox":
--log_gc (Log heap samples on garbage collection for the hp2ps tool.)
type: bool default: false
--expose_gc (expose gc extension)
type: bool default: false
--max_new_space_size (max size of the new generation (in kBytes))
type: int default: 0
--max_old_space_size (max size of the old generation (in Mbytes))
type: int default: 0
--max_executable_size (max size of executable memory (in Mbytes))
type: int default: 0