Reference

By default Previous.js comes with some common utility functions available in client and server side.

String Methods

replaceAll

Syntax

String.prototype.replaceAll(search, replace)

Description

Replace all ocurrencies of the search string with the replace string without using regular expressions.

var str = "The quick red fox jumps over the lazy dog.";
console.log(str.replaceAll('o', '0'));
// Result:
// The quick red f0x jumps 0ver the lazy d0g.

trim

Syntax

String.prototype.trim(char)

Description

Remove char from the beginning and the end of the string.

var str = "///test/path//";
console.log(str.trim('/'));
// Result:
// test/path

NOTE: If no param is provided, trim works with the original native implementation removing blanks.

encodeEntities

Syntax

String.prototype.encodeEntities()

Description

Converts all characters from a string to encoded entities.

var str = "Hello World!";
console.log(str.encodeEntities());
// Result:
// Hello World!

decodeEntities

Syntax

String.prototype.decodeEntities()

Description

Decode all encoded entities to its respective character in a string.

var str = "Hello World!";
console.log(str.decodeEntities());
// Result:
// Hello World!

Array Methods

unique

Syntax

Array.prototype.unique()

Description

Remove all duplicated elements in an array.

var arr = [null, null, 0, 0, 1, 2, 4, 0, 3, 'test', 'test', 3, 'hello'];
console.log(arr.unique());
// Result:
// Array(8) [ null, 0, 1, 2, 4, 3, "test", "hello" ]

Date Methods

getFullDateString

Syntax

Date.prototype.getFullDateString()

Description

Returns a string with the date in "yyyy-mm-dd hh:ii:ss" format.

var now = new Date();
console.log(now.getFullDateString());
// Result:
// 2023-09-17 08:17:27