Skip to content

Instantly share code, notes, and snippets.

View ditlevtojner's full-sized avatar

Ditlev Tøjner ditlevtojner

View GitHub Profile
@ditlevtojner
ditlevtojner / logic-problem.js
Last active February 16, 2020 20:54
ASOSIO Logic Exercise: Reactions
// Use the following function to generate random reactions and filter the output
// tweak LIMIT if you experience sluggishness
// Read below for more instructions
const LIMIT = 20
function randomAPIresponse() {
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]];}
return array
function addCss(fileName) {
var head = document.head;
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = fileName;
head.appendChild(link);
}
import React from 'React'
function asyncComponent(getComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = { Component: AsyncComponent.Component };
componentWillMount() {
@ditlevtojner
ditlevtojner / transaction.js
Last active October 4, 2017 20:57
transaction in node
const chain = startTransaction.chain()
const success = () => {
startTransaction.end()
res.sendStatus(204)
}
const error = (error) => {
startTransaction.end()
res.sendStatus(500)
}
@ditlevtojner
ditlevtojner / Component.react.js
Last active May 8, 2016 20:05
Simple validation i wrote for a React app
// bound to a button onClick method
let validation = Validator({
name: ReactDOM.findDOMNode(this.refs.name).value,
company: ReactDOM.findDOMNode(this.refs.company).value,
email: ReactDOM.findDOMNode(this.refs.email).value,
}, new Map([
@ditlevtojner
ditlevtojner / Type.js
Last active April 30, 2016 15:32
A small type utility
// Examples
Type.is.function(() => {}) // true
Type.is.array({}) // false
Type.is.not.array([]) // false
Type.is.not.object(null) // true
Type.detect({}) // "object"
Type.detect("string") // "string"
@ditlevtojner
ditlevtojner / ddebug.js
Created April 11, 2016 21:38
Simple debug scaffold that helps me introspect and trigger state changes via the browser console
import utils from './utils'
import AppDispatcher from 'AppDispatcher'
import Request from './Request'
import NotificationManager from './NotificationManager'
import SessionManager from './SessionManager'
import ResourceStore from './ResourceStore'
const context = {
AppDispatcher,
Request,
@ditlevtojner
ditlevtojner / How.js
Created March 4, 2016 18:01
How to create a draft.js "viewer"
/*
Draft.js came out, thank you again Facebook, Easy to implement,
but i struggled with how it could work with persisting it backend and then parseing it.
The flow looks like this
1. DraftJsEditor has set state with an immutable-js obj contaning the state of the contentfield
2. we transform it to json via:
*/
let data = {
content: JSON.stringify(convertToRaw(this.state.content.getCurrentContent()))
@ditlevtojner
ditlevtojner / Main.react.js
Last active January 14, 2016 11:03
ES6 + React + Browserify
import React from 'react'
export default class Main extends React.Component {
constructor(props) {
super(props);
}
render() {
return <div>It works</div>
@ditlevtojner
ditlevtojner / compose.js
Last active January 6, 2016 19:30
Example of composability within React :: http://jsbin.com/jamenunuso/1/edit?js,console,output
class Parent extends React.Component {
constructor(props) {
super(props)
}
render() {
return React.cloneElement(
this.props.children,
this.props.passThisToChild