From 2c75ba989d72b7c04e336bcb4ccfc08fb8981c6b Mon Sep 17 00:00:00 2001 From: vakrilov Date: Thu, 21 May 2015 18:00:53 +0300 Subject: [PATCH] Caching class names --- utils/types.d.ts | 22 ++++++++++++++++ utils/types.ts | 66 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/utils/types.d.ts b/utils/types.d.ts index 49e7b9206..bf84d102b 100644 --- a/utils/types.d.ts +++ b/utils/types.d.ts @@ -61,4 +61,26 @@ * Return an array of strings with the name of all classes. */ export function getBaseClasses(object): Array; + + /** + * A function that gets the ClassInfo for an object. + * @param object The object for which the ClassInfo will be get. + * Returns a ClassInfo for the object. + */ + export function getClassInfo(object: Object): ClassInfo; + + /** + * A Class holding information about a class + */ + export class ClassInfo { + /** + * Gets the name of the class. + */ + name: string; + + /** + * Gets the ClassInfo for the base class of the current info. + */ + baseClassInfo: ClassInfo; + } } \ No newline at end of file diff --git a/utils/types.ts b/utils/types.ts index 16a1d0c06..94bd41367 100644 --- a/utils/types.ts +++ b/utils/types.ts @@ -31,27 +31,63 @@ export function verifyCallback(value: any) { } } +var classInfosMap = new Map(); var funcNameRegex = /function (.{1,})\(/; -export function getClass(object): string { - var results = (funcNameRegex).exec((object).constructor.toString()); - return (results && results.length > 1) ? results[1] : ""; +export function getClass(object: Object): string { + return getClassInfo(object).name; +} + +export function getClassInfo(object: Object): ClassInfo { + var constructor = object.constructor; + + var result = classInfosMap.get(constructor); + if (!result) { + result = new ClassInfo(constructor); + classInfosMap.set(constructor, result); + } + + return result; } export function getBaseClasses(object): Array { - var baseProto = object.__proto__; var result = []; - result.push(getClass(object)); - - while (baseProto !== Object.prototype) { - var baseProtoString = baseProto.toString(); - // while extending some classes for platform specific versions results in duplicate class types in hierarchy - if (result.indexOf(baseProtoString) === -1) { - result.push(baseProtoString); - } - baseProto = baseProto.__proto__; + var info = getClassInfo(object); + while (info) { + result.push(info.name); + info = info.baseClassInfo; + } + return result; +} + +export class ClassInfo { + private _typeCosntructor: Function; + private _name: string; + private _baseClassInfo: ClassInfo; + + constructor(typeCosntructor: Function) { + this._typeCosntructor = typeCosntructor; } - result.push("Object"); + get name(): string { + if (!this._name) { + var results = (funcNameRegex).exec(this._typeCosntructor.toString()); + this._name = (results && results.length > 1) ? results[1] : ""; + } - return result; + return this._name; + } + + get baseClassInfo(): ClassInfo { + if (isUndefined(this._baseClassInfo)) { + var constructorProto = this._typeCosntructor.prototype; + if (constructorProto.__proto__) { + this._baseClassInfo = getClassInfo(constructorProto.__proto__); + } + else { + this._baseClassInfo = null; + } + } + + return this._baseClassInfo; + } } \ No newline at end of file