Skip to content

Instantly share code, notes, and snippets.

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 April 14, 2020 17:33 — 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
};
@tokland
tokland / puppeteer-click-by-text.js
Last active August 11, 2023 03:48
Click link by text in Puppeteer
const puppeteer = require('puppeteer');
const escapeXpathString = str => {
const splitedQuotes = str.replace(/'/g, `', "'", '`);
return `concat('${splitedQuotes}', '')`;
};
const clickByText = async (page, text) => {
const escapedText = escapeXpathString(text);
const linkHandlers = await page.$x(`//a[contains(text(), ${escapedText})]`);
@tokland
tokland / escape-xpath-string.js
Last active March 31, 2022 23:07
Escape xpath string using concat
function escapeXpathString(str) {
const splitedQuotes = str.replace(/'/g, `', "'", '`);
return `concat('${splitedQuotes}', '')`;
}