@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
Name | Type |
---|---|
count | number |
Returns
number
[]
compare
▸ compare(arrayA
, arrayB
): boolean
Compares if two arrays contain same elements.
Parameters
Name | Type |
---|---|
arrayA | any [] |
arrayB | any [] |
Returns
boolean
lastItem
▸ lastItem<T
>(array
): T
Returns the last item of an array
Type parameters
Name | Type |
---|---|
T | any |
Parameters
Name | Type |
---|---|
array | T [] |
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
Name | Type |
---|---|
T | extends object |
Parameters
Name | Type |
---|---|
collection | T [] |
propKey | string |
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
Name | Type |
---|---|
T | any |
Parameters
Name | Type |
---|---|
array | T [] |
Returns
T
[]
sortAlphaNumerically
▸ sortAlphaNumerically(a
, b
): number
Function for array.sort()
.
Parameters
Name | Type |
---|---|
a | string |
b | string |
Returns
number
sortAlphabetically
▸ sortAlphabetically(a
, b
): number
Function for array.sort()
.
Parameters
Name | Type |
---|---|
a | string |
b | string |
Returns
number
Entity Functions
pickByIdx
▸ pickByIdx(idx
): (child
: any
) => boolean
Finding function, for find()
iteration
Parameters
Name | Type |
---|---|
idx | number |
Returns
fn
▸ (child
): boolean
Parameters
Name | Type |
---|---|
child | any |
Returns
boolean
sortByIdx
▸ sortByIdx(a
, b
): number
Sorting function, for sort()
Parameters
Name | Type |
---|---|
a | any |
b | any |
Returns
number
Function Functions
noop
▸ noop(): void
Returns
void
runAll
▸ runAll(functions
): void
Parameters
Name | Type | Description |
---|---|---|
functions | Function [] | Array of functions to call one by one |
Returns
void
times
▸ times(length
, func
): void
Executes function multiple times
Parameters
Name | Type | Description |
---|---|---|
length | number | number of times function will be executed |
func | (idx : number ) => any | a function |
Returns
void
Number Functions
cm2px
▸ cm2px(value
): number
Parameters
Name | Type |
---|---|
value | number |
Returns
number
decimal
▸ decimal(value
, maxZeroes?
): number
Limits the number of digits "after comma"
Example
decimal(10.12345, 2)
// -> 10.12
Parameters
Name | Type | Default value |
---|---|---|
value | number | undefined |
maxZeroes | number | 2 |
Returns
number
deg2rad
▸ deg2rad(angle
): number
Converts degrees to radians
- discuss at: http://locutus.io/php/deg2rad/
- original by: Enrique Gonzalez
- improved by: Thomas Grainger (http://graingert.co.uk)
Example
deg2rad(45)
// -> 0.7853981633974483
Parameters
Name | Type |
---|---|
angle | number |
Returns
number
distance
▸ distance(ax
, ay
, bx
, by
): number
Parameters
Name | Type |
---|---|
ax | number |
ay | number |
bx | number |
by | number |
Returns
number
lerp
▸ lerp(a
, b
, t
): number
Parameters
Name | Type |
---|---|
a | number |
b | number |
t | number |
Returns
number
limit
▸ limit(val
, min?
, max?
): number
Limits val
to fin within range from min
to max
Parameters
Name | Type | Default value |
---|---|---|
val | number | undefined |
min | number | 0 |
max | number | 1 |
Returns
number
px2cm
▸ px2cm(value
): number
Parameters
Name | Type |
---|---|
value | number |
Returns
number
rad2deg
▸ rad2deg(angle
): number
Converts radians to degrees
- discuss at: http://locutus.io/php/rad2deg/
- original by: Enrique Gonzalez
- improved by: Brett Zamir (http://brett-zamir.me)
Example
rad2deg(3.141592653589793)
// -> 180
Parameters
Name | Type |
---|---|
angle | number |
Returns
number
wrap
▸ wrap(val
, max?
): number
Wraps val
around to fit within 0 and max
Parameters
Name | Type | Default value |
---|---|---|
val | number | undefined |
max | number | 1 |
Returns
number
Object Functions
deepClone
▸ deepClone(value
): any
Deprecated
use a deepmerge from npm or anything else
Parameters
Name | Type |
---|---|
value | unknown |
Returns
any
omit
▸ omit(object
, keys
): Record
<string
, any
>
Parameters
Name | Type |
---|---|
object | Record <string , any > |
keys | string [] |
Returns
Record
<string
, any
>
new object without provided keys
pick
▸ pick(object
, keys
): Record
<string
, any
>
Parameters
Name | Type |
---|---|
object | Record <string , any > |
keys | string [] |
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
Name | Type | Default value |
---|---|---|
sourceObject | Record <string , any > | undefined |
path | string | (string | number )[] | undefined |
separator | string | "." |
Returns
any
Random Functions
randomFloat
▸ randomFloat(min?
, max?
): number
Random float number in range min
to max
Parameters
Name | Type | Default value |
---|---|---|
min | number | 0 |
max | number | 1 |
Returns
number
randomInt
▸ randomInt(min?
, max?
): number
Random number in range min
to max
, without the remainder
Parameters
Name | Type | Default value |
---|---|---|
min | number | 0 |
max | number | 1 |
Returns
number
String Functions
camelCase
▸ camelCase(str?
): string
Convert string to "camelCase"
Parameters
Name | Type | Default value |
---|---|---|
str | string | "" |
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
Name | Type | Default value |
---|---|---|
str | string | "" |
Returns
string
trim
▸ trim(string?
, maxLength?
): string
Trim long string with nice ell… Works only on string, don't put numbers in it.
Parameters
Name | Type | Default value |
---|---|---|
string | string | "" |
maxLength | number | 7 |
Returns
string
Util Functions
applyMixins
▸ applyMixins(derivedCtor
, baseCtors
): void
Parameters
Name | Type |
---|---|
derivedCtor | AnyClass |
baseCtors | any [] |
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
Name | Type |
---|---|
value | unknown |
...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
Name | Type |
---|---|
...values | T [] |
Returns
T
isMap
▸ isMap(thing
): thing is Map<any, any>
Parameters
Name | Type |
---|---|
thing | unknown |
Returns
thing is Map<any, any>
isMapLike
▸ isMapLike(thing
): thing is Map<any, any>
Has somewhat same interface to native Map
Parameters
Name | Type |
---|---|
thing | unknown |
Returns
thing is Map<any, any>
isSet
▸ isSet(thing
): thing is Set<any>
Parameters
Name | Type |
---|---|
thing | unknown |
Returns
thing is Set<any>
timeout
▸ timeout(ms
): Promise
<unknown
>
Simple delay of execution. Use like this: await timeout(50)
Parameters
Name | Type | Description |
---|---|---|
ms | number | milliseconds |
Returns
Promise
<unknown
>