Global

Members

# (constant) markr

Simple wrapper for chalk

Since:
  • v2.2.0
Example
await markr.info('Hello world')
await markr.success('Hello world')
await markr.warn('Hello world')
await markr.warn('Hello world')

# (constant) spinner

Namespace for console spinner (Node.js only)

Since:
  • v2.1.1

Methods

# abbr(string, keepCaseopt)

Abbreviates a given string

Parameters:
NameTypeAttributesDefaultDescription
stringstring

The string to abbreviate

keepCaseboolean<optional>
false

Whether to keep the case of the string (eg lowercase to uppercase, or unchanged)

Since:
  • v3.0.0
Returns:

string

Example
abbr('Attack of the Clones') // => AOTC
abbr('Attack of the Clones', true) // => AotC

# afterEl(array, el)

Gets the values of an array after the specified element, not including the element at the position specified.

Parameters:
NameTypeDescription
arrayarray

The array to get the value from

el*

The element in the array that values will be taken from after

Since:
  • v1.1.2
Returns:

Array

Example
afterEl([1, 2, 3, "hello", "5", 6, 7, 8], "hello")

# afterPos(array, position)

Gets the values of an array before the specified position, not including the element at the position specified.

Parameters:
NameTypeDescription
arrayarray

The array to get the value from

positionnumber

The position that values will be retrieved from after (let's say position is x, this function will return all values after position x)

Since:
  • v1.1.2
Returns:

Array

Example
beforePos([1, 2, 3, 4, 5, 6, 7, 8], 4)

# arrayify(value)

Turns a value into an array

Parameters:
NameTypeDescription
value*

The value to add to a new array

Since:
  • v0.1.3
Returns:

Array

Example
arrayify('Hello')       //=> ['Hello']

# assert(condition, message, throwErropt)

A simple assert function

Parameters:
NameTypeAttributesDefaultDescription
conditionexpression

The condition for the check to pass, for example typeof argumentName === "string"

messagestringCheck failed

The message to throw as an error if the check does not pass

throwErrboolean<optional>
true

The message to throw as an error if the check does not pass

Since:
  • v1.0.1
Returns:

Array

Example
assert(typeof 'object' === 'object', '', false)  // this does not crash the script and does not show any errors

# atob(string)

Encodes a string into Base 64

Parameters:
NameTypeDescription
stringstring

The value convert to Base 64

Since:
  • v0.1.3
Returns:

string

Example
atob('hello')       //=> aGVsbG8=

# beforeEl(array, position)

Gets the values of an array before the specified element, not including the element at the position specified.

Parameters:
NameTypeDescription
arrayarray

The array to get the value from

positionnumber

The element in the array that values will be taken from before

Since:
  • v1.1.2
Returns:

Array

Example
beforeEl([1, 2, 3, "four", 5, "6", 7, 8], "four")

# beforePos(array, position)

Gets the values of an array before the specified position, not including the element at the position specified.

Parameters:
NameTypeDescription
arrayarray

The array to get the value from

positionnumber

The position that values will be retrieved from before (let's say position is x, this function will return all values before position x). Remember that an array starts at 0, so x in the example can be 1, which will return the first value in the array

Since:
  • v1.1.2
Returns:

Array

Example
beforePos([1, 2, 3, 4, 5, 6, 7, 8], 4)

# binarySearch(array, key) → {number}

Conducts a binary search on an array with the given key and returns the index of the key. The return behaviour of this function is the same as Array.prototype.indexOf().

Parameters:
NameTypeDescription
arrayArray.<any>

The array to get the index of the given key from. IMPORTANT NOTE: The array provided must be sorted

key*

The key to search for

Since:
  • v3.10.0
Returns:
Type
number
Example
binarySearch

# btoa(string)

Dencodes a Base 64 string

Parameters:
NameTypeDescription
stringstring

The value decode

Since:
  • v0.1.3
Returns:

string

Example
btoa('aGVsbG8=')       //=> hello

# camelCase(string)

Turns a string into camel case

Parameters:
NameTypeDescription
stringstring

The string to turn into camel case

Since:
  • v1.1.3
Returns:

string

Example
camelCase('Hello there') //=> 'helloThere'

# chunk(array, lengthopt)

Splits an array into chunks

Parameters:
NameTypeAttributesDefaultDescription
arrayarray

The array to split

lengthnumber<optional>
1

The number of elements in each chunk

Since:
  • v2.5.2-beta.5
Returns:

array - The array of chunks

Example
chunk(['a', 'b', 'c', 'd'], 2);

# circleArea(radius, piopt) → {number}

Gets the area of a circle with a given radius

Parameters:
NameTypeAttributesDefaultDescription
radiusnumber

The radius of the circle

pinumber<optional>
Math.PI

The value of pi

Since:
  • v3.8.0
Returns:
Type
number
Example
circleArea(9) // => 254.46900494077323

# compact(string)

Removes line breaks, trims whitespaces, and reduces all spaces with more than 1 space to 1 space

Parameters:
NameTypeDescription
stringstring

The string to compact

Since:
  • v0.4.4
Returns:

string

Example
let str = `I'm
a             multiline
    string!`
compact(str)    //=> "I'm a multiline string"

# deMorse(string)

Converts morse code to string

Parameters:
NameTypeDescription
stringstring

The string to convert

Since:
  • v2.5.2-beta.8
Returns:

string

Example
deMorse('... --- ... / .... . .-.. .--.')

# diff(arr1, arr2) → {Array}

Finds the difference between two arrays

Parameters:
NameTypeDescription
arr1Array

The first array to compare

arr2Array

The second array to compare

Since:
  • v2.5.2-beta.4
Returns:
Type
Array
Example
const arr1 = [1,2,3];
const arr2 = [2,3];
diff(arr1, arr2); //=> [1]

# ellipsis(string, position, options)

Splits the text at the specified character position (number) and replace the text behind it with an ellipsis (...)

Parameters:
NameTypeDescription
stringstring

The string to edit

positionnumber

The position to split the string at

optionsobject

The options to pass to the function

Properties
NameTypeAttributesDefaultDescription
ellipsisstring<optional>
...

What the ellipsis should be

Since:
  • v0.4.9
Returns:

string

Example
ellipsis('Hello! I am Joe!', 5, { ellipsis: '....' }) //=> 'Hello....'

# fill(arr, value, startopt, endopt) → {Array}

Fills elements of array with value from start up to, but not including, end.

Parameters:
NameTypeAttributesDefaultDescription
arrArray

The array to fill

value*

The value to fill the array with

startnumber<optional>
0

The start position

endnumber<optional>
arr.length

The end position

Since:
  • v3.5.0
Returns:
Type
Array
Example
fill(Array(5), 'a') // => ['a', 'a', 'a', 'a', 'a']

# flatten(array)

Flattens array

Parameters:
NameTypeDescription
arrayarray

The array flatten

Since:
  • v0.4.5
Returns:

array

Example
flatten([[[1, [1.1]], 2, 3], [4, 5]]); //=> [1, 1.1, 2, 3, 4, 5]

# formatNumber(number, thousandsSeparatoropt) → {String}

Formats a number (aka insert thousands separator)

Parameters:
NameTypeAttributesDefaultDescription
numberNumber

The number to format

thousandsSeparatorString<optional>
,

The thousands separator

Since:
  • v3.6.0
Returns:
Type
String
Example
formatNumber(1000) // => '1,000'

# getBrowser()

Find the browser information (includes code from this SO post).

Since:
  • v2.12.0
Returns:

String

Example
getBrowser();

# (async) getOS()

Find the OS information (includes code from this SO post). (PS This is nowhere as polished as libraries dedicated to only detecting the OS details)

Since:
  • v2.11.0
Returns:

String

Example
await getOS();

# hexToRgb(hex)

Converts an Hexadecimal color code to RGB

Parameters:
NameTypeDescription
hexString

The hexadecimal color code

Since:
  • v2.9.0
Returns:

string

Example
Tess.hexToRgb(''#ff64c8') ///=> 255, 100, 200

# includes(value, array)

Returns true if the given n-dimensional array contains a given value

Parameters:
NameTypeDescription
value*

The value to search for in the array

arrayarray

The array to search for the given value

Since:
  • v2.10.0
Returns:

Boolean

Example
includes(2, [[[1,3,5],2]])

# isNil(value)

Returns true if the value is null or undefined

Parameters:
NameTypeDescription
value*

The value to check

Since:
  • v0.0.1
Returns:

boolean

Example
isNill(null)       //=> true
isNill(undefined)  //=> true
isNill('')         //=> false
isNill([])         //=> false
isNill({})         //=> false

# isString(value)

Checks if the given value is a string.

Parameters:
NameTypeDescription
value*

The value to check.

Since:
  • v2.5.2-beta.1
Returns:

string

Example
isString('value') // true

# kebabCase(string)

Turns a string into kebab case

Parameters:
NameTypeDescription
stringstring

The string to turn into kebab case

Since:
  • v3.4.0
Returns:

string

Example
kebabCase('hello there') //=> hello-there

# m(metres, toopt)

Converts metres to a different unit or vice-versa

Parameters:
NameTypeAttributesDefaultDescription
metresNumber

The amount to convert

toString<optional>
km

What the metres should be converted to Accepted units:
km, cm, mm, um, nm, ft, yard, miles, inch, m

Since:
  • v2.5.2-beta.3
Returns:

string

Example
m('1 km', 'm') // 1000

# mean(numbers) → {number}

Gets the mean for an array of numbers

Parameters:
NameTypeDescription
numbersArray.<number>

The array to get the mean from

Since:
  • v3.7.0
Returns:
Type
number
Example
mean([1, 6, 8, 16, 28]) // => 11.8

# median(numbers)

Gets the median for an array of numbers

Parameters:
NameTypeDescription
numbersArray.<Number>

The array to get the median from

Since:
  • v3.7.0
Returns:

number

Example
median([8, 6, 1, 16, 28]) // => 8

# mode(numbers)

Gets the mode for an array of numbers

Parameters:
NameTypeDescription
numbersArray.<number>

The array to get the mean from

Since:
  • v3.7.0
Returns:

number

Example
mode([1, 6, 8, 16, 28, 8]) // => 8

# numeronym(string)

Turns a word into a numeronym

Parameters:
NameTypeDescription
stringstring

The string to turn into a numeronym

Since:
  • v3.2.0
Returns:

string

Example
numeronym('internationalization') // => i18n

# pascalCase(string)

Turns a string into pascal case

Parameters:
NameTypeDescription
stringstring

The string to turn into pascal case

Since:
  • v1.1.3
Returns:

string

Example
pascalCase('hello there') //=> 'HelloThere'

# pickRan(array) → {*}

Picks a random item from an array

Parameters:
NameTypeDescription
arrayarray

The array to pick the item from

Since:
  • v0.4.1
Returns:
Type
*
Example
let array = ['1', '2', '3', '4', '5']
pickRan(array)

# (async) progressBar(typeopt, numberopt, numberopt)

Creates a console progress bar (Node.js only)

Parameters:
NameTypeAttributesDefaultDescription
typenumber<optional>
1

The type of the progress bar

numbernumber<optional>
80

The time in ms for the progress bar to finish

numbernumber<optional>
20

The number length of the progress bar

Since:
  • v2.1.1
Returns:

Promise

Example
progressBar(1, 80)
//=> [-----------=========] 55%
progressBar(2)
//=>  [....................] 100%

# ranBool()

Selects a random boolean (true/false)

Since:
  • v0.2.0
Returns:

Boolean

Example
ranBool()

# ranFloat(min, max)

Selects a random float point

Parameters:
NameTypeDescription
minnumber

The smallest number of the range to pick the random float from

maxnumber

The largest number of the range to pick the random float from

Since:
  • v0.2.0
Returns:

number

Example
ranFloat(3,10)

# ranInt(min, max)

Selects a random number

Parameters:
NameTypeDescription
minnumber

The smallest number of the range to pick the random number from

maxnumber

The largest number of the range to pick the random number from

Since:
  • v0.2.0
Returns:

number

Example
ranInt(3,10)

# ranKey(length)

Generates a random key

Parameters:
NameTypeDescription
lengthnumber

The length of the key

Since:
  • v2.2.0
Returns:

string

Example
ranKey(10)

# reverse(string)

Reverse a string

Parameters:
NameTypeDescription
stringstring

The string to reverse

Since:
  • v0.4.9
Returns:

string

Example
reverse('Hello') //=> 'olleH'

# rgbToHex(red, green, blue)

Converts an RGB color code to a hexadecimal string

Parameters:
NameTypeDescription
rednumber

The amount of red

greennumber

The amount of green

bluenumber

The amount of blue

Since:
  • v2.9.0
Returns:

string

Example
rgbToHex(255, 100, 200) ///=> #ff64c8

# rmFalsey(array)

Removes falsy values from an array

Parameters:
NameTypeDescription
arrayarray

The array to remove falsey values from

Since:
  • v0.4.4
Returns:

array

Example
let array = [undefined, null, NaN, 1, 2, 3, 'hello']
rmFalsey(array)    //=> [1, 2, 3, 'hello']

# roman(val)

Converts a number to/from Roman Numerals. If the argument received is a string, it will be converted to a number. If the argument received is a number, it will be converted to a roman numeral.

Parameters:
NameTypeDescription
valNumber

The value to convert from/to Roman Numerals

Since:
  • v2.5.2-beta.7
Returns:

number/string

Example
roman('IV')
roman(4)

# shuffle(array)

Shuffles an array

Parameters:
NameTypeDescription
arrayarray

The array to shuffle

Since:
  • v0.4.8
Returns:

Array

Example
shuffle([1,2,3,4,5,6]) //=> [2,4,3,1,6,5]

# snakeCase(string)

Turns a string into snake case

Parameters:
NameTypeDescription
stringstring

The string to turn into snake case

Since:
  • v3.4.0
Returns:

string

Example
snakeCase('hello there') // => hello_there

# sparseEach(array, function)

async-friendly for-each functions for sparse arrays from this post

This function will make a fine addition to my collection

Parameters:
NameTypeDescription
arrayarray

The array to loop through (The array to sparse?)

functionfunction

A function to be run for each element in the array.

Properties
NameTypeAttributesDescription
currentValuefunction<optional>

The value of the current element

indexfunction<optional>

The array index of the current element

arrfunction<optional>

The array object the current element belongs to

Since:
  • v1.0.1
Returns:

Array

Example
const a = [];
a[5] = "five";
a[10] = "ten";
a[100000] = "one hundred thousand";
a.b = "bee";

sparseEach(a, function(currentValue, index, arr) {
    console.log("Value at " + index + " is " + value);
		// arr is the array object
});

# squash(…arrays) → {Array}

Squashes all the given arrays. Note: values like undefined × 1 or undefined × 40 are left out and not squashed.

Parameters:
NameTypeAttributesDescription
arraysArray<repeatable>

The arrays to squash

Since:
  • v3.1.0
Returns:
Type
Array
Example
squash([1, 2], [3, 4]); // => [1, 2, 3, 4]

# stdDev(array)

Gets standard deviation from an array

Parameters:
NameTypeDescription
arrayArray.<number>

The array to get the standard deviation from

Since:
  • v2.6.0
Returns:

number

Example
stdDev([1, 6, 8, 16, 28])

# strToNumArray(string, separatoropt) → {Array}

Turns a string into an array of numbers

Parameters:
NameTypeAttributesDefaultDescription
stringstring

The string to turn into an array of numbers

separatorstring<optional>
<space>

The separator that is used to separate numbers

Since:
  • v3.3.0
Returns:
Type
Array
Example
strToNumArray('1 2 3 4') // => [1, 2, 3, 4]

# temp(temperature)

Converts a temperature into different units

Parameters:
NameTypeDescription
temperaturestring

The temperature to convert. You should enter in this format: 1 C, 100 F etc.
Supported: K (Kelvin), F (Farenheit), C (Celsius)

Since:
  • v2.5.2-beta.9
Returns:

object with keys and values of different temperatures. The possible propnames are: K (Kelvin), F (Farenheit), C (Celsius)

Example
Tess.temp('20 C')['F'];
Tess.temp('20 F')['C'];
Tess.temp('100 K')['F'];

# terminalVelocity(density, projectedArea, dragCoefficient, gravityopt) → {function}

Sets the options for the function to calculate the terminal velocity of an object (this is probably gonna be useless)

Parameters:
NameTypeAttributesDefaultDescription
densitynumber

The density of the fluid the object is falling through

projectedAreanumber

the projected area of the object. This means the area of the object if you projected it onto a plane that was perpendicular to the direction the object is moving

dragCoefficientnumber

the drag coefficient. This number depends on the shape of the object. The more streamlined the shape, the lower the coefficient.

gravitynumber<optional>
9.8

the acceleration due to gravity. On Earth this is approximately 9.8 meters per second

Since:
  • v3.9.0
Returns:

a function that accepts a single parameter (the mass of the falling object)

Type
function

# time(time, toopt)

Converts a given time. If the given time is a number, it will be interpreted as minutes. Accepted units:
m, s, h, ms, d, y, us (microsecond), w, mth (month)

Parameters:
NameTypeAttributesDefaultDescription
timenumber | string

The amount to convert

tostring<optional>
ms

What the time should be converted to

Since:
  • v2.5.8
Returns:

string

Example
time('2 y', 'mth') // 24

# toJSON(Map)

Converts a map to JavaScript objects. To convert to JSON, just use JSON.stringify(). To get the object back from the string, use JSON.parse()

Parameters:
NameTypeDescription
MapMap

The map to convert to JavaScript objects

Since:
  • v2.5.2-beta.6
Returns:

Object

Example
const map = new Map([
 ['foo', 'bar'],
 ['baz', 42]
]);
const obj = toJSON(map); // get the JS object
const JSONObjectLiteral = JSON.stringify(obj); // get the JSON object literal
const JSObj = JSON.parse(JSONObjectLiteral); // get the JavaScript object back

# toMorse(string)

Converts a string to morse code

Parameters:
NameTypeDescription
stringstring

The string to convert

Since:
  • v2.5.2-beta.8
Returns:

string

Example
toMorse('Hello there')

# typeOf(value) → {string}

An improved typeof operator

Parameters:
NameTypeDescription
value*

The value to find the type of

Since:
  • v2.5.2-beta.5
Returns:
Type
string
Example
typeOf(null); //=> null

# uniq(list, compareopt, sortedopt)

Removes duplicate values from an array

Parameters:
NameTypeAttributesDefaultDescription
listarray

The array to remove duplicate values from

comparefunction<optional>

Optional. A function that defines an alternative sort order. The function should return a negative, zero, or positive value, depending on the arguments, like function(a, b){return a-b}

sortedboolean<optional>
false

Optional. Whether the array is already sorted

Since:
  • v0.4.6
Returns:

array

Example
uniq([1, 1, 1.1, 2, 2, 3, 4, 5, 5, 5]) //=> [1, 1.1, 2, 3, 4, 5]

# wait(ms)

waits a specific period of time (in ms)

Parameters:
NameTypeDescription
msnumber

The amount of time to wait in milliseconds

Since:
  • v0.4.2
Returns:

promise

Example
(async () => {
		console.log('Hi');
		await wait(1000);
		console.log('Hi after 1 second');
}());

# weight(weight, toopt)

Converts a weight into different units. If the weight received is a number, it will be taken as grams.
Supported units: kg, lb, g, mg, tonne, ton (US Ton), st (stone), oz, mcg

Parameters:
NameTypeAttributesDefaultDescription
weightstring

The weight to convert

tostring<optional>
g

The unit to convert the weight to

Since:
  • v2.5.7
Returns:

number

Example
Tess.weight('10 kg', 'g')