Skip to content

Instantly share code, notes, and snippets.

@rpbeukes
Last active November 27, 2019 12:44
Show Gist options
  • Save rpbeukes/4072f74b4fc7c6706fa753a16dfc908e to your computer and use it in GitHub Desktop.
Save rpbeukes/4072f74b4fc7c6706fa753a16dfc908e to your computer and use it in GitHub Desktop.
nameof-typescript
export class Reflection {
constructor() {
}
//If you need more than just a property name eg: full type name try this - https://github.com/dsherret/ts-nameof#nameofto--
static nameOf<T>(propertyFunction: (typeVal: T) => any): string {
return /\.([^\.;]+);?\s*\}$/.exec(propertyFunction.toString())[1];
}
static nameOfClass<T>(classObject: Function): string {
return classObject.name;
}
}
/*
import { } from 'jasmine';
import { Reflection } from './reflection';
describe(Reflection.nameOfClass(Reflection), () => {
class TestClass {
prop1: string
pror2: {
nestedProp: number
}
}
beforeEach(() => {
});
describe(".nameOfClass", () => {
beforeEach(() => {
});
it('should return class name', async(() => {
let actual = Reflection.nameOfClass(TestClass);
expect(actual).toBe("TestClass");
}));
});
describe(".nameOf<T>", () => {
beforeEach(() => {
});
it('should return property name', async(() => {
let actual = Reflection.nameOf<TestClass>(x => x.prop1);
expect(actual).toBe("prop1");
}));
it('should return nested property name', async(() => {
let actual = Reflection.nameOf<TestClass>(x => x.pror2.nestedProp);
expect(actual).toBe("nestedProp");
}));
});
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment