Skip to main content
Version: 4.4

Class: Utils#

core.Utils

Hierarchy#

  • Utils

Constructors#

constructor#

+ new Utils(): Utils

Returns: Utils

Properties#

PK_SEPARATOR#

Readonly Static PK_SEPARATOR: ~~~= '~~~'

Defined in: packages/core/src/utils/Utils.ts:119

Methods#

absolutePath#

StaticabsolutePath(path: string, baseDir?: string): string

Parameters:#

NameTypeDefault value
pathstring-
baseDirstring...

Returns: string

Defined in: packages/core/src/utils/Utils.ts:609


asArray#

StaticasArray<T>(data?: T | T[], strict?: boolean): T[]

Normalize the argument to always be an array.

Type parameters:#

Name
T

Parameters:#

NameTypeDefault value
data?T | T[]-
strictbooleanfalse

Returns: T[]

Defined in: packages/core/src/utils/Utils.ts:272


callCompiledFunction#

StaticcallCompiledFunction<T, R>(fn: (...args: T) => R, ...args: T): R

Type parameters:#

NameType
Tunknown[]
R-

Parameters:#

NameType
fn(...args: T) => R
...argsT

Returns: R

Defined in: packages/core/src/utils/Utils.ts:735


className#

StaticclassName<T>(classOrName: EntityName<T>): string

Gets string name of given class.

Type parameters:#

Name
T

Parameters:#

NameType
classOrNameEntityName<T>

Returns: string

Defined in: packages/core/src/utils/Utils.ts:508


copy#

Staticcopy<T>(entity: T): T

Creates deep copy of given entity.

Type parameters:#

Name
T

Parameters:#

NameType
entityT

Returns: T

Defined in: packages/core/src/utils/Utils.ts:265


createFunction#

StaticcreateFunction(context: Map<string, any>, code: string): any

Parameters:#

NameType
contextMap<string, any>
codestring

Returns: any

Defined in: packages/core/src/utils/Utils.ts:724


defaultValue#

StaticdefaultValue<T>(prop: T, option: keyof T, defaultValue: any): void

Type parameters:#

NameType
TDictionary<any>

Parameters:#

NameType
propT
optionkeyof T
defaultValueany

Returns: void

Defined in: packages/core/src/utils/Utils.ts:631


detectTsNode#

StaticdetectTsNode(): boolean

Tries to detect ts-node runtime.

Returns: boolean

Defined in: packages/core/src/utils/Utils.ts:519


diff#

Staticdiff(a: Dictionary<any>, b: Dictionary<any>): Record<string | number, any>

Computes difference between two objects, ignoring items missing in b.

Parameters:#

NameType
aDictionary<any>
bDictionary<any>

Returns: Record<string | number, any>

Defined in: packages/core/src/utils/Utils.ts:248


equals#

Staticequals(a: any, b: any): boolean

Checks if arguments are deeply (but not strictly) equal.

Parameters:#

NameType
aany
bany

Returns: boolean

Defined in: packages/core/src/utils/Utils.ts:191


extractEnumValues#

StaticextractEnumValues(target: Dictionary<any>): (string | number)[]

Extracts all possible values of a TS enum. Works with both string and numeric enums.

Parameters:#

NameType
targetDictionary<any>

Returns: (string | number)[]

Defined in: packages/core/src/utils/Utils.ts:657


extractPK#

StaticextractPK<T>(data: any, meta?: EntityMetadata<T>, strict?: boolean): null | string | Primary<T>

Extracts primary key from data. Accepts objects or primary keys directly.

Type parameters:#

NameType
TAnyEntity<T>

Parameters:#

NameTypeDefault value
dataany-
meta?EntityMetadata<T>-
strictbooleanfalse

Returns: null | string | Primary<T>

Defined in: packages/core/src/utils/Utils.ts:364


findDuplicates#

StaticfindDuplicates<T>(items: T[]): T[]

Type parameters:#

Name
T

Parameters:#

NameType
itemsT[]

Returns: T[]

Defined in: packages/core/src/utils/Utils.ts:635


flatten#

Staticflatten<T>(arrays: T[][]): T[]

Type parameters:#

Name
T

Parameters:#

NameType
arraysT[][]

Returns: T[]

Defined in: packages/core/src/utils/Utils.ts:673


getCompositeKeyHash#

StaticgetCompositeKeyHash<T>(entity: T, meta: EntityMetadata<T>): string

Type parameters:#

NameType
TAnyEntity<T>

Parameters:#

NameType
entityT
metaEntityMetadata<T>

Returns: string

Defined in: packages/core/src/utils/Utils.ts:388


getGlobalStorage#

StaticgetGlobalStorage(namespace: string): Dictionary<any>

Parameters:#

NameType
namespacestring

Returns: Dictionary<any>

Defined in: packages/core/src/utils/Utils.ts:689


getORMVersion#

StaticgetORMVersion(): string

Returns: string

Defined in: packages/core/src/utils/Utils.ts:710


getObjectKeysSize#

StaticgetObjectKeysSize(object: Dictionary<any>): number

Returns the number of properties on obj. This is 20x faster than Object.keys(obj).length.

see https://github.com/deepkit/deepkit-framework/blob/master/packages/core/src/core.ts

Parameters:#

NameType
objectDictionary<any>

Returns: number

Defined in: packages/core/src/utils/Utils.ts:146


getObjectType#

StaticgetObjectType(value: any): string

Gets the type of the argument.

Parameters:#

NameType
valueany

Returns: string

Defined in: packages/core/src/utils/Utils.ts:552


getOrderedPrimaryKeys#

StaticgetOrderedPrimaryKeys<T>(id: Primary<T> | Record<string, Primary<T>>, meta: EntityMetadata<T>, platform?: Platform, convertCustomTypes?: boolean): Primary<T>[]

Type parameters:#

NameType
TAnyEntity<T>

Parameters:#

NameType
idPrimary<T> | Record<string, Primary<T>>
metaEntityMetadata<T>
platform?Platform
convertCustomTypes?boolean

Returns: Primary<T>[]

Defined in: packages/core/src/utils/Utils.ts:449


getParamNames#

StaticgetParamNames(func: string | { toString: () => string }, methodName?: string): string[]

Returns array of functions argument names. Uses escaya for source code analysis.

Parameters:#

NameType
funcstring | { toString: () => string }
methodName?string

Returns: string[]

Defined in: packages/core/src/utils/Utils.ts:300


getPrimaryKeyCond#

StaticgetPrimaryKeyCond<T>(entity: T, primaryKeys: string[]): null | Record<string, Primary<T>>

Type parameters:#

NameType
TAnyEntity<T>

Parameters:#

NameType
entityT
primaryKeysstring[]

Returns: null | Record<string, Primary<T>>

Defined in: packages/core/src/utils/Utils.ts:429


getPrimaryKeyCondFromArray#

StaticgetPrimaryKeyCondFromArray<T>(pks: Primary<T>[], primaryKeys: string[]): Record<string, Primary<T>>

Type parameters:#

NameType
TAnyEntity<T>

Parameters:#

NameType
pksPrimary<T>[]
primaryKeysstring[]

Returns: Record<string, Primary<T>>

Defined in: packages/core/src/utils/Utils.ts:442


getPrimaryKeyHash#

StaticgetPrimaryKeyHash(pks: string[]): string

Parameters:#

NameType
pksstring[]

Returns: string

Defined in: packages/core/src/utils/Utils.ts:403


getPrimaryKeyValues#

StaticgetPrimaryKeyValues<T>(entity: T, primaryKeys: string[], allowScalar?: boolean): any

Type parameters:#

NameType
TAnyEntity<T>

Parameters:#

NameTypeDefault value
entityT-
primaryKeysstring[]-
allowScalarbooleanfalse

Returns: any

Defined in: packages/core/src/utils/Utils.ts:411


getRootEntity#

StaticgetRootEntity(metadata: IMetadataStorage, meta: EntityMetadata<any>): EntityMetadata<any>

Parameters:#

NameType
metadataIMetadataStorage
metaEntityMetadata<any>

Returns: EntityMetadata<any>

Defined in: packages/core/src/utils/Utils.ts:229


hasObjectKeys#

StatichasObjectKeys(object: Dictionary<any>): boolean

Returns true if obj has at least one property. This is 20x faster than Object.keys(obj).length.

see https://github.com/deepkit/deepkit-framework/blob/master/packages/core/src/core.ts

Parameters:#

NameType
objectDictionary<any>

Returns: boolean

Defined in: packages/core/src/utils/Utils.ts:163


hash#

Statichash(data: string): string

Parameters:#

NameType
datastring

Returns: string

Defined in: packages/core/src/utils/Utils.ts:621


isCollection#

StaticisCollection<T, O>(item: any, prop?: EntityProperty<T>, type?: SCALAR | ONE_TO_ONE | ONE_TO_MANY | MANY_TO_ONE | MANY_TO_MANY | EMBEDDED): item is Collection<T, O>

Type parameters:#

NameDefault
T-
Ounknown

Parameters:#

NameType
itemany
prop?EntityProperty<T>
type?SCALAR | ONE_TO_ONE | ONE_TO_MANY | MANY_TO_ONE | MANY_TO_MANY | EMBEDDED

Returns: item is Collection<T, O>

Defined in: packages/core/src/utils/Utils.ts:578


isDefined#

StaticisDefined<T>(data: any, considerNullUndefined?: boolean): data is T

Checks if the argument is not undefined

Type parameters:#

NameDefault
TRecord<string, unknown\>

Parameters:#

NameTypeDefault value
dataany-
considerNullUndefinedbooleanfalse

Returns: data is T

Defined in: packages/core/src/utils/Utils.ts:124


isEmpty#

StaticisEmpty(data: any): boolean

Checks whether the argument is empty (array without items, object without keys or falsy value).

Parameters:#

NameType
dataany

Returns: boolean

Defined in: packages/core/src/utils/Utils.ts:493


isEntity#

StaticisEntity<T>(data: any, allowReference?: boolean): data is T

Checks whether given object is an entity instance.

Type parameters:#

NameDefault
TAnyEntity<any\>

Parameters:#

NameTypeDefault value
dataany-
allowReferencebooleanfalse

Returns: data is T

Defined in: packages/core/src/utils/Utils.ts:471


isGroupOperator#

StaticisGroupOperator(key: string): boolean

Parameters:#

NameType
keystring

Returns: boolean

Defined in: packages/core/src/utils/Utils.ts:685


isNotObject#

StaticisNotObject<T>(o: any, not: any[]): o is T

Checks if the argument is instance of Object, but not one of the blacklisted types. Returns false for arrays.

Type parameters:#

NameDefault
TDictionary<any\>

Parameters:#

NameType
oany
notany[]

Returns: o is T

Defined in: packages/core/src/utils/Utils.ts:138


isNumber#

StaticisNumber<T>(s: any): s is T

Checks if the argument is number

Type parameters:#

NameDefault
Tnumber

Parameters:#

NameType
sany

Returns: s is T

Defined in: packages/core/src/utils/Utils.ts:184


isObject#

StaticisObject<T>(o: any): o is T

Checks if the argument is instance of Object. Returns false for arrays.

Type parameters:#

NameDefault
TDictionary<any\>

Parameters:#

NameType
oany

Returns: o is T

Defined in: packages/core/src/utils/Utils.ts:131


isObjectID#

StaticisObjectID(key: any): boolean

Checks whether the argument is ObjectId instance

Parameters:#

NameType
keyany

Returns: boolean

Defined in: packages/core/src/utils/Utils.ts:486


isOperator#

StaticisOperator(key: string, includeGroupOperators?: boolean): boolean

Parameters:#

NameTypeDefault value
keystring-
includeGroupOperatorsbooleantrue

Returns: boolean

Defined in: packages/core/src/utils/Utils.ts:677


isPlainObject#

StaticisPlainObject(value: any): value is Dictionary<any>

Checks whether the value is POJO (e.g. { foo: 'bar' }, and not instance of Foo)

Parameters:#

NameType
valueany

Returns: value is Dictionary<any>

Defined in: packages/core/src/utils/Utils.ts:560


isPrimaryKey#

StaticisPrimaryKey<T>(key: any, allowComposite?: boolean): key is Primary<T>

Checks whether the argument looks like primary key (string, number or ObjectId).

Type parameters:#

Name
T

Parameters:#

NameTypeDefault value
keyany-
allowCompositebooleanfalse

Returns: key is Primary<T>

Defined in: packages/core/src/utils/Utils.ts:353


isString#

StaticisString(s: any): s is string

Checks if the argument is string

Parameters:#

NameType
sany

Returns: s is string

Defined in: packages/core/src/utils/Utils.ts:177


lookupPathFromDecorator#

StaticlookupPathFromDecorator(name: string, stack?: string[]): string

Uses some dark magic to get source path to caller where decorator is used. Analyses stack trace of error created inside the function call.

Parameters:#

NameType
namestring
stack?string[]

Returns: string

Defined in: packages/core/src/utils/Utils.ts:531


merge#

Staticmerge(target: any, ...sources: any[]): any

Merges all sources into the target recursively.

Parameters:#

NameType
targetany
...sourcesany[]

Returns: any

Defined in: packages/core/src/utils/Utils.ts:205


normalizePath#

StaticnormalizePath(...parts: string[]): string

Parameters:#

NameType
...partsstring[]

Returns: string

Defined in: packages/core/src/utils/Utils.ts:586


pathExists#

StaticpathExists(path: string, options?: GlobbyOptions): Promise<boolean>

Parameters:#

NameTypeDefault value
pathstring-
optionsGlobbyOptions...

Returns: Promise<boolean>

Defined in: packages/core/src/utils/Utils.ts:645


propertyDecoratorReturnValue#

StaticpropertyDecoratorReturnValue(): any

see https://github.com/mikro-orm/mikro-orm/issues/840

Returns: any

Defined in: packages/core/src/utils/Utils.ts:751


randomInt#

StaticrandomInt(min: number, max: number): number

Parameters:#

NameType
minnumber
maxnumber

Returns: number

Defined in: packages/core/src/utils/Utils.ts:641


relativePath#

StaticrelativePath(path: string, relativeTo: string): string

Parameters:#

NameType
pathstring
relativeTostring

Returns: string

Defined in: packages/core/src/utils/Utils.ts:593


renameKey#

StaticrenameKey<T>(payload: T, from: string | keyof T, to: string): void

Renames object key, keeps order of properties.

Type parameters:#

Name
T

Parameters:#

NameType
payloadT
fromstring | keyof T
tostring

Returns: void

Defined in: packages/core/src/utils/Utils.ts:287


requireFrom#

StaticrequireFrom(id: string, from: string): any

Require a module from a specific location

Parameters:#

NameTypeDescription
idstringThe module to require
fromstringLocation to start the node resolution

Returns: any

Defined in: packages/core/src/utils/Utils.ts:701


runIfNotEmpty#

StaticrunIfNotEmpty(clause: () => any, data: any): void

Parameters:#

NameType
clause() => any
dataany

Returns: void

Defined in: packages/core/src/utils/Utils.ts:625


runSerial#

StaticrunSerial<T, U>(items: Iterable<U>, cb: (item: U) => Promise<T>): Promise<T[]>

Executes the cb promise serially on every element of the items array and returns array of resolved values.

Type parameters:#

NameDefault
Tany
Uany

Parameters:#

NameType
itemsIterable<U>
cb(item: U) => Promise<T>

Returns: Promise<T[]>

Defined in: packages/core/src/utils/Utils.ts:568


splitPrimaryKeys#

StaticsplitPrimaryKeys(key: string): string[]

Parameters:#

NameType
keystring

Returns: string[]

Defined in: packages/core/src/utils/Utils.ts:407


unique#

Staticunique<T>(items: T[]): T[]

Gets array without duplicates.

Type parameters:#

NameDefault
Tstring

Parameters:#

NameType
itemsT[]

Returns: T[]

Defined in: packages/core/src/utils/Utils.ts:198


walkNode#

Private StaticwalkNode(node: Dictionary<any>, checkNode: (node: Dictionary<any>) => void): void

Parameters:#

NameType
nodeDictionary<any>
checkNode(node: Dictionary<any>) => void

Returns: void

Defined in: packages/core/src/utils/Utils.ts:332

Last updated on by Martin Adámek