Options
All
  • Public
  • Public/Protected
  • All
Menu

Module Utilities

Random collection of utilities functions/classes.

Index

Functions

boxBoxCollision

  • boxBoxCollision(oneX: number, oneY: number, oneWidth: number, oneHeight: number, twoX: number, twoY: number, twoWidth: number, twoHeight: number): boolean
  • Detects collision between 2 boxes.

    Parameters

    • oneX: number
    • oneY: number
    • oneWidth: number
    • oneHeight: number
    • twoX: number
    • twoY: number
    • twoWidth: number
    • twoHeight: number

    Returns boolean

calculateAngle

  • calculateAngle(aX: number, aY: number, bX: number, bY: number): number
  • Returns the angle between 2 points in radians. Positive in clockwise direction.

    Throws an Error exception if:

    • any of the arguments isn't a number.

    Parameters

    • aX: number
    • aY: number
    • bX: number
    • bY: number

    Returns number

calculateDistance

  • calculateDistance(aX: number, aY: number, bX: number, bY: number): number
  • Distance between 2 points.

    Throws an Error exception if:

    • any of the arguments isn't a number.

    Parameters

    • aX: number
    • aY: number
    • bX: number
    • bY: number

    Returns number

circleCircleCollision

  • circleCircleCollision(x1: number, y1: number, radius1: number, x2: number, y2: number, radius2: number): boolean
  • Detects collision between two circles.

    Parameters

    • x1: number
    • y1: number
    • radius1: number
    • x2: number
    • y2: number
    • radius2: number

    Returns boolean

circlePointCollision

  • circlePointCollision(circleX: number, circleY: number, circleRadius: number, pointX: number, pointY: number): boolean
  • Detects collision between a circle and a point.

    Parameters

    • circleX: number
    • circleY: number
    • circleRadius: number
    • pointX: number
    • pointY: number

    Returns boolean

createEnum

  • createEnum(values: string[], start?: undefined | number): object
  • Enum - A way to associate a string name to a number.

    Parameters

    • values: string[]

      The enum names. Each name will have an associated number.

    • Optional start: undefined | number

      Starting number for the first name. The number is incremented by one for the next name.

    Returns object

    • [key: string]: string | number

deepClone

  • deepClone(obj: any): any
  • Returns a deep clone/copy of the object.

    Parameters

    • obj: any

    Returns any

getObject

  • getObject(key: string): any
  • Returns an object that was obtained by parsing (with json) some data that was saved on localStorage.

    Throws an Error exception if:

    • key is not a string.
    • key wasn't found.

    Parameters

    • key: string

    Returns any

getRandomFloat

  • getRandomFloat(min: number, max: number): number
  • Returns a random float number between min and max (inclusive).

    Throws an Error exception if:

    • either min or max is not a number.
    • the minimum value is bigger than the maximum.

    Parameters

    • min: number
    • max: number

    Returns number

getRandomInt

  • getRandomInt(min: number, max: number): number
  • Returns a random integer number between min and max (inclusive).

    Throws an Error exception if:

    • min or max isn't an integer.
    • the minimum value is bigger than the maximum.

    Parameters

    • min: number
    • max: number

    Returns number

getSeveralRandomInts

  • getSeveralRandomInts(min: number, max: number, howMany: number): number[]
  • Returns several different random integers, in the range between min and max (inclusive).

    Throws an Error exception if:

    • min, max or howMany isn't an integer.
    • the minimum value is bigger than the maximum.
    • the range is less than the number of integers required.

    Parameters

    • min: number
    • max: number
    • howMany: number

    Returns number[]

isArray

  • isArray(element: any): boolean
  • Parameters

    • element: any

    Returns boolean

    If it is an array or not.

isBoolean

  • isBoolean(element: any): boolean
  • Parameters

    • element: any

    Returns boolean

    If it is a boolean.

isFunction

  • isFunction(element: any): boolean
  • Parameters

    • element: any

    Returns boolean

    If it is a function.

isInteger

  • isInteger(value: any): boolean
  • Parameters

    • value: any

    Returns boolean

    If it is an integer.

isNumber

  • isNumber(element: any): boolean
  • Parameters

    • element: any

    Returns boolean

    If it is a number.

isString

  • isString(element: any): boolean
  • Parameters

    • element: any

    Returns boolean

    If it is a string.

numberOfDigits

  • numberOfDigits(theNumber: number): number
  • Returns the number of digits in a number. It doesn't consider the minus signal, nor the dot (in floats) as a digit.

    Throws an Error exception if:

    • the argument is not a number.

    Parameters

    • theNumber: number

    Returns number

pointBoxCollision

  • pointBoxCollision(pointX: number, pointY: number, boxX: number, boxY: number, boxWidth: number, boxHeight: number): boolean
  • Detects collision between a point and a box.

    Parameters

    • pointX: number
    • pointY: number
    • boxX: number
    • boxY: number
    • boxWidth: number
    • boxHeight: number

    Returns boolean

round

  • round(num: number, dec: number): number
  • Rounds a number to a specified decimal case.

    Throws an Error exception if:

    • num isn't a number.
    • dec isn't an integer.
    • dec is less than 0.

    Parameters

    • num: number
    • dec: number

    Returns number

saveObject

  • saveObject(key: string, value: any): void
  • Saves in the localStorage a json string representation of the value.

    Throws an Error exception if:

    • key is not a string.

    Parameters

    • key: string
    • value: any

    Returns void

shuffle

  • shuffle(array: any[]): Array<any>
  • Shuffle an array.

    Parameters

    • array: any[]

    Returns Array<any>

timeToString

  • timeToString(dateMilliseconds: number, totalUnits?: number): string
  • Converts a time (in milliseconds) to a string (with the number of days/hours...).

    The number of units to be shown can be set (days/hours, or hours/minutes or minutes/seconds, and not days/hours/minutes for example (for a totalUnits of 2)).

    The units available are: day/hour/minute/second.

    Throws an Error exception if:

    • the dateMilliseconds argument isn't a number.

    Parameters

    • dateMilliseconds: number
    • Default value totalUnits: number = 2

    Returns string

toDegrees

  • toDegrees(radians: number): number
  • Converts a number in radians to degrees and returns it.

    Throws an Error exception if:

    • the argument isn't a number.

    Parameters

    • radians: number

    Returns number

toRadians

  • toRadians(degrees: number): number
  • Converts a number in degrees to radians and returns it.

    Throws an Error exception if:

    • the argument isn't a number.

    Parameters

    • degrees: number

    Returns number

Object literals

KEY_CODE

KEY_CODE: object

Numeric code of each key.

0

0: number = 48

1

1: number = 49

2

2: number = 50

3

3: number = 51

4

4: number = 52

5

5: number = 53

6

6: number = 54

7

7: number = 55

8

8: number = 56

9

9: number = 57

a

a: number = 65

b

b: number = 66

backspace

backspace: number = 8

c

c: number = 67

d

d: number = 68

del

del: number = 46

downArrow

downArrow: number = 40

e

e: number = 69

end

end: number = 35

enter

enter: number = 13

esc

esc: number = 27

f

f: number = 70

f1

f1: number = 112

f10

f10: number = 121

f11

f11: number = 122

f12

f12: number = 123

f2

f2: number = 113

f3

f3: number = 114

f4

f4: number = 115

f5

f5: number = 116

f6

f6: number = 117

f7

f7: number = 118

f8

f8: number = 119

f9

f9: number = 120

g

g: number = 71

h

h: number = 72

home

home: number = 36

i

i: number = 73

j

j: number = 74

k

k: number = 75

l

l: number = 76

leftArrow

leftArrow: number = 37

m

m: number = 77

n

n: number = 78

o

o: number = 79

p

p: number = 80

q

q: number = 81

r

r: number = 82

rightArrow

rightArrow: number = 39

s

s: number = 83

space

space: number = 32

t

t: number = 84

tab

tab: number = 9

u

u: number = 85

upArrow

upArrow: number = 38

v

v: number = 86

w

w: number = 87

x

x: number = 88

y

y: number = 89

z

z: number = 90

MOUSE_CODE

MOUSE_CODE: object

Numeric code of each mouse button.

left

left: number = 0

middle

middle: number = 1

right

right: number = 2

Generated using TypeDoc