Skip to content

Instantly share code, notes, and snippets.

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<# var maxDimensions = 32; #>
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
public static class ArrayEnumerableExtensions
{
public static IEnumerable<T> AsEnumerable<T>(this Array array)
{
if (array is null)
throw new ArgumentNullException(nameof(array));
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];
export function getPropertyNames<T>(defaultInstance: T): NonFunctionPropertyNames<T>[] {
const memberNames = Object.getOwnPropertyNames(defaultInstance); // get all members of the instance
const propertyNames = memberNames.filter(x => typeof (defaultInstance as any)[x] != "function"); // filter out functions
return propertyNames as NonFunctionPropertyNames<T>[]; // cast as array of string union of non-function properties (ideally would be a string typed tuple but can't be done yet)
}
export function createFromObject<T>(constructor: { new(): T }, parameters: Partial<T>): T {
const newInstance = new constructor();
@NickStrupat
NickStrupat / subscriptions.ts
Created June 28, 2019 16:04
class for pushing in observable subscriptions in order and then unsubscribing from then in reverse order
import { Subscription } from "rxjs";
export class Subscriptions {
private readonly subscriptions = new Array<Subscription>();
push(subscription: Subscription): void {
this.subscriptions.push(subscription);
}
unsubscribeAll(): void {
import { strict as assert } from 'assert';
/**
* This class supports retrieving a class type name at run-time. It requires TypeScript decorators.
* Usage: Register the typename with `@TypeName.decorate(...)`
*
* @TypeName.decorate("Foo")
* class Foo {
* readonly text: string = "text";
* }
export class QueryablePromise<T> implements Promise<T> {
private readonly promise: Promise<T>;
private _isPending: boolean = true;
private _isRejected: boolean = false;
private _isFulfilled: boolean = false;
public get isPending(): boolean { return this._isPending; };
public get isRejected(): boolean { return this._isRejected; };
public get isFulfilled(): boolean { return this._isFulfilled; };
import { HostBinding, OnDestroy, OnInit } from '@angular/core';
import { MediaObserver } from '@angular/flex-layout';
import { Subscription } from 'rxjs';
/**
* If you inherit (extend) your component from this class, the host element of your component will have a class added to it with the media query alias.
* For example...
*
* <app-search-bar class="some-class" _nghost-c5 ...>
*
@NickStrupat
NickStrupat / ng-media-query-body-class.ts
Last active April 12, 2019 19:11
Add the media query alias to the body tag with angular flex-layout
import { Component, OnInit, OnDestroy, Inject, Renderer2 } from '@angular/core';
import { Subscription } from 'rxjs';
import { MediaObserver } from '@angular/flex-layout';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
@NickStrupat
NickStrupat / maps-grouping.ts
Last active April 12, 2019 18:04
Grouping using maps example (now with es5)
class Person {
name!: string;
age!: number;
sex!: string;
public toString = () : string => JSON.stringify(this);
}
const people: Person[] = [
{ age: 25, sex: 'f', name: 'Sue' },
{ age: 30, sex: 'f', name: 'Angela' },
@NickStrupat
NickStrupat / save_excel_to_json.vb
Created March 26, 2019 19:33
UTF-8 and double quote escaping. It saves the file beside the active workbook file but with the .json file extension. It's fast. It can be easily formatted in VS Code (Shift + Alt + F). To use it, hit Alt + F11 to get to the VBA code editor, open the code for your active worksheet, then paste it into the code window. Hit F5 to run.
Public Sub tojson()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
jsonFilename = fso.GetBaseName(ActiveWorkbook.Name) & ".json"
fullFilePath = Application.ActiveWorkbook.Path & "\" & jsonFilename
Dim fileStream As Object
Set fileStream = CreateObject("ADODB.Stream")
fileStream.Type = 2 'Specify stream type - we want To save text/string data.
fileStream.Charset = "utf-8" 'Specify charset For the source text data.