Skip to content

Instantly share code, notes, and snippets.

@tomjamesallen
tomjamesallen / Counter.ts
Last active April 25, 2019 09:53
TS function with statics example
// https://medium.com/@martin_hotell/interface-vs-type-alias-in-typescript-2-7-2a8f1777af4c
interface Counter {
// callable part
(): string;
// static properties
interval: number;
reset(): void;
}
const bubbleSort = nums => {
let wip = nums.slice()
let swapped;
do {
swapped = false;
for(let i = 0; i < wip.length; i++) {
let thisItem = wip[i];
let nextItem = wip[i + 1];
const { trimEnd } = require('lodash');
const buildParamsString = params =>
trimEnd(
Object.keys(params).reduce(
(acc, key) => `${acc}${key}=${params[key]}&`,
'?',
),
['?', '&'],
);
@tomjamesallen
tomjamesallen / MyComponent.js
Last active August 2, 2018 11:07
create a query container that will accept a map of fragments
import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import { getQuery } from '.queries';
class MyComponent extends Component {
render() {
return null // render stuff here.
}
}
@tomjamesallen
tomjamesallen / wait.js
Created July 24, 2018 09:40
Await compatible wait function
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
// Usage
const method = async () => {
await wait(200);
console.log('do something 200ms later');
};
@tomjamesallen
tomjamesallen / app.js
Created June 20, 2018 17:34
React Async Set State
import React, {Component} from "react";
import ReactDOM from "react-dom";
class App extends Component {
constructor(props) {
super(props)
this.state = {
mounted: false
}
}
@tomjamesallen
tomjamesallen / index.js
Last active May 30, 2017 11:13
Playing with styled components type api...
const render = (props, strings, ...toInterpolate) =>
`.generatedClass {${strings.reduce((prev, next, i) => `${prev}${next}${typeof toInterpolate[i] === 'function' ? toInterpolate[i](props) : ''}`, '')}}`;
const StyledComponent = (...args) => ({
render: props => render(props, ...args)
});
const MyComponent = StyledComponent`
color: ${props => (props.primary ? 'red' : 'blue')};
font-size: ${props => (props.primary ? '3em' : '2em')};
// input action
// default output action
// blockers : [{condition, action}]
const createConditionalAction = (inputActionType, final, potentialBlockers: []) => {
return (action$, store) => (
action$
.ofType(inputActionType)
.map(action => {
const blockers = potentialBlockers.filter(b => b[0](action, store))
/*
eslint no-unused-vars: 2,
react/no-unused-prop-types: 2,
react/prop-types: 2,
react/jsx-no-duplicate-props: 2,
eqeqeq: 2,
no-case-declarations: 2,
react/no-deprecated: 2
*/
const BemClassGenerator = (b = "") => {
@tomjamesallen
tomjamesallen / isIphone6Plus.js
Created November 18, 2016 15:33
Cordova check for iPhone 6 plus.
const isIPhone6Plus = () => {
if (typeof window.device === "object" &&
window.device.model &&
window.device.model === "iPhone7,1") {
return true;
}
return false;
};
export default isIPhone6Plus;