Skip to content

Instantly share code, notes, and snippets.

interface Metric {
timestamp: Date;
temperature: number;
pressure: number;
humidity: number;
rain: number;
sun: number;
windDirection: number;
windVelocity: number;
dew: number;
@tokland
tokland / SelectedPick.ts
Last active June 26, 2022 21:21
SelectorPick<Model, Selector>: An exhanced version of Pick<T, Keys> to select nested properties from T
/* SelectedPick<T, Selector>
An extended version of Pick<T, Key> where, instead of a union of keys,
you pass an object with the properties to get from a type. */
type Extends<T1, T2> = [T1] extends [T2] ? true : false
type OmitNever<T> = Pick<T, { [K in keyof T]: T[K] extends never ? never : K }[keyof T]>
export type Selector<Model> = {
[Key in keyof Model]?: boolean | NestedSelectorValue<Model[Key]>
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 March 1, 2019 21:26
Stateful component wrapper over wavesoft/dot-dom with a functional reducer (hooks supported)
/* 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$) {
/* 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 December 30, 2018 11:39
Save current XFCE4 session
#!/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 February 23, 2025 14:10 — forked from yangchenyun/fetch_kindle.js
Get back my books from Kindle
#!/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.
*/
async function(request, response, next) {
return validate()
.then(() => {
return doRealWork()
.then(result => response.send(result))
.catch(error => next(error)))
})
.catch(error => response.status(400).send(error))
}
// intersperse(items: any[], value: any): any[]
// http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-List.html#v:intersperse
const _ = require('lodash');
_.mixin({
intersperse(array, sep) {
return _(array)
.flatMap(x => [x, sep])
.slice(0, -1)
module StringMap = Map.Make({ type t = string; let compare = compare; });
let mapFromList = (pairs: list((string, 'a))): StringMap.t('a) =>
List.fold_left((map, (k, v)) => StringMap.add(k, v, map), StringMap.empty, pairs);
let mapGet = (key: string, defaultValue: 'a, mapping: StringMap.t('a)): 'a =>
switch (StringMap.find(key, mapping)) {
| exception Not_found => defaultValue
| value => value
};