ts-commons
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -11,2 +11,11 @@ export declare class ObjectUtils { | ||
static toSafeString(value: any): string; | ||
static getProperty<T, K extends keyof T>(obj: T, key: K): T[K]; | ||
static setProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]): void; | ||
static getClassName<T>(o: T | { | ||
new (): T; | ||
}): string; | ||
static createObject<T>(type: { | ||
new (): T; | ||
}): T; | ||
static getPropertyName<T>(fn: (o: T) => any): string; | ||
} |
@@ -40,2 +40,37 @@ "use strict"; | ||
}; | ||
ObjectUtils.getProperty = function (obj, key) { | ||
return obj[key]; // Inferred type is T[K] | ||
}; | ||
ObjectUtils.setProperty = function (obj, key, value) { | ||
obj[key] = value; | ||
}; | ||
ObjectUtils.getClassName = function (o) { | ||
if (this.isNullOrUndefined(o)) { | ||
return ""; | ||
} | ||
var testObj = typeof o === "function" ? new o() : o; | ||
return testObj.constructor.name; | ||
}; | ||
ObjectUtils.createObject = function (type) { | ||
if (this.isNullOrUndefined(type)) { | ||
return null; | ||
} | ||
return new type(); | ||
}; | ||
// getPropertyName<Student>((student) => student.name) return name | ||
// getPropertyName<Student>((student) => student.age) return age | ||
ObjectUtils.getPropertyName = function (fn) { | ||
if (this.isNullOrUndefined(fn)) { | ||
return ""; | ||
} | ||
// ES5: function (name) { return student.name; } | ||
// ES6: (student) => student.name; | ||
var expression = fn.toString(); | ||
var returnIndex = expression.indexOf("return"); | ||
var regexp = returnIndex > -1 | ||
? new RegExp("^.*return\\s+\\w+.(\\w+)\\s*;\\s*\\}\\s*$") | ||
: RegExp("^\\s*\\(?\\w+\\)?\\s*=>\\s*\\w+\\.(\\w+)\\s*$"); | ||
var match = regexp.exec(expression); | ||
return !this.isNullOrUndefined(match) && match.length === 2 ? match[1] : ""; | ||
}; | ||
return ObjectUtils; | ||
@@ -42,0 +77,0 @@ }()); |
{ | ||
"name": "ts-commons", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"description": "common methods for typescript", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
60982
810
1