Skip to content

Instantly share code, notes, and snippets.

Arnau Sanchez tokland

Block or report user

Report or block tokland

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@tokland
tokland / react-render-join.ts
Last active Jan 16, 2020
Join array of React nodes with a separator
View react-render-join.ts
function renderJoin(nodes: ReactNode[], separator: ReactNode): ReactNode {
return nodes.flatMap((node, idx) =>
idx < nodes.length - 1 ? [node, separator] : [node]
).map((node, idx) => <React.Fragment key={idx}>{node}</React.Fragment>);
}
// Usage
const myComponent: React.FC = props => {
return <div>{renderJoin([<p>item1</p>, <p>item2</p>], <br />)}</div>;
@tokland
tokland / tagged-unions-pattern-matching.ts
Last active Jan 22, 2020
Typescript: Pattern-matching for tagged unions (match only by discriminator tag)
View tagged-unions-pattern-matching.ts
export type Matcher<KindField extends string, Kind extends string, Obj, Result> = {
[K in Kind]: (obj: Extract<Obj, { [F in KindField]: K }>) => Result;
};
export function match<KindField extends string>(field: KindField) {
return function<
Obj extends { [K in KindField]: Kind },
Result,
Kind extends string = Obj[KindField]
>(obj: Obj, matcher: Matcher<KindField, Kind, Obj, Result>): Result {
@tokland
tokland / spreadsheet-cell-name-from-indexes.ts
Last active Jan 9, 2020
Get cell name from indexes for spreadsheet (Excel, ...)
View spreadsheet-cell-name-from-indexes.ts
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function idOf(idx: number): string {
const quotient = Math.floor(idx / letters.length);
const remainder = idx % letters.length;
return (quotient > 0 ? idOf(quotient - 1) : "") + letters[remainder];
}
function cell(colIdx: number, rowIdx: number): string {
return idOf(colIdx) + (rowIdx + 1).toString();
View meteo-typed-metrics.ts
interface Metric {
timestamp: Date;
temperature: number;
pressure: number;
humidity: number;
rain: number;
sun: number;
windDirection: number;
windVelocity: number;
dew: number;
@tokland
tokland / typed-api.ts
Last active Oct 23, 2019
Nested Typescript API Calls
View typed-api.ts
/* Generic Helpers */
type If<Condition, Then, Else> = Condition extends true ? Then : Else;
type IsLiteral<T> = T extends object ? false : true;
type RejectByValue<Base, Value> = { [Key in keyof Base]: Base[Key] extends Value ? never : Key };
type RejectedKeys<Base, Value> = RejectByValue<Base, Value>[keyof Base];
type OmitByValue<Base, Value> = Pick<Base, RejectedKeys<Base, Value>>;
type OmitNever<T> = OmitByValue<T, never>;
/* Selector<Model> describes the accepted type for a Model selector */
View FlashLight.js
import React from "react";
import PropTypes from "prop-types";
import { Text, View, StyleSheet } from "react-native";
import { Camera, Permissions } from "expo";
/* Expo does not provide a light component, but we can control the flash using the Camera component. */
class FlashLight extends React.Component {
static propTypes = {
isActive: PropTypes.bool.isRequired
};
@tokland
tokland / dom-dom-function-stateful-component.js
Last active Mar 1, 2019
Stateful component wrapper over wavesoft/dot-dom with a functional reducer (hooks supported)
View dom-dom-function-stateful-component.js
/* Stateful component wrapper over wavesoft/dot-dom with a functional reducer (hooks supported) */
function mapValues(input, mapper) {
return Object.keys(input).reduce((acc, key) => {
const value = mapper(input[key], key);
return value ? Object.assign(acc, { [key]: value }) : acc;
}, {});
}
function _setState(setState, newState$) {
View fibonaci-lazy-generators.ts
/* Some monadic and helper functions */
function* pure<T>(value: T): IterableIterator<T> {
yield value;
}
function* concat<T>(...iterators: Array<IterableIterator<T>>): IterableIterator<T> {
for (const iterator of iterators) {
yield* iterator;
}
@tokland
tokland / xfce4-save-session.sh
Created Dec 30, 2018
Save current XFCE4 session
View xfce4-save-session.sh
#!/bin/sh
exec dbus-send \
--session \
--dest=org.xfce.SessionManager \
--print-reply /org/xfce/SessionManager \
org.xfce.Session.Manager.Checkpoint string:""
@tokland
tokland / fetch_kindle.js
Last active Nov 12, 2018 — forked from yangchenyun/fetch_kindle.js
Get back my books from Kindle
View fetch_kindle.js
#!/usr/bin/env node
/*
* @fileoverview Program to free the content in kindle books as plain HTML.
*
* This is largely based on reverse engineering kindle cloud app
* (https://read.amazon.com) to read book data from webSQL.
*
* Access to kindle library is required to download this book.
*/
You can’t perform that action at this time.