Skip to content

Instantly share code, notes, and snippets.

@dsherret
dsherret / DictionaryExtensionMethods.cs
Last active May 31, 2017 20:34
Dictionary - AddIfNotExists Extension Method
/// <summary>
/// Very simple extension method for adding an item to a dictionary
/// and not evaluating the function unless the key doesn't exist.
///
/// Example with Entity Framework:
/// dataValueCache.AddIfNotExists(dataID, () => db.DataItems.First(d => d.DataID == dataID).Value);
/// </summary>
/// <param name="key">The dictionary key.</param>
/// <param name="value">The dictionary value.</param>
/// <returns>If the item was added to the dictionary.</returns>
/* Typescript conversion of Ben Nadel's Collection class.
Based on the fact that subclassing Array directly does not work very well,
at least in Ecmascript versions < 6. Instead, this is based on creating a
collection from an array, where the actual methods are added to the array
after creation.
It works exactly like an array, while at the same time having additional
functionality.
*/
@dsherret
dsherret / jquery.regex-limit.js
Last active December 28, 2017 09:43
Limit input by regex
/*!
* Copyright 2014 - David Sherret
* MIT License
*/
// Limits an input to specific values without checking keycodes and without flickering the value.
//
// Example usage:
// $("input").limitRegex(/^[0-9]+\.?[0-9]{0,2}$/); -- limit to numbers, up to 2 decimal places
//
@dsherret
dsherret / Using.ts
Last active October 26, 2023 13:30
Typescript Disposable (using statement)
// NOTE: This is now rolled up in a package and supports more scenarios: https://github.com/dsherret/using-statement
interface IDisposable {
dispose();
}
function using<T extends IDisposable>(resource: T, func: (resource: T) => void) {
try {
func(resource);
} finally {
/**
* Caches the return value of get accessors and methods.
*
* Notes:
* - Doesn't really make sense to put this on a method with parameters.
* - Creates an obscure non-enumerable property on the instance to store the memoized value.
* - Could use a WeakMap, but this way has support in old environments.
*/
export function Memoize(target: any, propertyName: string, descriptor: TypedPropertyDescriptor<any>) {
if (descriptor.value != null) {
import * as ts from "typescript";
import * as Lint from "tslint/lib/lint";
export class Rule extends Lint.Rules.AbstractRule {
static FAILURE_STRING = "duplicate imports from same file forbidden";
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoImportsWalker(sourceFile, this.getOptions()));
}
}
export class ThrottledQueue {
private isThrottling = false;
private items: (() => void)[] = [];
constructor(private duration: number) {
}
run(item: () => void) {
this.items.push(item);
this.attemptExecute();
// only works with number enums...
class EnumEx {
private constructor() {
}
static getMembersCount(e: any) {
return EnumEx.getNames(e).length;
}
static getMemberValue(e: any, memberName: string) {
@dsherret
dsherret / WeakCache.ts
Last active September 27, 2018 23:53
A weak cache with support for ES5.
export class WeakCache<T extends object, U> {
private readonly cacheItems: WeakDictionary<T, U>;
constructor() {
if (typeof WeakMap !== "undefined")
this.cacheItems = new WeakMap<T, U>();
else
this.cacheItems = new Es5WeakMap();
}
import { tsquery } from "@phenomnomnominal/tsquery";
import { Node } from "ts-morph";
export function query(node: Node, query: string) {
return tsquery(node.compilerNode, query)
.map(n => (node as any)._getNodeFromCompilerNode(n) as Node);
}