Skip to main content

@cardsgame/utils

Classes

Array Functions

Entity Functions

Function Functions

Number Functions

Object Functions

Random Functions

String Functions

Util Functions

Array Functions

arrayWith

arrayWith(count): number[]

Returns an array which holds count items, each being the index number starting from 0.

Parameters

NameType
countnumber

Returns

number[]


compare

compare(arrayA, arrayB): boolean

Compares if two arrays contain same elements.

Parameters

NameType
arrayAany[]
arrayBany[]

Returns

boolean


lastItem

lastItem<T>(array): T

Returns the last item of an array

Type parameters

NameType
Tany

Parameters

NameType
arrayT[]

Returns

T


pickMostCommonProp

pickMostCommonProp<T>(collection, propKey, condition?): [string, number]

Grabs you most common propKey in your collection of T, additionally filtering out items which match given optional condition.

Most useful when creating BotNeurons

Example

const [rank] = pickMostCommonProp(myCards, "rank")

Type parameters

NameType
Textends object

Parameters

NameType
collectionT[]
propKeystring
condition?(arg0: T) => boolean

Returns

[string, number]

a tuple of [propValue, count]


shuffle

shuffle<T>(array): T[]

Returns new array with items shuffled around.

Type parameters

NameType
Tany

Parameters

NameType
arrayT[]

Returns

T[]


sortAlphaNumerically

sortAlphaNumerically(a, b): number

Function for array.sort().

Parameters

NameType
astring
bstring

Returns

number


sortAlphabetically

sortAlphabetically(a, b): number

Function for array.sort().

Parameters

NameType
astring
bstring

Returns

number


Entity Functions

pickByIdx

pickByIdx(idx): (child: any) => boolean

Finding function, for find() iteration

Parameters

NameType
idxnumber

Returns

fn

▸ (child): boolean

Parameters
NameType
childany
Returns

boolean


sortByIdx

sortByIdx(a, b): number

Sorting function, for sort()

Parameters

NameType
aany
bany

Returns

number


Function Functions

noop

noop(): void

Returns

void


runAll

runAll(functions): void

Parameters

NameTypeDescription
functionsFunction[]Array of functions to call one by one

Returns

void


times

times(length, func): void

Executes function multiple times

Parameters

NameTypeDescription
lengthnumbernumber of times function will be executed
func(idx: number) => anya function

Returns

void


Number Functions

cm2px

cm2px(value): number

Parameters

NameType
valuenumber

Returns

number


decimal

decimal(value, maxZeroes?): number

Limits the number of digits "after comma"

Example

decimal(10.12345, 2)
// -> 10.12

Parameters

NameTypeDefault value
valuenumberundefined
maxZeroesnumber2

Returns

number


deg2rad

deg2rad(angle): number

Converts degrees to radians

Example

deg2rad(45)
// -> 0.7853981633974483

Parameters

NameType
anglenumber

Returns

number


distance

distance(ax, ay, bx, by): number

Parameters

NameType
axnumber
aynumber
bxnumber
bynumber

Returns

number


lerp

lerp(a, b, t): number

Parameters

NameType
anumber
bnumber
tnumber

Returns

number


limit

limit(val, min?, max?): number

Limits val to fin within range from min to max

Parameters

NameTypeDefault value
valnumberundefined
minnumber0
maxnumber1

Returns

number


px2cm

px2cm(value): number

Parameters

NameType
valuenumber

Returns

number


rad2deg

rad2deg(angle): number

Converts radians to degrees

Example

rad2deg(3.141592653589793)
// -> 180

Parameters

NameType
anglenumber

Returns

number


wrap

wrap(val, max?): number

Wraps val around to fit within 0 and max

Parameters

NameTypeDefault value
valnumberundefined
maxnumber1

Returns

number


Object Functions

deepClone

deepClone(value): any

Deprecated

use a deepmerge from npm or anything else

Parameters

NameType
valueunknown

Returns

any


omit

omit(object, keys): Record<string, any>

Parameters

NameType
objectRecord<string, any>
keysstring[]

Returns

Record<string, any>

new object without provided keys


pick

pick(object, keys): Record<string, any>

Parameters

NameType
objectRecord<string, any>
keysstring[]

Returns

Record<string, any>

new object only with provided keys


resolve

resolve(sourceObject, path, separator?): any

Resolves target object/property given source object and path.

Parameters

NameTypeDefault value
sourceObjectRecord<string, any>undefined
pathstring | (string | number)[]undefined
separatorstring"."

Returns

any


Random Functions

randomFloat

randomFloat(min?, max?): number

Random float number in range min to max

Parameters

NameTypeDefault value
minnumber0
maxnumber1

Returns

number


randomInt

randomInt(min?, max?): number

Random number in range min to max, without the remainder

Parameters

NameTypeDefault value
minnumber0
maxnumber1

Returns

number


String Functions

camelCase

camelCase(str?): string

Convert string to "camelCase"

Parameters

NameTypeDefault value
strstring""

Returns

string


randomName

randomName(): string

For now it's just 3 random letters

Returns

string


sentenceCase

sentenceCase(str?): string

Convert string to "SentenceCase" (first letter capital)

Parameters

NameTypeDefault value
strstring""

Returns

string


trim

trim(string?, maxLength?): string

Trim long string with nice ell… Works only on string, don't put numbers in it.

Parameters

NameTypeDefault value
stringstring""
maxLengthnumber7

Returns

string


Util Functions

applyMixins

applyMixins(derivedCtor, baseCtors): void

Parameters

NameType
derivedCtorAnyClass
baseCtorsany[]

Returns

void


compose

compose<T>(value, ...functions): T

Calls each function with the current argument and its result is used for the next call

Type parameters

Name
T

Parameters

NameType
valueunknown
...functions(...args: any[]) => any[]

Returns

T


def

def<T>(...values): T

Returns first, defined value

Example

const options = {}

def(options.value, "default")
// -> "default"

Type parameters

Name
T

Parameters

NameType
...valuesT[]

Returns

T


isMap

isMap(thing): thing is Map<any, any>

Parameters

NameType
thingunknown

Returns

thing is Map<any, any>


isMapLike

isMapLike(thing): thing is Map<any, any>

Has somewhat same interface to native Map

Parameters

NameType
thingunknown

Returns

thing is Map<any, any>


isSet

isSet(thing): thing is Set<any>

Parameters

NameType
thingunknown

Returns

thing is Set<any>


timeout

timeout(ms): Promise<unknown>

Simple delay of execution. Use like this: await timeout(50)

Parameters

NameTypeDescription
msnumbermilliseconds

Returns

Promise<unknown>