Skip to main content
Version: Next

Class: Utils

core.Utils

Constructors#

constructor#

new Utils()

Properties#

PK_SEPARATOR#

Static Readonly PK_SEPARATOR: "~~~"

Defined in#

packages/core/src/utils/Utils.ts:128

Methods#

absolutePath#

Static absolutePath(path, baseDir?): string

Parameters#

NameType
pathstring
baseDirstring

Returns#

string

Defined in#

packages/core/src/utils/Utils.ts:639


asArray#

Static asArray<T>(data?, strict?): T[]

Normalize the argument to always be an array.

Type parameters#

Name
T

Parameters#

NameTypeDefault value
data?T | T[]undefined
strictbooleanfalse

Returns#

T[]

Defined in#

packages/core/src/utils/Utils.ts:281


callCompiledFunction#

Static callCompiledFunction<T, R>(fn, ...args): R

Type parameters#

NameType
TT: unknown[]
RR

Parameters#

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

Returns#

R

Defined in#

packages/core/src/utils/Utils.ts:765


className#

Static className<T>(classOrName): 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:535


copy#

Static copy<T>(entity): T

Creates deep copy of given object.

Type parameters#

Name
T

Parameters#

NameType
entityT

Returns#

T

Defined in#

packages/core/src/utils/Utils.ts:274


createFunction#

Static createFunction(context, code): any

Parameters#

NameType
contextMap<string, any>
codestring

Returns#

any

Defined in#

packages/core/src/utils/Utils.ts:754


defaultValue#

Static defaultValue<T>(prop, option, defaultValue): void

Type parameters#

NameType
TT: Dictionary<any>

Parameters#

NameType
propT
optionkeyof T
defaultValueany

Returns#

void

Defined in#

packages/core/src/utils/Utils.ts:661


detectTsNode#

Static detectTsNode(): boolean

Tries to detect ts-node runtime.

Returns#

boolean

Defined in#

packages/core/src/utils/Utils.ts:546


diff#

Static diff(a, b): 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:257


equals#

Static equals(a, b): boolean

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

Parameters#

NameType
aany
bany

Returns#

boolean

Defined in#

packages/core/src/utils/Utils.ts:200


extractEnumValues#

Static extractEnumValues(target): (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:687


extractPK#

Static extractPK<T>(data, meta?, strict?): null | string | Primary<T>

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

Type parameters#

NameType
TT: AnyEntity<T>

Parameters#

NameTypeDefault value
dataanyundefined
meta?EntityMetadata<T>undefined
strictbooleanfalse

Returns#

null | string | Primary<T>

Defined in#

packages/core/src/utils/Utils.ts:377


findDuplicates#

Static findDuplicates<T>(items): T[]

Type parameters#

Name
T

Parameters#

NameType
itemsT[]

Returns#

T[]

Defined in#

packages/core/src/utils/Utils.ts:665


flatten#

Static flatten<T>(arrays): T[]

Type parameters#

Name
T

Parameters#

NameType
arraysT[][]

Returns#

T[]

Defined in#

packages/core/src/utils/Utils.ts:703


getCompositeKeyHash#

Static getCompositeKeyHash<T>(entity, meta): string

Type parameters#

NameType
TT: AnyEntity<T>

Parameters#

NameType
entityT
metaEntityMetadata<T>

Returns#

string

Defined in#

packages/core/src/utils/Utils.ts:401


getGlobalStorage#

Static getGlobalStorage(namespace): Dictionary<any>

Parameters#

NameType
namespacestring

Returns#

Dictionary<any>

Defined in#

packages/core/src/utils/Utils.ts:719


getORMVersion#

Static getORMVersion(): string

Returns#

string

Defined in#

packages/core/src/utils/Utils.ts:740


getObjectKeysSize#

Static getObjectKeysSize(object): 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:155


getObjectType#

Static getObjectType(value): string

Gets the type of the argument.

Parameters#

NameType
valueany

Returns#

string

Defined in#

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


getOrderedPrimaryKeys#

Static getOrderedPrimaryKeys<T>(id, meta, platform?, convertCustomTypes?): Primary<T>[]

Type parameters#

NameType
TT: AnyEntity<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:475


getParamNames#

Static getParamNames(func, methodName?): 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:309


getPrimaryKeyCond#

Static getPrimaryKeyCond<T>(entity, primaryKeys): null | Record<string, Primary<T>>

Type parameters#

NameType
TT: AnyEntity<T>

Parameters#

NameType
entityT
primaryKeysstring[]

Returns#

null | Record<string, Primary<T>>

Defined in#

packages/core/src/utils/Utils.ts:455


getPrimaryKeyCondFromArray#

Static getPrimaryKeyCondFromArray<T>(pks, primaryKeys): Record<string, Primary<T>>

Type parameters#

NameType
TT: AnyEntity<T>

Parameters#

NameType
pksPrimary<T>[]
primaryKeysstring[]

Returns#

Record<string, Primary<T>>

Defined in#

packages/core/src/utils/Utils.ts:468


getPrimaryKeyHash#

Static getPrimaryKeyHash(pks): string

Parameters#

NameType
pksstring[]

Returns#

string

Defined in#

packages/core/src/utils/Utils.ts:421


getPrimaryKeyValues#

Static getPrimaryKeyValues<T>(entity, primaryKeys, allowScalar?, convertCustomTypes?): any

Type parameters#

NameType
TT: AnyEntity<T>

Parameters#

NameTypeDefault value
entityTundefined
primaryKeysstring[]undefined
allowScalarbooleanfalse
convertCustomTypesbooleanfalse

Returns#

any

Defined in#

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


getRootEntity#

Static getRootEntity(metadata, meta): EntityMetadata<any>

Parameters#

NameType
metadataIMetadataStorage
metaEntityMetadata<any>

Returns#

EntityMetadata<any>

Defined in#

packages/core/src/utils/Utils.ts:238


hasObjectKeys#

Static hasObjectKeys(object): 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:172


hash#

Static hash(data): string

Parameters#

NameType
datastring

Returns#

string

Defined in#

packages/core/src/utils/Utils.ts:651


isCollection#

Static isCollection<T, O>(item, prop?, type?): item is Collection<T, O>

Type parameters#

NameType
TT
OO = unknown

Parameters#

NameType
itemany
prop?EntityProperty<T>
type?ReferenceType

Returns#

item is Collection<T, O>

Defined in#

packages/core/src/utils/Utils.ts:608


isDefined#

Static isDefined<T>(data, considerNullUndefined?): data is T

Checks if the argument is not undefined

Type parameters#

NameType
TT = Record<string, unknown>

Parameters#

NameTypeDefault value
dataanyundefined
considerNullUndefinedbooleanfalse

Returns#

data is T

Defined in#

packages/core/src/utils/Utils.ts:133


isEmpty#

Static isEmpty(data): 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:520


isEntity#

Static isEntity<T>(data, allowReference?): data is T

Checks whether given object is an entity instance.

Type parameters#

NameType
TT = AnyEntity<any>

Parameters#

NameTypeDefault value
dataanyundefined
allowReferencebooleanfalse

Returns#

data is T

Defined in#

packages/core/src/utils/Utils.ts:498


isGroupOperator#

Static isGroupOperator(key): boolean

Parameters#

NameType
keystring

Returns#

boolean

Defined in#

packages/core/src/utils/Utils.ts:715


isNotObject#

Static isNotObject<T>(o, not): o is T

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

Type parameters#

NameType
TT = Dictionary<any>

Parameters#

NameType
oany
notany[]

Returns#

o is T

Defined in#

packages/core/src/utils/Utils.ts:147


isNumber#

Static isNumber<T>(s): s is T

Checks if the argument is number

Type parameters#

NameType
TT = number

Parameters#

NameType
sany

Returns#

s is T

Defined in#

packages/core/src/utils/Utils.ts:193


isObject#

Static isObject<T>(o): o is T

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

Type parameters#

NameType
TT = Dictionary<any>

Parameters#

NameType
oany

Returns#

o is T

Defined in#

packages/core/src/utils/Utils.ts:140


isObjectID#

Static isObjectID(key): boolean

Checks whether the argument is ObjectId instance

Parameters#

NameType
keyany

Returns#

boolean

Defined in#

packages/core/src/utils/Utils.ts:513


isOperator#

Static isOperator(key, includeGroupOperators?): boolean

Parameters#

NameTypeDefault value
keystringundefined
includeGroupOperatorsbooleantrue

Returns#

boolean

Defined in#

packages/core/src/utils/Utils.ts:707


isPlainObject#

Static isPlainObject(value): 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:590


isPrimaryKey#

Static isPrimaryKey<T>(key, allowComposite?): key is Primary<T>

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

Type parameters#

Name
T

Parameters#

NameTypeDefault value
keyanyundefined
allowCompositebooleanfalse

Returns#

key is Primary<T>

Defined in#

packages/core/src/utils/Utils.ts:362


isString#

Static isString(s): s is string

Checks if the argument is string

Parameters#

NameType
sany

Returns#

s is string

Defined in#

packages/core/src/utils/Utils.ts:186


lookupPathFromDecorator#

Static lookupPathFromDecorator(name, stack?): 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:558


merge#

Static merge(target, ...sources): any

Merges all sources into the target recursively.

Parameters#

NameType
targetany
...sourcesany[]

Returns#

any

Defined in#

packages/core/src/utils/Utils.ts:214


normalizePath#

Static normalizePath(...parts): string

Parameters#

NameType
...partsstring[]

Returns#

string

Defined in#

packages/core/src/utils/Utils.ts:616


pathExists#

Static pathExists(path, options?): Promise<boolean>

Parameters#

NameType
pathstring
optionsGlobbyOptions

Returns#

Promise<boolean>

Defined in#

packages/core/src/utils/Utils.ts:675


propertyDecoratorReturnValue#

Static propertyDecoratorReturnValue(): any

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

Returns#

any

Defined in#

packages/core/src/utils/Utils.ts:781


randomInt#

Static randomInt(min, max): number

Parameters#

NameType
minnumber
maxnumber

Returns#

number

Defined in#

packages/core/src/utils/Utils.ts:671


relativePath#

Static relativePath(path, relativeTo): string

Parameters#

NameType
pathstring
relativeTostring

Returns#

string

Defined in#

packages/core/src/utils/Utils.ts:623


renameKey#

Static renameKey<T>(payload, from, to): 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:296


requireFrom#

Static requireFrom(id, from): 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:731


runIfNotEmpty#

Static runIfNotEmpty(clause, data): void

Parameters#

NameType
clause() => any
dataany

Returns#

void

Defined in#

packages/core/src/utils/Utils.ts:655


runSerial#

Static runSerial<T, U>(items, cb): Promise<T[]>

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

Type parameters#

NameType
TT = any
UU = any

Parameters#

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

Returns#

Promise<T[]>

Defined in#

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


splitPrimaryKeys#

Static splitPrimaryKeys(key): string[]

Parameters#

NameType
keystring

Returns#

string[]

Defined in#

packages/core/src/utils/Utils.ts:425


unique#

Static unique<T>(items): T[]

Gets array without duplicates.

Type parameters#

NameType
TT = string

Parameters#

NameType
itemsT[]

Returns#

T[]

Defined in#

packages/core/src/utils/Utils.ts:207