Skip to content

Instantly share code, notes, and snippets.

View iwillwen's full-sized avatar
🎮
Seeking anwser

Will Wen Gunn iwillwen

🎮
Seeking anwser
View GitHub Profile
export class LabeledEnumType<T = number> {
private value: T;
private label: string;
private props: Map<string, unknown> = new Map<string, unknown>();
constructor(value: T, label: string, props: Record<string, unknown> = {}) {
this.value = value;
this.label = label;
for (const key of Object.keys(props)) {
@iwillwen
iwillwen / int.ts
Last active October 26, 2021 20:00
type Zero = {
isZero: true;
};
type Int = Zero | { prev: Int; isZero: false };
type IncrOne<T extends Int> = {
prev: T;
isZero: false;
};
import * as Vuex from 'vuex'
type GettersOption<S> = { [key: string]: (state: S) => unknown }
type Store<S, M, A, G extends GettersOption<S>> = {
state: S
mutations: M
actions: A
getters: { [key in keyof G]: ReturnType<G[key]> }
}
import * as Vuex from 'vuex'
type GettersOption<S> = { [key: string]: (state: S) => unknown }
type Store<S, M, A, G extends GettersOption<S>> = {
state: S
mutations: M
actions: A
getters: { [key in keyof G]: ReturnType<G[key]> }
}
@iwillwen
iwillwen / types.ts
Created August 5, 2019 16:01
TypeScript ImferredOptionType
interface Options {
type: "boolean" | "number" | "string" | "array";
}
type InferredOptionType<O extends Options> =
O extends { type: "array" } ? Array<string | number> :
O extends { type: "boolean" } ? boolean :
O extends { type: "number" } ? number :
O extends { type: "string" } ? string :
unknown;
@iwillwen
iwillwen / co.js
Last active November 24, 2016 10:15
A simple version of co
/**
* A simple co
* @param {Function} fn Generator Function
* @return {Function} callback
*/
function co(fn) {
return function(done) {
done = done || function() {};
var gen = fn();
@iwillwen
iwillwen / app.js
Last active December 30, 2015 19:08
webjs example
var web = require('webjs');
var redis = require('redis');
var client = redis.createClient();
// webjs style
web.run(process.argv[2] || 8080)
.config({
'views': __dirname + '/views',
'view engine': 'ejs', // jade, etc.
@iwillwen
iwillwen / jsdata.js
Created July 26, 2011 06:37
A micro blog powered by Node.js with Web.js and JSData.js
var fs = require('fs');
var colls = {};
jsd = exports;
function Collection (json) {
this.data = json;
this.toJSON = function () {
return this.data;
};
this.findOne = function (opt) {