Version: 4.3

Class: Utils

Hierarchy

  • Utils

Properties

PK_SEPARATOR

Static Readonly PK_SEPARATOR: "~" = "~"

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

Methods

absolutePath

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

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

Parameters:

NameTypeDefault value
pathstring-
baseDirstringprocess.cwd()

Returns: string


asArray

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

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

Normalize the argument to always be an array.

Type parameters:

Name
T

Parameters:

NameTypeDefault value
data?T | T[]-
strictbooleanfalse

Returns: T[]


callCompiledFunction

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

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

Type parameters:

NameType
Tunknown[]
R-

Parameters:

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

Returns: R


className

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

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

Gets string name of given class.

Type parameters:

Name
T

Parameters:

NameType
classOrNameEntityName<T>

Returns: string


copy

Staticcopy<T>(entity: T): T

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

Creates deep copy of given entity.

Type parameters:

Name
T

Parameters:

NameType
entityT

Returns: T


createFunction

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

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

Parameters:

NameType
contextMap<string, any>
codestring

Returns: any


defaultValue

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

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

Type parameters:

NameType
TDictionary

Parameters:

NameType
propT
optionkeyof T
defaultValueany

Returns: void


detectTsNode

StaticdetectTsNode(): boolean

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

Tries to detect ts-node runtime.

Returns: boolean


diff

Staticdiff(a: Dictionary, b: Dictionary): Record<keyof typeof a & typeof b, any>

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

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

Parameters:

NameType
aDictionary
bDictionary

Returns: Record<keyof typeof a & typeof b, any>


equals

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

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

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

Parameters:

NameType
aany
bany

Returns: boolean


extractEnumValues

StaticextractEnumValues(target: Dictionary): (string | number)[]

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

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

Parameters:

NameType
targetDictionary

Returns: (string | number)[]


extractPK

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

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

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: Primary<T> | string | null


findDuplicates

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

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

Type parameters:

Name
T

Parameters:

NameType
itemsT[]

Returns: T[]


flatten

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

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

Type parameters:

Name
T

Parameters:

NameType
arraysT[][]

Returns: T[]


getCompositeKeyHash

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

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

Type parameters:

NameType
TAnyEntity<T>

Parameters:

NameType
entityT
metaEntityMetadata<T>

Returns: string


getGlobalStorage

StaticgetGlobalStorage(namespace: string): Dictionary

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

Parameters:

NameType
namespacestring

Returns: Dictionary


getORMVersion

StaticgetORMVersion(): string

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

Returns: string


getObjectKeysSize

StaticgetObjectKeysSize(object: Dictionary): number

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

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

Returns: number


getObjectType

StaticgetObjectType(value: any): string

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

Gets the type of the argument.

Parameters:

NameType
valueany

Returns: string


getOrderedPrimaryKeys

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

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

Type parameters:

NameType
TAnyEntity<T>

Parameters:

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

Returns: Primary<T>[]


getParamNames

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

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

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

Parameters:

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

Returns: string[]


getPrimaryKeyCond

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

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

Type parameters:

NameType
TAnyEntity<T>

Parameters:

NameType
entityT
primaryKeysstring[]

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


getPrimaryKeyCondFromArray

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

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

Type parameters:

NameType
TAnyEntity<T>

Parameters:

NameType
pksPrimary<T>[]
primaryKeysstring[]

Returns: Record<string, Primary<T>>


getPrimaryKeyHash

StaticgetPrimaryKeyHash(pks: string[]): string

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

Parameters:

NameType
pksstring[]

Returns: string


getPrimaryKeyValues

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

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

Type parameters:

NameType
TAnyEntity<T>

Parameters:

NameTypeDefault value
entityT-
primaryKeysstring[]-
allowScalarbooleanfalse

Returns: any


getRootEntity

StaticgetRootEntity(metadata: IMetadataStorage, meta: EntityMetadata): EntityMetadata

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

Parameters:

NameType
metadataIMetadataStorage
metaEntityMetadata

Returns: EntityMetadata


hasObjectKeys

StatichasObjectKeys(object: Dictionary): boolean

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

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

Returns: boolean


hash

Statichash(data: string): string

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

Parameters:

NameType
datastring

Returns: string


isCollection

StaticisCollection<T, O>(item: any, prop?: EntityProperty<T>, type?: ReferenceType): item is Collection<T, O>

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

Type parameters:

NameDefault
T-
Ounknown

Parameters:

NameType
itemany
prop?EntityProperty<T>
type?ReferenceType

Returns: item is Collection<T, O>


isDefined

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

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

Checks if the argument is not undefined

Type parameters:

NameDefault
TRecord\<string, unknown>

Parameters:

NameTypeDefault value
dataany-
considerNullUndefinedbooleanfalse

Returns: data is T


isEmpty

StaticisEmpty(data: any): boolean

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

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

Parameters:

NameType
dataany

Returns: boolean


isEntity

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

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

Checks whether given object is an entity instance.

Type parameters:

NameDefault
TAnyEntity

Parameters:

NameTypeDefault value
dataany-
allowReferencebooleanfalse

Returns: data is T


isGroupOperator

StaticisGroupOperator(key: string): boolean

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

Parameters:

NameType
keystring

Returns: boolean


isNotObject

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

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

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

Type parameters:

NameDefault
TDictionary

Parameters:

NameType
oany
notany[]

Returns: o is T


isNumber

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

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

Checks if the argument is number

Type parameters:

NameDefault
Tnumber

Parameters:

NameType
sany

Returns: s is T


isObject

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

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

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

Type parameters:

NameDefault
TDictionary

Parameters:

NameType
oany

Returns: o is T


isObjectID

StaticisObjectID(key: any): boolean

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

Checks whether the argument is ObjectId instance

Parameters:

NameType
keyany

Returns: boolean


isOperator

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

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

Parameters:

NameTypeDefault value
keystring-
includeGroupOperatorsbooleantrue

Returns: boolean


isPlainObject

StaticisPlainObject(value: any): value is Dictionary

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

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

Parameters:

NameType
valueany

Returns: value is Dictionary


isPrimaryKey

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

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

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>


isString

StaticisString(s: any): s is string

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

Checks if the argument is string

Parameters:

NameType
sany

Returns: s is string


lookupPathFromDecorator

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

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

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


merge

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

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

Merges all sources into the target recursively.

Parameters:

NameType
targetany
...sourcesany[]

Returns: any


normalizePath

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

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

Parameters:

NameType
...partsstring[]

Returns: string


pathExists

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

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

Parameters:

NameTypeDefault value
pathstring-
optionsGlobbyOptions{}

Returns: Promise<boolean>


propertyDecoratorReturnValue

StaticpropertyDecoratorReturnValue(): any

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

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

Returns: any


randomInt

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

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

Parameters:

NameType
minnumber
maxnumber

Returns: number


relativePath

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

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

Parameters:

NameType
pathstring
relativeTostring

Returns: string


renameKey

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

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

Renames object key, keeps order of properties.

Type parameters:

Name
T

Parameters:

NameType
payloadT
fromstring | keyof T
tostring

Returns: void


requireFrom

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

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

Require a module from a specific location

Parameters:

NameTypeDescription
idstringThe module to require
fromstringLocation to start the node resolution

Returns: any


runIfNotEmpty

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

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

Parameters:

NameType
clause() => any
dataany

Returns: void


runSerial

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

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

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[]>


splitPrimaryKeys

StaticsplitPrimaryKeys(key: string): string[]

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

Parameters:

NameType
keystring

Returns: string[]


unique

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

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

Gets array without duplicates.

Type parameters:

NameDefault
Tstring

Parameters:

NameType
itemsT[]

Returns: T[]


walkNode

Static PrivatewalkNode(node: Dictionary, checkNode: (node: Dictionary) => void): void

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

Parameters:

NameType
nodeDictionary
checkNode(node: Dictionary) => void

Returns: void

Last updated on by Martin Adámek