Skip to content

Instantly share code, notes, and snippets.

View Willmo36's full-sized avatar

Max Willmott Willmo36

  • SkipTheDishes
  • Saskatoon
View GitHub Profile
@Willmo36
Willmo36 / toRanges.ts
Last active October 20, 2017 10:57
Split T[] into T[][].
function toRanges<T>(items: T[], comparator: (a: T, b: T) => boolean): T[][] {
return items.reduce<T[][]>(
(acc, item) => {
const previousItem = R.last(R.last(acc));
if (R.isNil(previousItem)) {
acc[acc.length - 1].push(item);
return acc;
}
@Willmo36
Willmo36 / pipeLog.ts
Last active August 4, 2017 11:43
Help debug & log R.pipe/compase
const pipeLog = <T>(x: T): T => {
console.log(x);
return x;
};
const pipeDebug = <T>(x: T): T => {
debugger;
return x;
};
/**REMOTE DATA IMPLEMENTATION */
type Idle = { key: "idle", data: null };
type Loading = { key: "loading", data: boolean };
type Success = { key: "success", data: string };
type RemoteDataState = Idle | Loading | Success;
componentWillReceiveProps(nextProps){
console.info("New props, about to render...");
Object.keys(nextProps).forEach((p) => {
console.info(`${p} is the same: ${this.props[p] === nextProps[p]}`);
});
const shouldIUsePureRender = Object.keys(nextProps).reduce((acc, p) => {
if (nextProps[p] !== this.props[p]) {
return false;
}
@Willmo36
Willmo36 / freactal.d.ts
Created June 2, 2017 16:58
Some freactal types
declare module "freactal" {
interface ProvideStateArg<P, S, C, E> {
middleware?: any[];
initialState: (props: P, freactal: {}) => S;
computed?: FreactalComputed<C, S>;
effects?: FreactalEffectDefinitions<E, S>;
}
export type InjectStateProps<P, S, C = null, E = null> = P & { state: S & C, effects: FreactalEffectUsages<E, S> };
type InjectStateArg<P, S, C, E> = (p: InjectStateProps<P, S, C, E>) => JSX.Element;
@Willmo36
Willmo36 / HoC.jsx
Last active April 11, 2017 11:07
TypeScript HoC props interface
export default function hoc<T extends HoCProps>(WrappedComponent): React.ComponentClass<T> {
return class extends React.Component<T, null> {
...
}
}
@Willmo36
Willmo36 / esnextbin.md
Created March 27, 2017 17:31
esnextbin sketch
@Willmo36
Willmo36 / RamdaOptionalImmutableLens.js
Created December 16, 2016 14:16
Lens which accepts POJO or Immutable Maps
//helpers
const getter = R.curry(function (key, obj) {
return Iterable.isIterable(obj)
? obj.get(key) : obj[key];
});
const setter = R.curry(function (key, val, obj) {
return Iterable.isIterable(obj)
? obj.set(key, value) : R.assoc(key, val, obj);
});
@Willmo36
Willmo36 / logFn.js
Created August 24, 2016 18:18
Wrap function call in a log
const logFn = R.curry((msg, fn) => {
return function () {
console.info("Calling " + msg, arguments);
const result = fn.apply(this, arguments);
console.info("Result " + msg, result);
return result;
}
});
@Willmo36
Willmo36 / esnextbin.md
Created August 22, 2016 17:24
esnextbin sketch