xutilities

require.mx('mxjs/base/xutilities.js');


This utilities lib contains utilites for: dates, maths, arrays, asserts, conversions and misc.
These functions, where possible, should be used to avoid common coding errors.

AVALIABLE UTILITIES:
const array_uti = utilities.xarray;
const assert_uti = utilities.xassert;
const conv_uti = utilities.xconvert;
const math_uti = utilities.xmath;
const group_uti = utilities.xgroup;
const misc_uti = utilities.xmisc;

EXAMPLE IMPLEMENTATION:
const utilities = require.mx('mxjs/base/xutilities.js') //load utility lib
const assert_uti = utilities.xassert; //Optional parse of the individual utility libraries

utilities are accessed as (if not parsed):
utilities.xassert.isnum(1); returns true;
OR (if parsed)
date_uti.isnum('hello_world'); returns false;

StatusName
Array xarray.argsort ( Array ↓array, Function ↓comparefn )

Returns an array containing the indices of the input array, sorted by the values stored at those indices (using the given comparison function) . c.f. numpy's argsort https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.argsort.html

Number xarray.clone_obj ( Number Placeholder )

Number xarray.copy ( Number Placeholder )

Number xarray.equal_range ( Number Placeholder )

Number xarray.find_first_not_equal ( Number Placeholder )

Array xarray.get_unique_elements ( Array ↓array, Function equalfn, Function comparefn )

Returns an array containing the unique elements from the given input array. Uniqueness is tested using the !== operator by default.

xarray.insertElement ( Array array, Object itemToInsert, Number index, Number arraySize )

Inserts an element into an array at specified location

Number xarray.lower_bound ( Number Placeholder )

T xarray.maxElement ( Array ↓array )

Returns the value of the element in the array (if the array has at least one element) that has the maximum value (using the > operator).

Int32 xarray.maxElementIndex ( Array ↓array )

Returns the index of the element in the array (if the array has at least one element) that has the maximum value (using the > operator).

xarray.moveElement ( Array array, Number fromIndex, Number toIndex )

moves an element within an array

Number xarray.print_obj ( Number Placeholder )

Array xarray.sort_array ( Array ⇅array )

Sorts an array based on the contained values OR strings
WARNING: Inherents some limitations of the .sort() array method
e.g. A = [99,-99,0];
A.sort(sort_array);
A = [-99,0,99];
OR
A = ['z','a','Z', 'A'];
A.sort(sort_array);
A = [A,Z,a,z];
Note: Do not pass in arrays of mixed values and strings for sorting using this method.

Array xarray.sort_nested_array ( Array ⇅array, Number ↓index )

Sorts a nested array based on an array index
WARNING: Inherents some limitations of the .sort() array method
const sorting_function = sort_nested_array(1);
e.g. A = [[-1,9,X],[1,1,z],[0,4,q]];
B = sort_nested_array(A,1);
A = [[1,1,z],[0,4,q],[-1,9,X]];
Note: Do not pass in arrays of mixed values and strings for the same index for sorting using this method.

Number xarray.sort_three_numbered_array ( Number Placeholder )

Number xarray.starts_with ( Number Placeholder )

Number xarray.starts_with_N ( Number Placeholder )

Array xarray.transform_array ( Array ⇅array, Number ↓arraySize, Function ↓Function )

Executes the provided function over each element of the provided array and replaces the element with the result of the function execution.

Array xarray.transform_nested_array ( Array ⇅array, Number ↓nestedIndex, Number ↓arraySize, Function ↓Function )

Executes the provided function over each element, at the specified index, of the provided nested array and replaces the element with the result of the function execution.

Number xarray.update ( Number Placeholder )

Number xarray.upper_bound ( Number Placeholder )

Number xassert.assert ( Number Placeholder )

Number xassert.assert_arguments_valid_num ( Number Placeholder )

Number xassert.assert_valid_num ( Number Placeholder )

Number xassert.invalid_to_null ( Number Placeholder )

Number xassert.isnum ( Number Placeholder )

Number xdate.calc_iso_week ( Number Placeholder )

Number xdate.calc_iso_week_ymd ( Number Placeholder )

Number xdate.calc_time_period_ms ( Number Placeholder )

Number xdate.calc_week_startdate_ms ( Number Placeholder )

Number xdate.get_hours ( Number Placeholder )

Number xdate.get_minutes ( Number Placeholder )

xdate.pad_2 ( Point {Number} )

txt

Number xdate.pad_3 ( Number Placeholder )

Number xdate.remove_time ( Number Placeholder )

Number xdate.str_utc ( Number Placeholder )

Number xdate.str_utc_ms ( Number Placeholder )

Number xgroup.   extract_groups ( Number Placeholder )

Number xgroup.discover_no_overlap ( Number Placeholder )

Number xgroup.no_overlap ( Number Placeholder )

Number xgroup.print_group_info ( Number Placeholder )

xmath.cbrt ( Number ↓x )

= 3√x

Number xmath.clamp ( Number Placeholder )

xmath.cube(x) ( Number ↓x )

= x 3

xmath.dist ( [x1,y1,z1],[x2,y2,z2] ↓x )

= distance between point a and b

xmath.exp ( Number ↓x )

= e x where e is the "Euler number", 2.71828...

xmath.exp10 ( Number ↓x )

= 10 x

xmath.ln ( Number ↓x )

= ln(x)

xmath.log10 ( Number ↓x )

= log 10

Number xmath.log2 ( Number ↓x )

`log_2`

Number xmath.numdigit ( Number ↓x, Number ↓numdigit )

rounds a number 'x' to number of digits. e.g. rounding 123456 to 4 digits will return 123500.

xmath.pow10 ( Number ↓x )

= exp10(x) = 10 x

Number xmath.rd ( Number ↓x, Number ↓dp )

rounds a number 'x' to 'dp' number of places

xmath.sqr ( Number ↓x )

= x 2

xmath.sqr_dist ( ↓{[x1,y1,z1],[x2,y2,z2]} )

= squared distance between point a and b

xmath.sqrt ( Number ↓x )

= √x

Number xmisc.compare ( Number Placeholder )

Number xmisc.pointstats ( Number Placeholder )


Category: xarray

indices = Lib.xarray.argsort ( ↓array, ↓comparefn )

Returns an array containing the indices of the input array, sorted by the values stored at those indices (using the given comparison function) . c.f. numpy's argsort https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.argsort.html

Parameters:
  • Array ↓array - the input array
  • Function ↓comparefn - the comparison function
Returns: Array indices - the indices of the sorted array
Placeholder = Lib.xarray.clone_obj ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xarray.copy ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xarray.equal_range ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xarray.find_first_not_equal ( Placeholder )

Parameters: Returns: Number Placeholder
uniques = Lib.xarray.get_unique_elements ( ↓array, equalfn, comparefn )

Returns an array containing the unique elements from the given input array. Uniqueness is tested using the !== operator by default.

Parameters:
  • Array ↓array - the input array
  • Function equalfn - function (a, b) -> bool (true if a and b are the same)
  • Function comparefn - comparison function to use with Array.sort (internally used to simplify the uniqueness checking: non-unique items should be next to each other in sorted order)
Returns: Array uniques - array containing the unique elements
Lib.xarray.insertElement ( array, itemToInsert, index, arraySize )

Inserts an element into an array at specified location

Parameters:
  • Array array - Array to insert into
  • Object itemToInsert - element to insert
  • Number index - index to insert element into
  • Number arraySize - size of the array
Placeholder = Lib.xarray.lower_bound ( Placeholder )

Parameters: Returns: Number Placeholder
value = Lib.xarray.maxElement ( ↓array )

Returns the value of the element in the array (if the array has at least one element) that has the maximum value (using the > operator).

Parameters:
  • Array ↓array - the input array
Returns: T value - the maximum value of all elements in the array
index = Lib.xarray.maxElementIndex ( ↓array )

Returns the index of the element in the array (if the array has at least one element) that has the maximum value (using the > operator).

Parameters:
  • Array ↓array - the input array
Returns: Int32 index - the index of the maximum value of all elements in the array
Lib.xarray.moveElement ( array, fromIndex, toIndex )

moves an element within an array

Parameters:
  • Array array - Array containing element to move
  • Number fromIndex - index to move element from
  • Number toIndex - index to move element to
Placeholder = Lib.xarray.print_obj ( Placeholder )

Parameters: Returns: Number Placeholder
⇅array = Lib.xarray.sort_array ( ⇅array )

Sorts an array based on the contained values OR strings
WARNING: Inherents some limitations of the .sort() array method
e.g. A = [99,-99,0];
A.sort(sort_array);
A = [-99,0,99];
OR
A = ['z','a','Z', 'A'];
A.sort(sort_array);
A = [A,Z,a,z];
Note: Do not pass in arrays of mixed values and strings for sorting using this method.

Parameters:
  • Array ⇅array - An array for sorting
Returns: Array ⇅array - the array that was passed in for sorting.
⇅array = Lib.xarray.sort_nested_array ( ⇅array, ↓index )

Sorts a nested array based on an array index
WARNING: Inherents some limitations of the .sort() array method
const sorting_function = sort_nested_array(1);
e.g. A = [[-1,9,X],[1,1,z],[0,4,q]];
B = sort_nested_array(A,1);
A = [[1,1,z],[0,4,q],[-1,9,X]];
Note: Do not pass in arrays of mixed values and strings for the same index for sorting using this method.

Parameters:
  • Array ⇅array - An array for sorting
  • Number ↓index - array index to use when sorting
Returns: Array ⇅array - the array that was passed in for sorting.
Placeholder = Lib.xarray.sort_three_numbered_array ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xarray.starts_with ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xarray.starts_with_N ( Placeholder )

Parameters: Returns: Number Placeholder
⇅array = Lib.xarray.transform_array ( ⇅array, ↓arraySize, ↓Function )

Executes the provided function over each element of the provided array and replaces the element with the result of the function execution.

Parameters:
  • Array ⇅array - array to be transformed
  • Number ↓arraySize - optional - size of array (if reusing array). If not specified, array.length will be used.
  • Function ↓Function - A function that takes a single parameter (each element of the array) and returns a value.
Returns: Array ⇅array - array that was passed in to be transformed
⇅array = Lib.xarray.transform_nested_array ( ⇅array, ↓nestedIndex, ↓arraySize, ↓Function )

Executes the provided function over each element, at the specified index, of the provided nested array and replaces the element with the result of the function execution.

Parameters:
  • Array ⇅array - array of arrays to be transformed
  • Number ↓nestedIndex - the index of the element within each nested array to be transformed
  • Number ↓arraySize - optional - size of array of arrays. If not specified, array.length will be used.
  • Function ↓Function - A function that takes a single parameter (each element) and returns a value.
Returns: Array ⇅array - array that was passed in to be transformed
Placeholder = Lib.xarray.update ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xarray.upper_bound ( Placeholder )

Parameters: Returns: Number Placeholder

Category: xassert

Placeholder = Lib.xassert.assert ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xassert.assert_arguments_valid_num ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xassert.assert_valid_num ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xassert.invalid_to_null ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xassert.isnum ( Placeholder )

Parameters: Returns: Number Placeholder

Category: xdate

Placeholder = Lib.xdate.calc_iso_week ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xdate.calc_iso_week_ymd ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xdate.calc_time_period_ms ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xdate.calc_week_startdate_ms ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xdate.get_hours ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xdate.get_minutes ( Placeholder )

Parameters: Returns: Number Placeholder
↑ = Lib.xdate.pad_2 ( {Number} )

txt

Parameters:
  • Point {Number} - ↓ parameter in
Returns: - return
Placeholder = Lib.xdate.pad_3 ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xdate.remove_time ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xdate.str_utc ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xdate.str_utc_ms ( Placeholder )

Parameters: Returns: Number Placeholder

Category: xgroup

This library provides some groupby functions
QUICK START:
//note: This assumes you have grouped by the group in the table calc input.

//Load script and find groups
const xgroup_utility = require.mx('mxjs/base/utilities/group.js');
const group_object = {};
xgroup_utility.extract_groups(table_name, 'group_read_function', group_object)
xgroup_utility.print_group_info(group_object); //this logs all group results
//common group usage
for (let group in group_object) { //For all group results groups in group_object
   const start_i = group_object[group].StartIDX;
   const end_i = group_object[group].EndIDX;
   const n = group_object[group].n;
   const name = group_object[group].name;
   for(let i = start_i; i <= end_i; ++i){ //do some stuff for the indices
//add function in here!
}
}

Placeholder = Lib.xgroup.&nbsp;&nbsp; extract_groups ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xgroup.discover_no_overlap ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xgroup.no_overlap ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xgroup.print_group_info ( Placeholder )

Parameters: Returns: Number Placeholder

Category: xmath

Lib.xmath.cbrt ( ↓x )

= 3√x

Parameters:
Placeholder = Lib.xmath.clamp ( Placeholder )

Parameters: Returns: Number Placeholder
Lib.xmath.cube(x) ( ↓x )

= x 3

Parameters:
Lib.xmath.dist ( ↓x )

= distance between point a and b

Parameters:
  • [x1,y1,z1],[x2,y2,z2] ↓x
Lib.xmath.exp ( ↓x )

= e x where e is the "Euler number", 2.71828...

Parameters:
Lib.xmath.exp10 ( ↓x )

= 10 x

Parameters:
Lib.xmath.ln ( ↓x )

= ln(x)

Parameters:
Lib.xmath.log10 ( ↓x )

= log 10

Parameters:
result = Lib.xmath.log2 ( ↓x )

`log_2`

Parameters: Returns: Number result
↑rnd_num = Lib.xmath.numdigit ( ↓x, ↓numdigit )

rounds a number 'x' to number of digits. e.g. rounding 123456 to 4 digits will return 123500.

Parameters:
  • Number ↓x - number to be rounded
  • Number ↓numdigit - number of digits to be rounded to
Returns: Number ↑rnd_num
Lib.xmath.pow10 ( ↓x )

= exp10(x) = 10 x

Parameters:
↑rnd_num = Lib.xmath.rd ( ↓x, ↓dp )

rounds a number 'x' to 'dp' number of places

Parameters:
  • Number ↓x - number to be rounded
  • Number ↓dp - number of decimal places to be rounded to
Returns: Number ↑rnd_num
Lib.xmath.sqr ( ↓x )

= x 2

Parameters:
Lib.xmath.sqr_dist ( ↓{[x1,y1,z1],[x2,y2,z2]} )

= squared distance between point a and b

Parameters:
  • ↓{[x1,y1,z1],[x2,y2,z2]}
Lib.xmath.sqrt ( ↓x )

= √x

Parameters:

Category: xmisc

Placeholder = Lib.xmisc.compare ( Placeholder )

Parameters: Returns: Number Placeholder
Placeholder = Lib.xmisc.pointstats ( Placeholder )

Parameters: Returns: Number Placeholder