Skip to content

Instantly share code, notes, and snippets.

@simonpai
simonpai / build-tsconfig.mjs
Created January 15, 2023 03:46
tsconfig.js
import { writeFile } from 'fs/promises';
import { resolve } from 'path';
import glob from 'fast-glob';
const cwd = process.env.PWD;
async function outputConfigJson(file) {
const outputFile = file.replace(/\.js$/, '.json');
// use import() so both CJS and ESM work
let config = await import(resolve(cwd, file));
export function compose(...fns) {
const [fn0, ...rest] = fns.reverse();
return (...args) => rest.reduce((acc, fn) => fn(acc), fn0(...args));
}
export function mapValue(obj, fn) {
return Object.keys(obj).reduce((acc, key) => {
acc[key] = fn(obj[key]);
return acc;
}, {});
@simonpai
simonpai / ConfirmProvider.js
Last active March 15, 2019 13:22
useConfirm
import React, { useState, useCallback } from 'react';
// import PropTypes from 'prop-types';
import { Confirm } from 'semantic-ui-react';
const PureConfirm = React.memo(Confirm);
const CLOSE = [false, undefined, undefined, undefined];
export const Context = React.createContext(CLOSE);
import React from 'react';
import PropTypes from 'prop-types';
import { Provider, connect } from 'react-redux';
import { api } from '../../util/stores';
export const Context = React.createContext();
function mapDispatchToProps(dispatch, {action}) {
return {
api: api(action, dispatch)
function combine() {
return Array.prototype.slice.call(arguments)
.reverse()
.reduce(function(acc, m) {
return function(left, right, next) {
return m(left, right, function(l, r) {
return acc(l, r, next);
});
};
});
@simonpai
simonpai / RequestAbs.java
Created August 19, 2015 03:16
Vert.x 3.x HttpClient::requestAbs test case
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpMethod;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@simonpai
simonpai / Quire analysis AST type counts
Created August 26, 2014 06:40
Quire analysis AST type counts.
time cost: 6905ms
{
AdjacentStrings: 64
Annotation: 408
ArgumentList: 13635
AsExpression: 142
AssertStatement: 90
AssignmentExpression: 2390
BinaryExpression: 3989
Block: 4737
@simonpai
simonpai / dart-factory-constructor-redirect
Last active August 29, 2015 14:05
Dart has a way to do factory constructor alias...
class A extends B {
A();
factory A.x() => new A();
factory A.y() = A.x;
}
@simonpai
simonpai / Quire analysis AST type counts
Created July 29, 2014 08:52
Quire analysis AST type counts.
time cost: 6509ms
{
CompilationUnit: 278,
ImportDirective: 940,
SimpleStringLiteral: 6238,
SimpleIdentifier: 90616,
FunctionDeclaration: 1006,
TypeName: 19521,
FunctionExpression: 3160,
FormalParameterList: 5409,
@simonpai
simonpai / plants
Created May 3, 2014 04:01
Plant coverage in pesticide entries.
乾豆類: 11
亞洲棕櫚: 3
亞麻: 5
亞麻園: 2
仙草: 10
休閒地: 1
倉庫: 2
冬瓜: 3
刺桐屬植物: 2
包葉菜類: 30