Namespace: Utils

Utils

Utility functions for use in operations, the core framework and the stage.

Author:
  • <p>n1474335 [n1474335@gmail.com]</p>
License:
  • Apache-2.0
Source:

Members


<private, static, constant> UNIC_WIN1251_MAP

Mapping of Unicode code points to Windows-1251

Source:

<private, static, constant> WIN1251_UNIC_MAP

Mapping of Windows-1251 code points to Unicode

Source:

<static, constant> char_rep

A mapping of names of delimiter characters to their symbols.

Source:

<static, constant> regex_rep

A mapping of names of delimiter characters to regular expressions which can select them.

Source:

<static, constant> format

A mapping of string formats to their classes in the CryptoJS library.

Source:

Methods


<static> chr(o)

Translates an ordinal into a character.

Parameters:
Name Type Description
o number
Source:
Returns:
Type
char
Example
// returns 'a'
Utils.chr(97);

<static> ord(c)

Translates a character into an ordinal.

Parameters:
Name Type Description
c char
Source:
Returns:
Type
number
Example
// returns 97
Utils.ord('a');

<static> pad_left(str, max [, chr])

Adds leading zeros to strings

Parameters:
Name Type Argument Default Description
str string

String to add leading characters to.

max number

Maximum width of the string.

chr char <optional>
'0'

The character to pad with.

Source:
Returns:
Type
string
Example
// returns "0a"
Utils.pad_left("a", 2);

// returns "000a"
Utils.pad_left("a", 4);

// returns "xxxa"
Utils.pad_left("a", 4, "x");

// returns "bcabchello"
Utils.pad_left("hello", 10, "abc");

<static> pad_right(str, max [, chr])

Adds trailing spaces to strings.

Parameters:
Name Type Argument Default Description
str string

String to add trailing characters to.

max number

Maximum width of the string.

chr char <optional>
'0'

The character to pad with.

Source:
Returns:
Type
string
Example
// returns "a   "
Utils.pad_right("a", 4);

// returns "axxx"
Utils.pad_right("a", 4, "x");

<static> pad_left()

Source:

<static> truncate(str, max [, suffix])

Truncates a long string to max length and adds suffix.

Parameters:
Name Type Argument Default Description
str string

String to truncate

max number

Maximum length of the final string

suffix string <optional>
'...'

The string to add to the end of the final string

Source:
Returns:
Type
string
Example
// returns "A long..."
Utils.truncate("A long string", 9);

// returns "A long s-"
Utils.truncate("A long string", 9, "-");

<static> hex(c [, length])

Converts a character or number to its hex representation.

Parameters:
Name Type Argument Default Description
c char | number
length number <optional>
2

The width of the resulting hex number.

Source:
Returns:
Type
string
Example
// returns "6e"
Utils.hex("n");

// returns "6e"
Utils.hex(110);

<static> bin(c [, length])

Converts a character or number to its binary representation.

Parameters:
Name Type Argument Default Description
c char | number
length number <optional>
8

The width of the resulting binary number.

Source:
Returns:
Type
string
Example
// returns "01101110"
Utils.bin("n");

// returns "01101110"
Utils.bin(110);

<static> printable(str [, preserve_ws])

Returns a string with all non-printable chars as dots, optionally preserving whitespace.

Parameters:
Name Type Argument Default Description
str string

The input string to display.

preserve_ws boolean <optional>
false

Whether or not to print whitespace.

Source:
Returns:
Type
string

<static> parse_escaped_chars(str)

Parse a string entered by a user and replace escaped chars with the bytes they represent.

Parameters:
Name Type Description
str string
Source:
Returns:
Type
string
Example
// returns "\x00"
Utils.parse_escaped_chars("\\x00");

// returns "\n"
Utils.parse_escaped_chars("\\n");

<static> expand_alph_range(alph_str)

Expand an alphabet range string into a list of the characters in that range.

Parameters:
Name Type Description
alph_str string
Source:
Returns:
Type
Array.<char>
Example
// returns ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
Utils.expand_alph_range("0-9");

// returns ["a", "b", "c", "d", "0", "1", "2", "3", "+", "/"]
Utils.expand_alph_range("a-d0-3+/");

// returns ["a", "b", "c", "d", "0", "-", "3"]
Utils.expand_alph_range("a-d0\\-3")

<static> hex_to_byte_array(hex_str)

Translates a hex string into an array of bytes.

Parameters:
Name Type Description
hex_str string
Source:
Returns:
Type
byte_array
Example
// returns [0xfe, 0x09, 0xa7]
Utils.hex_to_byte_array("fe09a7");

<static> byte_array_to_hex(byte_array)

Translates an array of bytes to a hex string.

Parameters:
Name Type Description
byte_array byte_array
Source:
Returns:
Type
string
Example
// returns "fe09a7"
Utils.byte_array_to_hex([0xfe, 0x09, 0xa7]);

<static> str_to_byte_array(str)

Converts a string to a byte array.
Treats the string as UTF-8 if any values are over 255.

Parameters:
Name Type Description
str string
Source:
Returns:
Type
byte_array
Example
// returns [72,101,108,108,111]
Utils.str_to_byte_array("Hello");

// returns [228,189,160,229,165,189]
Utils.str_to_byte_array("你好");

<static> str_to_utf8_byte_array(str)

Converts a string to a UTF-8 byte array.

Parameters:
Name Type Description
str string
Source:
Returns:
Type
byte_array
Example
// returns [72,101,108,108,111]
Utils.str_to_utf8_byte_array("Hello");

// returns [228,189,160,229,165,189]
Utils.str_to_utf8_byte_array("你好");

<static> str_to_charcode(str)

Converts a string to a charcode array

Parameters:
Name Type Description
str string
Source:
Returns:
Type
byte_array
Example
// returns [72,101,108,108,111]
Utils.str_to_charcode("Hello");

// returns [20320,22909]
Utils.str_to_charcode("你好");

<static> byte_array_to_utf8(byte_array)

Attempts to convert a byte array to a UTF-8 string.

Parameters:
Name Type Description
byte_array byte_array
Source:
Returns:
Type
string
Example
// returns "Hello"
Utils.byte_array_to_utf8([72,101,108,108,111]);

// returns "你好"
Utils.byte_array_to_utf8([228,189,160,229,165,189]);

<static> byte_array_to_chars(byte_array)

Converts a charcode array to a string.

Parameters:
Name Type Description
byte_array byte_array
Source:
Returns:
Type
string
Example
// returns "Hello"
Utils.byte_array_to_chars([72,101,108,108,111]);

// returns "你好"
Utils.byte_array_to_chars([20320,22909]);

<static> word_array_to_byte_array(word_array)

Converts a CryptoJS.lib.WordArray to a byte_array.

Parameters:
Name Type Description
word_array CryptoJS.lib.WordArray
Source:
Returns:
Type
byte_array
Example
// returns [84, 101, 115, 116]
Utils.word_array_to_byte_array(CryptoJS.enc.Hex.parse("54657374"));

<static> unicode_to_win1251(unic_str)

Converts a Unicode string to Windows-1251 encoding

Parameters:
Name Type Description
unic_str string
Source:
Returns:

String encoded in Windows-1251

Type
string
Example
// returns "îáíîâëåííàÿ òåõíè÷êà ïî Áîèíãó. îðèãèíàë ó ìåíÿ. çàáåðåòå êîãäà áóäåòå â ÊÈ"
Utils.unicode_to_win1251("обновленная техничка по Боингу. оригинал у меня. заберете когда будете в КИ");

<static> win1251_to_unicode(win1251_str)

Converts a Windows-1251 string to Unicode encoding

Parameters:
Name Type Description
win1251_str string
Source:
Returns:

String encoded in Unicode

Type
string
Example
// returns "обновленная техничка по Боингу. оригинал у меня. заберете когда будете в КИ"
Utils.unicode_to_win1251("îáíîâëåííàÿ òåõíè÷êà ïî Áîèíãó. îðèãèíàë ó ìåíÿ. çàáåðåòå êîãäà áóäåòå â ÊÈ");

<static> to_base64(data [, alphabet])

Base64's the input byte array using the given alphabet, returning a string.

Parameters:
Name Type Argument Description
data byte_array | string
alphabet string <optional>
Source:
Returns:
Type
string
Example
// returns "SGVsbG8="
Utils.to_base64([72, 101, 108, 108, 111]);

// returns "SGVsbG8="
Utils.to_base64("Hello");

<static> from_base64(data [, alphabet] [, return_type] [, remove_non_alph_chars])

UnBase64's the input string using the given alphabet, returning a byte array.

Parameters:
Name Type Argument Default Description
data byte_array
alphabet string <optional>
return_type string <optional>
"string"

Either "string" or "byte_array"

remove_non_alph_chars boolean <optional>
true
Source:
Returns:
Type
byte_array
Example
// returns "Hello"
Utils.from_base64("SGVsbG8=");

// returns [72, 101, 108, 108, 111]
Utils.from_base64("SGVsbG8=", null, "byte_array");

<static> to_hex(data [, delim] [, padding])

Convert a byte array into a hex string.

Parameters:
Name Type Argument Default Description
data byte_array
delim string <optional>
" "
padding number <optional>
2
Source:
Returns:
Type
string
Example
// returns "0a 14 1e"
Utils.to_hex([10,20,30]);

// returns "0a:14:1e"
Utils.to_hex([10,20,30], ":");

<static> to_hex_fast(data)

Convert a byte array into a hex string as efficiently as possible with no options.

Parameters:
Name Type Description
data byte_array
Source:
Returns:
Type
string
Example
// returns "0a141e"
Utils.to_hex([10,20,30]);

<static> from_hex(data [, delim] [, byte_len])

Convert a hex string into a byte array.

Parameters:
Name Type Argument Default Description
data string
delim string <optional>
byte_len number <optional>
2
Source:
Returns:
Type
byte_array
Example
// returns [10,20,30]
Utils.from_hex("0a 14 1e");

// returns [10,20,30]
Utils.from_hex("0a:14:1e", "Colon");

<static> parse_csv(data)

Parses CSV data and returns it as a two dimensional array or strings.

Parameters:
Name Type Description
data string
Source:
Returns:
Type
Array.<Array.<string>>
Example
// returns [["head1", "head2"], ["data1", "data2"]]
Utils.parse_csv("head1,head2\ndata1,data2");

<static> strip_html_tags(html_str, remove_script_and_style)

Removes all HTML (or XML) tags from the input string.

Parameters:
Name Type Description
html_str string
remove_script_and_style boolean

Flag to specify whether to remove entire script or style blocks

Source:
Returns:
Type
string
Example
// returns "Test"
Utils.strip_html_tags("<div>Test</div>");

<static> escape_html(str)

Escapes HTML tags in a string to stop them being rendered

Parameters:
Name Type Description
str string
Source:
Returns:

string

Example
// return "A &lt;script> tag"
Utils.escape_html("A <script> tag");

<static> fuzzy_time(ms)

Expresses a number of milliseconds in a human readable format.

Range Sample Output
0 to 45 seconds a few seconds ago
45 to 90 seconds a minute ago
90 seconds to 45 minutes 2 minutes ago ... 45 minutes ago
45 to 90 minutes an hour ago
90 minutes to 22 hours 2 hours ago ... 22 hours ago
22 to 36 hours a day ago
36 hours to 25 days 2 days ago ... 25 days ago
25 to 45 days a month ago
45 to 345 days 2 months ago ... 11 months ago
345 to 545 days (1.5 years) a year ago
546 days+ 2 years ago ... 20 years ago
Parameters:
Name Type Description
ms number
Source:
Returns:
Type
string
Example
// returns "3 minutes"
Utils.fuzzy_time(152435);

// returns "5 days"
Utils.fuzzy_time(456851321);

<static> extend(a, b)

Adds the properties of one Object to another.

Parameters:
Name Type Description
a Object
b Object
Source:
Returns:
Type
Object