Skip to content

Instantly share code, notes, and snippets.

View MikeyBurkman's full-sized avatar

Michael Burkman MikeyBurkman

  • Truebill
  • Washington, DC, USA
View GitHub Profile
@MikeyBurkman
MikeyBurkman / parser.ts
Created April 30, 2020 16:37
XPath for TypeScript
import { SelectedValue, useNamespaces } from 'xpath';
import { DOMParser } from 'xmldom';
import * as _ from 'lodash';
import { VError } from 'verror';
interface Parser {
parseXml: (xml: string) => Document;
/**
* Type guard that returns true iff the selected value is a Node.
@MikeyBurkman
MikeyBurkman / banProps.ts
Last active September 11, 2019 13:19
Ban Props TS
export type BanProps<T extends string> = { [P in T]: never } & {
[key: string]: any;
};
interface Foo {
x: string;
y: number;
}
export type NotFoo = BanProps<keyof Foo>;
@MikeyBurkman
MikeyBurkman / objCurry.ts
Last active July 24, 2019 21:20
"Currying" a single object in a one-arg function
type Omit<T1, T2> = { [K in Exclude<keyof T1, keyof T2>]: T1[K] };
const curry = <TDeps, TPartialDeps extends Partial<TDeps>, TReturn>(
fn: (deps: TDeps) => TReturn,
supplied: TPartialDeps
): ((newDeps: Omit<TDeps, TPartialDeps>) => TReturn) => {
return (deps: Omit<TDeps, TPartialDeps>) =>
fn({
...supplied,
...deps
import * as _ from 'lodash';
/**
* Checks to see if a value is defined (not null/undefined) on a given object.
* Is a type-guard function, so the argument will be cast to a version of itself
* where that field is no longer nullable.
* @example
* type Foo = { x?: string }
* const foo: Foo = ...
* if (isDefined(foo, 'x')) {
@MikeyBurkman
MikeyBurkman / tasks.json
Last active February 6, 2022 19:30
VS Code Typescript Build Task
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "tsc-watch",
"group": "build",
"command": "./node_modules/.bin/tsc",
"type": "shell",
@MikeyBurkman
MikeyBurkman / mocker.ts
Last active April 30, 2020 16:36
Typescript Mocking
import util from 'util';
/**
* Behaves like Partial<T>, except nested properties also become optional.
* @example
* ```ts
* type Foo = DeepPartial<{x: { y: number, z: number }>;
* const foo: Foo = {x: { y: 42 } }; // Note that z is now not required
* ```
*/
@MikeyBurkman
MikeyBurkman / gralphql.ts
Last active May 7, 2019 14:56
Graphql Remote Schema Stitching TS
import { readFileSync } from 'fs';
import { mergeSchemas, makeExecutableSchema } from 'graphql-tools';
import { getRemoteSchemas } from './remoteSchemas';
import { Resolvers } from './resolvers';
export const createSchema = async () => {
const resolvers = [Resolvers];
const localSchema = readFileSync(require.resolve('./schema.gql'), 'utf8');
@MikeyBurkman
MikeyBurkman / js-launch.json
Last active February 6, 2022 19:33
JS/Typescript Launch.json files
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Start",
"type": "node",
"request": "launch",
@MikeyBurkman
MikeyBurkman / baz.ts
Last active January 2, 2019 22:27
TS Dependency Example
/**
* Dependency Example in TS.
* Instead of dependency injection, this method shows how services can "fetch" their dependencies,
* without being concerned about the lifecycles of their dependencies.
* These services are also isolated from their dependencies, lending themselves to easier testing.
* Does not use classes or decorators or any black magic to accomplish this.
*
* All services are essentially split into 3 pieces:
* 1. The interface that defines the service to other services.
* 2. The service constructing function that takes in all depedencies as arguments.
@MikeyBurkman
MikeyBurkman / deepExclude.js
Last active December 7, 2018 21:44
Deep Exclude Properties (Immutable)
'use strict';
const deepExclude = (node, exclude) => {
if (Array.isArray(node)) {
return node.map((n) => deepExclude(n, exclude));
} else if (typeof node === 'object' && node) {
return Object.entries(node)
.filter(([key]) => exclude.indexOf(key) === -1)
.reduce((obj, [key, value]) => ({ ...obj, [key]: deepExclude(value, exclude) }), {});
}