Skip to content

Instantly share code, notes, and snippets.

View omichelsen's full-sized avatar

Ole Bjørn Michelsen omichelsen

View GitHub Profile
@omichelsen
omichelsen / cheatsheet.js
Last active November 20, 2021 16:51
JavaScript cheat sheet
// Random 0 to max (excl)
const random = (max) => Math.floor(Math.random() * max)
// Random between (min incl, max excl)
const random = (min, max) => Math.floor(Math.random() * (max - min) + min)
// Fill array
const arr = new Array(42).fill(0).map(() => random(1, 5))
// Remove duplicates from Array (tip: fallback using indexOf)
let chars = ['A', 'B', 'A', 'C', 'B']
@omichelsen
omichelsen / ReactEntry.ts
Created January 29, 2020 00:31
Creates a function that will render a React component into a target element and return an unmount function.
import React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
/**
* Creates a function that will render a React component into a target element and return an unmount function.
* The entry point should be loaded async by a ReactLoader component to ensure code splitting.
* @param component Root component to render
*/
export default <P>(component: React.ComponentType<P>) => (domElement: Element, props: P) => {
render(React.createElement(component, props), domElement)
@omichelsen
omichelsen / ReactLoader.ts
Last active January 28, 2020 21:31
ReactLoader
import { OnDestroy, OnInit } from '@angular/core'
export type VoidFunction = () => void
export interface ReactAppInit {
default(component: Element, ...args: any[]): VoidFunction
}
export default class ReactLoaderComponent<T extends {}> implements OnInit, OnDestroy {
constructor(
@omichelsen
omichelsen / feature-flip-data-rules.js
Last active July 7, 2016 22:18
Feature flipping data and rules
const data = {
browser: {
name: 'Chrome',
version: '52.4.53.123',
os: 'Mac'
},
env: 'development',
settings: {
audioConference: {
release: '2016-10-23 22:34:12'
@omichelsen
omichelsen / emojis.js
Created April 27, 2016 18:51
JS: ascii smiley to emoji mapping
export const keys = Object.freeze({
'o/': '👋',
'</3': '💔',
'<3': '💗',
'8-D': '😁',
'8D': '😁',
':-D': '😁',
'=-3': '😁',
'=-D': '😁',
'=3': '😁',
@omichelsen
omichelsen / .editorconfig
Last active March 8, 2016 19:03
Default editorconfig with tabs and 4 spaces
root = true
[*]
indent_style = tab
indent_size = 4
@omichelsen
omichelsen / logger.js
Last active March 5, 2016 21:10
JS: Logger
export const LogLevels = {
log : 0,
info : 1,
warn : 2,
error : 3
}
function clone(arr) {
return arr.map(a => JSON.parse(JSON.stringify(a)))
}
@omichelsen
omichelsen / PHP: AtomFeed
Created November 24, 2015 08:50
Helper class for creating an Atom feed
class AtomFeed
{
private $xml;
function __construct()
{
$this->xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" />');
}
function setFeedHeader($title, $uri, $updated, $author_name, $author_email, $author_uri)
@omichelsen
omichelsen / feed.xml.jade
Created October 16, 2015 11:11
Jade: RSS
doctype xml
rss(version="2.0", xmlns:atom="http://www.w3.org/2005/Atom")
channel
title= title
link= url
description= description
language= language
atom:link(href="#{ url }feed.xml", rel="self", type="application/rss+xml")
each post, slug in public.blog._data
if slug !== 'index'
@omichelsen
omichelsen / guid.js
Created October 6, 2015 11:45
JS guid
export function create() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0,
v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
var guid = require('js/guid');