@deepkit/core
Advanced tools
Comparing version 1.0.1-alpha.11 to 1.0.1-alpha.12
@@ -75,2 +75,35 @@ /** | ||
/** | ||
* Returns the ClassType for a given instance. | ||
*/ | ||
export declare function getClassTypeFromInstance<T>(target: T): ClassType<T>; | ||
/** | ||
* Returns true when target is a class instance. | ||
*/ | ||
export declare function isClassInstance(target: any): boolean; | ||
/** | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
export declare function stringifyValueWithType(value: any): string; | ||
/** | ||
* Changes the class of a given instance and returns the new object. | ||
* | ||
* @example | ||
* ```typescript | ||
* | ||
* class Model1 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* class Model2 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* const model1 = new Model1(); | ||
* const model2 = changeClass(model1, Model2); | ||
* model2 instanceof Model2; //true | ||
* ``` | ||
*/ | ||
export declare function changeClass<T>(value: any, newClass: ClassType<T>): T; | ||
export declare function prettyPrintObject(object: object): string; | ||
/** | ||
* Returns true if given obj is a function. | ||
@@ -77,0 +110,0 @@ * |
@@ -15,3 +15,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isPrototypeOfBase = exports.isConstructable = exports.getObjectKeysSize = exports.humanBytes = exports.deletePathValue = exports.setPathValue = exports.getPathValue = exports.time = exports.collectForMicrotask = exports.mergeStack = exports.createStack = exports.mergePromiseStack = exports.asyncOperation = exports.appendObject = exports.prependObjectKeys = exports.average = exports.arrayRemoveItem = exports.arrayClear = exports.last = exports.first = exports.lastKey = exports.firstKey = exports.size = exports.empty = exports.copy = exports.sleep = exports.indexOf = exports.arrayHasItem = exports.isString = exports.isNumber = exports.isSet = exports.isUndefined = exports.isNull = exports.isArray = exports.isObject = exports.isClass = exports.isPromise = exports.isFunction = exports.isPlainObject = exports.typeOf = exports.applyDefaults = exports.getClassPropertyName = exports.getClassName = exports.CustomError = void 0; | ||
exports.isPrototypeOfBase = exports.isConstructable = exports.getObjectKeysSize = exports.humanBytes = exports.deletePathValue = exports.setPathValue = exports.getPathValue = exports.time = exports.collectForMicrotask = exports.mergeStack = exports.createStack = exports.mergePromiseStack = exports.asyncOperation = exports.appendObject = exports.prependObjectKeys = exports.average = exports.arrayRemoveItem = exports.arrayClear = exports.last = exports.first = exports.lastKey = exports.firstKey = exports.size = exports.empty = exports.copy = exports.sleep = exports.indexOf = exports.arrayHasItem = exports.isString = exports.isNumber = exports.isSet = exports.isUndefined = exports.isNull = exports.isArray = exports.isObject = exports.isClass = exports.isPromise = exports.isFunction = exports.prettyPrintObject = exports.changeClass = exports.stringifyValueWithType = exports.isClassInstance = exports.getClassTypeFromInstance = exports.isPlainObject = exports.typeOf = exports.applyDefaults = exports.getClassPropertyName = exports.getClassName = exports.CustomError = void 0; | ||
const dot_prop_1 = __importDefault(require("dot-prop")); | ||
@@ -102,2 +102,75 @@ const iterators_1 = require("./iterators"); | ||
/** | ||
* Returns the ClassType for a given instance. | ||
*/ | ||
function getClassTypeFromInstance(target) { | ||
if (!isClassInstance(target)) { | ||
throw new Error(`Value is not a class instance. Got ${stringifyValueWithType(target)}`); | ||
} | ||
return target['constructor']; | ||
} | ||
exports.getClassTypeFromInstance = getClassTypeFromInstance; | ||
/** | ||
* Returns true when target is a class instance. | ||
*/ | ||
function isClassInstance(target) { | ||
return target !== undefined && target !== null | ||
&& target['constructor'] | ||
&& Object.getPrototypeOf(target) === target['constructor'].prototype | ||
&& !isPlainObject(target) | ||
&& isObject(target); | ||
} | ||
exports.isClassInstance = isClassInstance; | ||
/** | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
function stringifyValueWithType(value) { | ||
if ('string' === typeof value) | ||
return `String(${value})`; | ||
if ('number' === typeof value) | ||
return `Number(${value})`; | ||
if ('boolean' === typeof value) | ||
return `Boolean(${value})`; | ||
if ('function' === typeof value) | ||
return `Function ${value.name}`; | ||
if (isPlainObject(value)) | ||
return `Object ${prettyPrintObject(value)}`; | ||
if (isObject(value)) | ||
return `${getClassName(getClassTypeFromInstance(value))} ${prettyPrintObject(value)}`; | ||
if (null === typeof value) | ||
return `null`; | ||
return 'undefined'; | ||
} | ||
exports.stringifyValueWithType = stringifyValueWithType; | ||
/** | ||
* Changes the class of a given instance and returns the new object. | ||
* | ||
* @example | ||
* ```typescript | ||
* | ||
* class Model1 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* class Model2 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* const model1 = new Model1(); | ||
* const model2 = changeClass(model1, Model2); | ||
* model2 instanceof Model2; //true | ||
* ``` | ||
*/ | ||
function changeClass(value, newClass) { | ||
return Object.assign(Object.create(newClass.prototype), value); | ||
} | ||
exports.changeClass = changeClass; | ||
function prettyPrintObject(object) { | ||
let res = []; | ||
for (const i in object) { | ||
res.push(i + ': ' + stringifyValueWithType(object[i])); | ||
} | ||
return '{' + res.join(',') + '}'; | ||
} | ||
exports.prettyPrintObject = prettyPrintObject; | ||
/** | ||
* Returns true if given obj is a function. | ||
@@ -104,0 +177,0 @@ * |
@@ -288,2 +288,60 @@ "use strict"; | ||
}); | ||
globals_1.test('stringifyValueWithType', async () => { | ||
class Peter { | ||
constructor() { | ||
this.id = 1; | ||
} | ||
} | ||
globals_1.expect(core_1.stringifyValueWithType(new Peter)).toBe(`Peter {id: Number(1)}`); | ||
globals_1.expect(core_1.stringifyValueWithType({ id: 1 })).toBe(`Object {id: Number(1)}`); | ||
globals_1.expect(core_1.stringifyValueWithType('foo')).toBe(`String(foo)`); | ||
globals_1.expect(core_1.stringifyValueWithType(2)).toBe(`Number(2)`); | ||
globals_1.expect(core_1.stringifyValueWithType(true)).toBe(`Boolean(true)`); | ||
globals_1.expect(core_1.stringifyValueWithType(function Peter() { })).toBe(`Function Peter`); | ||
}); | ||
globals_1.test('getClassTypeFromInstance', async () => { | ||
class Peter { | ||
} | ||
globals_1.expect(core_1.getClassTypeFromInstance(new Peter)).toBe(Peter); | ||
globals_1.expect(() => core_1.getClassTypeFromInstance({})).toThrow('Value is not a class instance'); | ||
globals_1.expect(() => core_1.getClassTypeFromInstance('asd')).toThrow('Value is not a class instance'); | ||
globals_1.expect(() => core_1.getClassTypeFromInstance(23)).toThrow('Value is not a class instance'); | ||
globals_1.expect(() => core_1.getClassTypeFromInstance(undefined)).toThrow('Value is not a class instance'); | ||
}); | ||
globals_1.test('isClassInstance', async () => { | ||
class Peter { | ||
} | ||
globals_1.expect(core_1.isClassInstance(new Peter)).toBe(true); | ||
globals_1.expect(core_1.isClassInstance({})).toBe(false); | ||
globals_1.expect(core_1.isClassInstance('asd')).toBe(false); | ||
globals_1.expect(core_1.isClassInstance(undefined)).toBe(false); | ||
globals_1.expect(core_1.isClassInstance(null)).toBe(false); | ||
globals_1.expect(core_1.isClassInstance(3223)).toBe(false); | ||
}); | ||
globals_1.test('isClassInstance', async () => { | ||
class Model1 { | ||
constructor() { | ||
this.id = 0; | ||
} | ||
} | ||
class Base { | ||
} | ||
class Model2 extends Base { | ||
constructor() { | ||
super(...arguments); | ||
this.id = 0; | ||
} | ||
model2() { | ||
return true; | ||
} | ||
} | ||
const model1 = new Model1; | ||
model1.id = 22; | ||
core_1.changeClass(model1, Model2); | ||
globals_1.expect(model1 instanceof Model1).toBe(true); | ||
globals_1.expect(core_1.changeClass(model1, Model2) instanceof Model2).toBe(true); | ||
globals_1.expect(core_1.changeClass(model1, Model2) instanceof Base).toBe(true); | ||
globals_1.expect(core_1.changeClass(model1, Model2) instanceof Model1).toBe(false); | ||
globals_1.expect(core_1.changeClass(model1, Model2).model2()).toBe(true); | ||
}); | ||
//# sourceMappingURL=core.spec.js.map |
@@ -75,2 +75,35 @@ /** | ||
/** | ||
* Returns the ClassType for a given instance. | ||
*/ | ||
export declare function getClassTypeFromInstance<T>(target: T): ClassType<T>; | ||
/** | ||
* Returns true when target is a class instance. | ||
*/ | ||
export declare function isClassInstance(target: any): boolean; | ||
/** | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
export declare function stringifyValueWithType(value: any): string; | ||
/** | ||
* Changes the class of a given instance and returns the new object. | ||
* | ||
* @example | ||
* ```typescript | ||
* | ||
* class Model1 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* class Model2 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* const model1 = new Model1(); | ||
* const model2 = changeClass(model1, Model2); | ||
* model2 instanceof Model2; //true | ||
* ``` | ||
*/ | ||
export declare function changeClass<T>(value: any, newClass: ClassType<T>): T; | ||
export declare function prettyPrintObject(object: object): string; | ||
/** | ||
* Returns true if given obj is a function. | ||
@@ -77,0 +110,0 @@ * |
@@ -89,2 +89,70 @@ /* | ||
/** | ||
* Returns the ClassType for a given instance. | ||
*/ | ||
export function getClassTypeFromInstance(target) { | ||
if (!isClassInstance(target)) { | ||
throw new Error(`Value is not a class instance. Got ${stringifyValueWithType(target)}`); | ||
} | ||
return target['constructor']; | ||
} | ||
/** | ||
* Returns true when target is a class instance. | ||
*/ | ||
export function isClassInstance(target) { | ||
return target !== undefined && target !== null | ||
&& target['constructor'] | ||
&& Object.getPrototypeOf(target) === target['constructor'].prototype | ||
&& !isPlainObject(target) | ||
&& isObject(target); | ||
} | ||
/** | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
export function stringifyValueWithType(value) { | ||
if ('string' === typeof value) | ||
return `String(${value})`; | ||
if ('number' === typeof value) | ||
return `Number(${value})`; | ||
if ('boolean' === typeof value) | ||
return `Boolean(${value})`; | ||
if ('function' === typeof value) | ||
return `Function ${value.name}`; | ||
if (isPlainObject(value)) | ||
return `Object ${prettyPrintObject(value)}`; | ||
if (isObject(value)) | ||
return `${getClassName(getClassTypeFromInstance(value))} ${prettyPrintObject(value)}`; | ||
if (null === typeof value) | ||
return `null`; | ||
return 'undefined'; | ||
} | ||
/** | ||
* Changes the class of a given instance and returns the new object. | ||
* | ||
* @example | ||
* ```typescript | ||
* | ||
* class Model1 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* class Model2 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* const model1 = new Model1(); | ||
* const model2 = changeClass(model1, Model2); | ||
* model2 instanceof Model2; //true | ||
* ``` | ||
*/ | ||
export function changeClass(value, newClass) { | ||
return Object.assign(Object.create(newClass.prototype), value); | ||
} | ||
export function prettyPrintObject(object) { | ||
let res = []; | ||
for (const i in object) { | ||
res.push(i + ': ' + stringifyValueWithType(object[i])); | ||
} | ||
return '{' + res.join(',') + '}'; | ||
} | ||
/** | ||
* Returns true if given obj is a function. | ||
@@ -91,0 +159,0 @@ * |
import { expect, test } from '@jest/globals'; | ||
import { asyncOperation, collectForMicrotask, getClassName, getObjectKeysSize, getPathValue, isArray, isClass, isConstructable, isFunction, isObject, isPlainObject, isPromise, isPrototypeOfBase, isUndefined, setPathValue, sleep } from '../src/core'; | ||
import { asyncOperation, changeClass, collectForMicrotask, getClassName, getClassTypeFromInstance, getObjectKeysSize, getPathValue, isArray, isClass, isClassInstance, isConstructable, isFunction, isObject, isPlainObject, isPromise, isPrototypeOfBase, isUndefined, setPathValue, sleep, stringifyValueWithType } from '../src/core'; | ||
class SimpleClass { | ||
@@ -286,2 +286,60 @@ constructor(name) { | ||
}); | ||
test('stringifyValueWithType', async () => { | ||
class Peter { | ||
constructor() { | ||
this.id = 1; | ||
} | ||
} | ||
expect(stringifyValueWithType(new Peter)).toBe(`Peter {id: Number(1)}`); | ||
expect(stringifyValueWithType({ id: 1 })).toBe(`Object {id: Number(1)}`); | ||
expect(stringifyValueWithType('foo')).toBe(`String(foo)`); | ||
expect(stringifyValueWithType(2)).toBe(`Number(2)`); | ||
expect(stringifyValueWithType(true)).toBe(`Boolean(true)`); | ||
expect(stringifyValueWithType(function Peter() { })).toBe(`Function Peter`); | ||
}); | ||
test('getClassTypeFromInstance', async () => { | ||
class Peter { | ||
} | ||
expect(getClassTypeFromInstance(new Peter)).toBe(Peter); | ||
expect(() => getClassTypeFromInstance({})).toThrow('Value is not a class instance'); | ||
expect(() => getClassTypeFromInstance('asd')).toThrow('Value is not a class instance'); | ||
expect(() => getClassTypeFromInstance(23)).toThrow('Value is not a class instance'); | ||
expect(() => getClassTypeFromInstance(undefined)).toThrow('Value is not a class instance'); | ||
}); | ||
test('isClassInstance', async () => { | ||
class Peter { | ||
} | ||
expect(isClassInstance(new Peter)).toBe(true); | ||
expect(isClassInstance({})).toBe(false); | ||
expect(isClassInstance('asd')).toBe(false); | ||
expect(isClassInstance(undefined)).toBe(false); | ||
expect(isClassInstance(null)).toBe(false); | ||
expect(isClassInstance(3223)).toBe(false); | ||
}); | ||
test('isClassInstance', async () => { | ||
class Model1 { | ||
constructor() { | ||
this.id = 0; | ||
} | ||
} | ||
class Base { | ||
} | ||
class Model2 extends Base { | ||
constructor() { | ||
super(...arguments); | ||
this.id = 0; | ||
} | ||
model2() { | ||
return true; | ||
} | ||
} | ||
const model1 = new Model1; | ||
model1.id = 22; | ||
changeClass(model1, Model2); | ||
expect(model1 instanceof Model1).toBe(true); | ||
expect(changeClass(model1, Model2) instanceof Model2).toBe(true); | ||
expect(changeClass(model1, Model2) instanceof Base).toBe(true); | ||
expect(changeClass(model1, Model2) instanceof Model1).toBe(false); | ||
expect(changeClass(model1, Model2).model2()).toBe(true); | ||
}); | ||
//# sourceMappingURL=core.spec.js.map |
{ | ||
"name": "@deepkit/core", | ||
"version": "1.0.1-alpha.11", | ||
"version": "1.0.1-alpha.12", | ||
"description": "Deepkit core library", | ||
@@ -43,3 +43,3 @@ "type": "module", | ||
}, | ||
"gitHead": "f2267aa0b598cb3b65a24f3420be8a69c3ec3047" | ||
"gitHead": "9bc89def945cb010e7521a753e03df461349110f" | ||
} |
@@ -12,2 +12,3 @@ /* | ||
import dotProp from 'dot-prop'; | ||
import { join } from 'path'; | ||
import { eachPair } from './iterators'; | ||
@@ -116,2 +117,69 @@ | ||
/** | ||
* Returns the ClassType for a given instance. | ||
*/ | ||
export function getClassTypeFromInstance<T>(target: T): ClassType<T> { | ||
if (!isClassInstance(target)) { | ||
throw new Error(`Value is not a class instance. Got ${stringifyValueWithType(target)}`); | ||
} | ||
return (target as any)['constructor'] as ClassType<T>; | ||
} | ||
/** | ||
* Returns true when target is a class instance. | ||
*/ | ||
export function isClassInstance(target: any): boolean { | ||
return target !== undefined && target !== null | ||
&& target['constructor'] | ||
&& Object.getPrototypeOf(target) === (target as any)['constructor'].prototype | ||
&& !isPlainObject(target) | ||
&& isObject(target); | ||
} | ||
/** | ||
* Returns a human readable string representation from the given value. | ||
*/ | ||
export function stringifyValueWithType(value: any): string { | ||
if ('string' === typeof value) return `String(${value})`; | ||
if ('number' === typeof value) return `Number(${value})`; | ||
if ('boolean' === typeof value) return `Boolean(${value})`; | ||
if ('function' === typeof value) return `Function ${value.name}`; | ||
if (isPlainObject(value)) return `Object ${prettyPrintObject(value)}`; | ||
if (isObject(value)) return `${getClassName(getClassTypeFromInstance(value))} ${prettyPrintObject(value)}`; | ||
if (null === typeof value) return `null`; | ||
return 'undefined'; | ||
} | ||
/** | ||
* Changes the class of a given instance and returns the new object. | ||
* | ||
* @example | ||
* ```typescript | ||
* | ||
* class Model1 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* class Model2 { | ||
* id: number = 0; | ||
* } | ||
* | ||
* const model1 = new Model1(); | ||
* const model2 = changeClass(model1, Model2); | ||
* model2 instanceof Model2; //true | ||
* ``` | ||
*/ | ||
export function changeClass<T>(value: any, newClass: ClassType<T>): T { | ||
return Object.assign(Object.create(newClass.prototype), value); | ||
} | ||
export function prettyPrintObject(object: object): string { | ||
let res: string[] = []; | ||
for (const i in object) { | ||
res.push(i + ': ' + stringifyValueWithType((object as any)[i])); | ||
} | ||
return '{' + res.join(',') + '}'; | ||
} | ||
/** | ||
* Returns true if given obj is a function. | ||
@@ -118,0 +186,0 @@ * |
@@ -1,7 +0,9 @@ | ||
import { expect, test } from '@jest/globals'; | ||
import { expect, test } from '@jest/globals'; | ||
import { domainToASCII } from 'url'; | ||
import { | ||
asyncOperation, | ||
changeClass, | ||
collectForMicrotask, | ||
getClassName, | ||
getClassTypeFromInstance, | ||
getObjectKeysSize, | ||
@@ -11,2 +13,3 @@ getPathValue, | ||
isClass, | ||
isClassInstance, | ||
isConstructable, | ||
@@ -20,3 +23,4 @@ isFunction, | ||
setPathValue, | ||
sleep | ||
sleep, | ||
stringifyValueWithType | ||
} from '../src/core'; | ||
@@ -340,2 +344,58 @@ | ||
expect(got).toEqual(['d']); | ||
}); | ||
}); | ||
test('stringifyValueWithType', async () => { | ||
class Peter {id = 1} | ||
expect(stringifyValueWithType(new Peter)).toBe(`Peter {id: Number(1)}`); | ||
expect(stringifyValueWithType({id: 1})).toBe(`Object {id: Number(1)}`); | ||
expect(stringifyValueWithType('foo')).toBe(`String(foo)`); | ||
expect(stringifyValueWithType(2)).toBe(`Number(2)`); | ||
expect(stringifyValueWithType(true)).toBe(`Boolean(true)`); | ||
expect(stringifyValueWithType(function Peter() {})).toBe(`Function Peter`); | ||
}); | ||
test('getClassTypeFromInstance', async () => { | ||
class Peter {} | ||
expect(getClassTypeFromInstance(new Peter)).toBe(Peter); | ||
expect(() => getClassTypeFromInstance({})).toThrow('Value is not a class instance'); | ||
expect(() => getClassTypeFromInstance('asd')).toThrow('Value is not a class instance'); | ||
expect(() => getClassTypeFromInstance(23)).toThrow('Value is not a class instance'); | ||
expect(() => getClassTypeFromInstance(undefined)).toThrow('Value is not a class instance'); | ||
}); | ||
test('isClassInstance', async () => { | ||
class Peter {} | ||
expect(isClassInstance(new Peter)).toBe(true); | ||
expect(isClassInstance({})).toBe(false); | ||
expect(isClassInstance('asd')).toBe(false); | ||
expect(isClassInstance(undefined)).toBe(false); | ||
expect(isClassInstance(null)).toBe(false); | ||
expect(isClassInstance(3223)).toBe(false); | ||
}); | ||
test('isClassInstance', async () => { | ||
class Model1 { | ||
id: number = 0; | ||
} | ||
class Base {} | ||
class Model2 extends Base { | ||
id: number = 0; | ||
model2() { | ||
return true; | ||
} | ||
} | ||
const model1 = new Model1; | ||
model1.id = 22; | ||
changeClass(model1, Model2); | ||
expect(model1 instanceof Model1).toBe(true); | ||
expect(changeClass(model1, Model2) instanceof Model2).toBe(true); | ||
expect(changeClass(model1, Model2) instanceof Base).toBe(true); | ||
expect(changeClass(model1, Model2) instanceof Model1).toBe(false); | ||
expect(changeClass(model1, Model2).model2()).toBe(true); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
611823
8032