Skip to content

Instantly share code, notes, and snippets.

@gunzip
gunzip / snapshot.js
Last active January 14, 2018 23:33
HTML to SVG screenshots with phantomjs (html to pdf) and inkscape (pdf to svg)
/*
* Requires `inkscape` and `phantomjs` installed globally
* Handles @media print and @media all
*/
/* global phantom */
/* global jQuery */
const TIMEOUT_MS = 1000
@gunzip
gunzip / gist:936897e963327d18223f64072497db1b
Created September 16, 2017 10:05
typescript tagged type
declare class As<S extends string> {
private as: S;
}
type Email = string & As<'email'>;
type CustomerId = number & As<'customer-id'>;
@gunzip
gunzip / flatten.ts
Created September 21, 2017 20:32
flatten object with ramda
import { chain, fromPairs, map, toPairs } from "ramda";
export const flattenObj = (obj: {}) => {
// tslint:disable-next-line:no-any
const go = (objX: {}): any =>
chain(([k, v]: [{}, {}]) => {
if (typeof v === "object") {
return map(([kX, vX]) => [`${k}.${kX}`, vX], go(v));
} else {
return [[k, v]];
@gunzip
gunzip / strings.ts
Created September 22, 2017 14:46
typescript validation
/**
* A string guaranteed to have a length within the range [L,H)
*/
export type WithinRangeString<L extends number, H extends number> = string &
IWithinRangeStringTag<L, H>;
function validateWithinRangeString<L extends number, H extends number>(
arg: string,
l: L,
h: H
/**
* A string guaranteed to have a length within the range [L,H)
*/
export type WithinRangeString<L extends number, H extends number> = string &
IWithinRangeStringTag<L, H>;
function validateWithinRangeString<L extends number, H extends number>(
arg: string,
l: L,
h: H
@gunzip
gunzip / created_message_queue_handler.ts
Last active September 28, 2017 11:30
azure queue defer
const failCb = (ctx: IContextWithBindings, error: string) => ctx.done(error);
export function index(context: IContextWithBindings): void {
const fail = (ctx: IContextWithBindings, error: string, timeOut: number) =>
setTimeout(failCb, timeOut, ctx, error);
const timeout = Math.pow(2, context.bindingData.dequeueCount) * 1000;
context.log("sleeping:" + timeout);
fail(context, "error", timeout);
}
<policies>
<inbound>
<base />
<set-variable name="Channel" value="
@{
try {
var request = context.Request.Body.As<JObject>();
return request["content"]["channel"].ToString();
}
@gunzip
gunzip / findLastVersionByModelId.ts
Created October 26, 2017 09:58
findLastVersionByModelId.ts
protected async findLastVersionByModelId<V>(
collectionName: string,
modelIdField: string,
modelIdValue: V
): Promise<Either<DocumentDb.QueryError, Option<TR>>> {
const errorOrMaybeDocument = await DocumentDbUtils.queryOneDocument(
this.dbClient,
this.collectionUri,
{
parameters: [
{
"authorization": {
"action": "Microsoft.Web/serverfarms/write",
"scope": "/subscriptions/XXXXXXXX/resourceGroups/agid-rg-test/providers/Microsoft.Web/serverfarms/agid-app-test"
},
"channels": "Operation",
"correlationId": "XXX",
"eventDataId": "XXX",
"eventName": {
"value": "EndRequest",
function withDefault<T extends t.Any>(
type: T,
defaultValue: t.TypeOf<T>
): t.Type<any, t.TypeOf<T>> {
return new t.Type(
type.name,
(v: any): v is T => type.is(v),
(v: any, c: any) => type.validate(v !== undefined ? v : defaultValue, c),
(v: any) => type.serialize(v)
);