Type-safe JS Utilities Library, v.1.7.11
The xuu.js
(pronounced "zoo") utility library is a stand-alone port of
the hi_score utility library found in hi_score/js/xhi/01_util.js
.
The test suite, nodeunit_xuu.js
is extracted from
hi_score/test.d/00_xhi_libs.js
.
The version of xuu.js
tracks that of hi_score
. In other words, version
1.7.11 matches the capabilties and testing of the 1.7.11 of hi_score.
- 1.7.11
-- Update dependencies to latest
-- Update minified version to 1.7.11
-- Makes timezone check more permissive to include a colon.
- 1.7.10 Adds support for
_return_map_
in method _makeTmpltStr_
.
Last updated 2024-08-01:
- Merge back doc fixups from below back to core
- Switch from
nodeunit
to tap
- Update JS to more current version, replacing var with let and const
- Remove or wrap recent JS advancements
- Backport changes to
hi_score
The following is mostly autogenerated from the Xuu function docs. Please merge
fixes that were applied back as described in TODO, above.
These variable type and cast methods are reuired for almost all utility
methods, and are therefore shown first in alphabetical order.
|
|
Summary |
getVarType( <data> ); |
Purpose |
Determine the type of data provided. |
Example |
getVarType( [] ); // '_Array_' |
Arguments |
<data> - value to examine |
Returns |
'_Function_', '_Object_', '_Array_', '_String_', |
|
'_Number_', '_Null_', '_Boolean_', or '_Undefined_' |
Throws |
None |
|
|
Summary |
castBool( <data>, <alt_data> ); |
Purpose |
Cast a boolean value. |
Example |
castBool( __true ); // returns __true |
Arguments |
(positional) |
|
<data> - data to cast as boolean |
|
<alt_data> - alt value to return |
Returns |
<data> if it is a boolean, <alt_data> otherwise |
Throws |
None |
|
|
Summary |
castFn( <data>, <alt_data> ); |
Purpose |
Cast a function. |
Example |
castFn( function() {} ); // returns function |
Arguments |
(positional) |
|
<data> - data to cast as function |
|
<alt_data> - alt value to return |
Returns |
<data> if it is a function, <alt_data> otherwise |
Throws |
None |
|
|
Summary |
castInt( <data>, <alt_data>, <option_map> ); |
Purpose |
Cast an integer. |
Example |
castInt( '25.425' ); // returns 25 |
Arguments |
(positional) |
|
<data> - data to cast as int |
|
<alt_data> - alt value to return |
|
<option_map> - Optional constraint map |
|
+ _do_autobound_ - Auto bound input to min/max |
|
+ _do_warn_ - Log warnings |
|
+ _max_num_ - Max allowed value |
|
+ _min_num_ - Min allowed value |
Returns |
|
|
If a number, returns the number rounded to nearest int. |
|
If a string, returns the number rep rounded to nearest int. |
|
Otherwise <alt_data>. |
Throws |
None |
|
|
Summary |
castJQ( <data>, <alt_data> ); |
Purpose |
Cast a jQuery (xhiJQ) object. |
Example |
castJQ( $top_box ); // returns $top_box |
Arguments |
(positional) |
|
<data> - Data to cast as jQuery object |
|
<alt_data> - Alt value to return |
Returns |
<data> if it is a xhiJQ object, <alt_data> otherwise |
Throws |
None |
|
|
Summary |
castList( <data>, <alt_data>, <option_map> ); |
Purpose |
Cast a list. |
Example |
castList( [] ); // returns the array |
Arguments |
(positional) |
|
<data> - Data to cast as list |
|
<alt_data> - Optional alt value to return. Default is __undef. |
|
<option_map> - Optional constraint map |
|
+ _do_warn_ - Log warnings. Default is __true. |
|
+ _is_empty_ok_ - Allow empty list. Default is __true. |
|
+ _max_length_ - Max allowed length. Default is __undef. |
|
+ _min_length_ - Min allowed length. Default is __undef. |
Returns |
<data> if it is an array and passes constraints, |
|
<alt_data> otherwise |
Throws |
None |
|
|
Summary |
castMap( <data>, <alt_data> ); |
Purpose |
Cast a map. |
Example |
castMap( {} ); // returns the object |
Arguments |
(positional) |
|
<data> - data to cast as map |
|
<alt_data> - alt value to return |
Returns |
<data> if it is a map, <alt_data> otherwise |
Throws |
None |
|
|
Summary |
castNum( <data>, <alt_data>, <option_map> ); |
Purpose |
Cast an integer. |
Example |
castNum( '25.425' ); // returns 25 |
Arguments |
(positional) |
|
<data> - data to cast as int |
|
<alt_data> - alt value to return. Default is __undef. |
|
<option_map> - Optional constraint map |
|
+ _do_autobound_ - Auto bound input to min/max as appropriate. |
|
Default is __false. |
|
+ _do_warn_ - Log warnings. Default is __false. |
|
+ _max_num_ - Max allowed value. Default is __undef. |
|
+ _min_num_ - Min allowed value. Default is __undef. |
Returns |
|
|
If a number, returns the number rounded to nearest int. |
|
If a string, returns the number rep rounded to nearest int. |
|
Otherwise <alt_data>. |
Throws |
None |
|
|
Summary |
castObj( <obj_type>, <data>, <alt_data> ); |
Purpose |
Cast an object. |
Example |
castObj( 'styleSheetList', document.styleSheets ); |
Arguments |
(positional) |
|
<obj_type> - string of object type (see Example) |
|
<data> - data to cast as <obj_type> object |
|
<alt_data> - alt value to return |
Returns |
<data> if an <obj_type> object, <alt_data> otherwise |
Throws |
None |
|
|
Summary |
cast( <data>, <alt_data>, <option_map> ); |
Purpose |
Cast a RegExp. |
|
|
Summary |
castStr( <data>, <alt_data> ); |
Purpose |
Cast a string. |
Example |
castStr( 25.425 ); // returns '25.425' |
Arguments |
(positional) |
|
<data> - Data to cast as string |
|
<alt_data> - Alt value to return |
|
<option_map> - Optional constraints |
|
+ _do_warn_ - Log warnings. Default is __false. |
|
+ _filter_rx_ - A regex filter that must be passed |
|
+ _is_empty_ok_ - Allow blank string. Default is __true. |
|
+ _max_length_ - Max allowed length. Default is __undef. |
|
+ _min_length_ - Min allowed length. Default is __undef. |
Returns |
<data> if a string, or a number converted to a string, |
|
<alt_data> otherwise |
Throws |
None |
|
|
Summary |
safeJsonParse( <json_str>, <alt_data> ); |
Purpose |
Parse JSON safely. |
Example |
my map = safeJsonParse( '{}', {} ); |
Arguments |
(positional) |
|
<json_str> - JSON string to parse |
|
<alt_data> - Alt return if parsing fails |
Returns |
Parsed JSON or alt_data |
Throws |
None |
|
|
Summary |
safeJsonStringify( <data>, <alt_data> ); |
Purpose |
Stringify JSON safely. |
Example |
my str = safeJsonStringify( {}, '{}' ); |
Arguments |
(positional) |
|
<data> - Data structure to stringify |
|
<alt_data> - Alt return if parsing fails |
Returns |
JSON string on success, alt_data on failure |
Throws |
None |
These are required for third-tier methods, and are shown in alphabetical order.
|
|
Summary |
cloneData( <data> ); |
Purpose |
Deep clone non-recursive data structures fast. |
Example |
cloneData( data, [] ); // return copy data or list on fail |
Arguments |
(positional) |
|
<arg_data> - data to clone |
|
<alt_data> - alt if clone fails |
Returns |
Success: A deep copy. |
|
Failure: alt_data (malformed, recursive structures) |
Throws |
None |
|
|
Summary |
extendList( <base_list>, <extend_list> ); |
Purpose |
Extend <base_list> with contents of <extend_list>. |
Example |
extendList( [0], [1,2,3] ); // returns [0,1,2,3] |
Arguments |
(positional) |
|
<base_list> - list to extend |
|
<extend_list> - list to append to base_list |
Returns |
base_list after change |
Throws |
None |
|
|
Summary |
getNowMs( <do_local> ); |
Purpose |
Get timestamp. |
Example |
getNowMs(); // returns 1486283077968 |
Arguments |
|
|
+ <do_local> - Subtract local TZ offset if true |
Returns |
The current timestamp in milliseconds |
Throws |
None |
|
|
Summary |
getNumSign( <data> ); |
Purpose |
Convert number into -1 or 1. |
Example |
getNumSign( '-25' ); // returns -1 |
Arguments |
|
|
<data> - number or string to convert to -1 or 1 |
Returns |
-1 if the processed number is less than 0, |
|
otherwise 1. |
Throws |
None |
Returns the log object singleton with these capabilities.
|
|
Summary |
logObj._setLogLevel_( <log_level> ); |
Purpose |
Return log4j-style log object singleton. |
Example |
|
|
logObj._setLogLevel_('_warn_'); |
|
logObj._logMsg_('_warn_', 'This will show'); |
|
logObj._logMsg_('_info_', 'This will not'); |
|
logObj._getLevelName_(); // '_warn_' |
|
logObj._getLevelIdx_(); // 4 |
Methods |
|
|
Log level is based on syslog values and is one of the following: |
|
` '[emerg |
|
+ _getLevelName_() - Return log level, e.g. '_warn_'. |
|
+ _getLevelIdx_() - Return log level index. 0=emerg,4=warn,7=debug |
|
+ _logMsg_( <log_level>, <message_str> ) - Log message string with |
|
<log_level> urgency. Messages with urgency below the threshold |
|
are not presented to the log. This is provided so developers |
|
can see if log level is more permissive than a threshold: |
|
if ( logObj._getLevelIdx_() > 3 ) { ... } |
|
+ _setLoglLevel_(<log_level>) - Set threshold urgency. |
Returns |
Differs per method |
Throws |
None |
|
|
Summary |
makeArgList( <arg_obj> ); |
Purpose |
Make a real array from data in argument object. |
Example |
makeArgList( arguments ); // returns [ ... ] |
Arguments |
|
|
<arg_obj> - an argument object ('arguments' in functions) |
Returns |
An array of argument values |
Throws |
None |
|
|
Summary |
makePadStr( <str>, <count>, [ <char>, <do_left> ] ); |
Purpose |
Pad <str> with <char> for <count> digits. |
Example |
makePadStr( 25, 3 ); // return ' 25' |
|
makePadStr( 25, 3, '0' ); // returns '025' |
Arguments |
(positional) |
|
<str> - The string to pad |
|
<count> - The desired length |
|
<char> - The padding character (default is space) |
|
<do_left> - Pad left (default is true), otherwise right |
Returns |
A trimmed and padded string |
Throws |
None |
|
|
Summary |
makeEscRxStr( <str> ); |
Purpose |
Escape a regular expression string. |
Example |
makeEscRxStr( '[]' ) // returns '\[\]\' |
Arguments |
<str> to escape |
Returns |
Escaped regular expression string |
Throws |
None |
Other |
See http://stackoverflow.com/questions/3115150 |
_makeExtractMap_
|
|
Summary |
makeExtractMap( <base_map>, <key_list> ); |
Purpose |
Make a new map using <key_list>. |
Example |
makeExtractMap( { a:1, b:2 }, ['a'] ); // returns { a:1 } |
Arguments |
(positional) |
|
<base_map> - Map to extract values from |
|
<key_list> - List of keys to copy (shallow) |
|
Default: all keys |
Returns |
Newly created map |
Throws |
None |
|
|
Summary |
makeRxObj( <pattern>, <options> ); |
Purpose |
Make a regular expression object. |
Example |
makeRxObj( '\\s*hello\\s*', 'i' ); |
Arguments |
(positional) |
|
<pattern> - a string to convert into a regexp |
|
<options> - an option string |
Returns |
A regular expression object |
Throws |
None |
|
|
Summary |
makeScrubStr( <str>, <do_space> ); |
Purpose |
Remove HTML tags and trim string. |
Example |
makeScrubStr( '<h1>Hello</h1><p>Hi</p>', __true ); |
|
// returns 'Hello Hi' |
Arguments |
(positional) |
|
<str> - A string to scrub |
|
<do_space> - Add a space between groups |
Returns |
The scrubbed string |
Throws |
None |
|
|
Summary |
makeUcFirstStr( <str> ); |
Purpose |
Capitalize the first letter of a string. |
Example |
makeUcFirstStr( 'hello' ); // returns 'Hello' |
Arguments |
<str> - A string to process |
Returns |
The string with the first character capitalized |
Throws |
None |
These are the remaining methods, and are shown in alphabetical order.
|
|
Summary |
checkDateStr( <arg_map> ); |
Purpose |
Check validity of a date string. |
Example |
checkDateStr( { _date_str_: '2017-02-29' } ); // __false |
|
checkDateStr( { _date_str_: '2016-02-29' } ); // __true |
|
|
|
// This method works only of strings in the formats |
|
// yyyy-mm-dd or yyyy/mm/dd and does not validate the time. |
Arguments |
<arg_map> |
|
+ _date_str_ - The date string to consider |
|
+ _order_str_ - The date form to check ('_us_' or ISO) |
Returns |
boolean |
Throws |
None |
|
|
Summary |
makeMetricStr( <number> ); |
Purpose |
Convert number to a 3 digit string with suffix. |
Example |
makeMetricStr( 968125965968 ); // '968G' |
|
makeMetricStr( 125965968 ); // '126M' |
|
makeMetricStr( 965968 ); // '966K' |
|
makeMetricStr( 968 ); // '968' |
|
configMap._metric_table_ can be expanded as needed. |
|
Place largest numbers first. |
Arguments |
|
|
+ <number> - The number to process |
|
Settings : Uses configMap._metric_table_ |
Returns |
String |
Throws |
None |
|
|
Summary |
clearMap( <data_map>, <key_list>, <do_undef> ); |
Purpose |
Process some or all map key values. |
Example |
|
|
clearMap( my_map ); // Delete all keys |
|
clearMap( my_map, [ 'name', 'serial_number' ] ); // Delete 2 keys |
|
clearMap( my_map, __undef, __true ); // Set all values to __undef |
Arguments |
(positional) |
|
<data_map> - Map to modify. Required. |
|
<key_list> - List of keys to process. Default is all keys. |
|
Provide any non-list value (like 0) to use default. |
|
<do_undef> - If __true, will set the value of all processed keys to |
|
__undef. Otherwise, all processed keys will be deleted from |
|
the map. Default is __false. |
Returns |
The modified map |
Throws |
None |
|
|
Summary |
encodeHtml( <str>, <do_exclude_amp> ); |
Purpose |
Single-pass encode HTML entities from string. |
Example |
|
|
str = encodeHtml( "<h1>'Help me!'</h1> she said" ); |
|
__logMsg( 'info', str ); |
|
// > <h1&ht;"Help me!"</h1> she said.' |
|
|
|
str = encodeHtml( "<h1>'Help me!'</h1> & fast!", __false ); |
|
__logMsg( 'info', str ); |
|
// > <h1&ht;"Help me!"</h1> & fast!' |
|
|
|
str = encodeHtml( "<h1>'Help me!'</h1> & fast!", __true ); |
|
__logMsg( 'info', str ); |
|
// > <h1&ht;"Help me!"</h1> & fast!' |
Arguments |
(positional) |
|
<str> - The HTML string to encode |
|
<do_exclude_amp> - Exclude ampersand from encoding. |
|
Default is __false. |
Returns |
Modified string |
Throws |
None |
|
|
Summary |
getBaseDirname.call( <path_str>, <delim_str> ); |
|
getBasename( <path_str> ); getDirname( <path_str> ); |
Purpose |
Returns the last filename of a path or the dirname. |
Examples |
getBaseDirname.call( '_base_', /var/log/demo.log') |
|
// returns 'demo.log' |
|
getBaseDirname.call( null, '/var/log/demo.log' ) |
|
// returns '/var/log' |
Arguments |
(positional) |
|
<path_str> - Path string. Default is __blank. |
|
<delim_str> - Delimeter string. Default is '/'. |
|
If context_str === '_base_' returns basename, otherwise |
|
provides dirname. |
Returns |
Modified string |
Throws |
None |
|
|
Summary |
getListAttrIdx( <list>, <key>, <data> ); |
Purpose |
Find the first map in <list> where the value of. |
|
<key> is <data>. |
|
Examples : getListAttrIdx( [ { a: 1 } ], 'a', 1 ); // returns 0 |
|
getListAttrIdx( [ { a: 1 } ], 'b', 1 ); // returns -1 |
|
getListAttrIdx( [ { a: 1 } ], 'a', 9 ); // returns -1 |
Arguments |
(positional) |
|
<list> - List of maps. Default is []. |
|
<key> - The key to match. Default is __blank. |
|
<data> - The data expected for key. Default is __undef. |
Returns |
Returns integer index or -1 if not found |
Throws |
None |
|
|
Summary |
getListAttrMap( <list>, <key>, <data> ); |
Purpose |
Find the first map in <list> where the value of. |
|
<key> is <data>. |
Examples |
getListAttrMap( [ { a: 1 } ], 'a', 1 ); // returns map |
|
getListAttrMap( [ { a: 1 } ], 'b', 1 ); // returns undef |
|
getListAttrMap( [ { a: 1 } ], 'a', 9 ); // returns undef |
Arguments |
(positional) |
|
<list> - List of maps. Default is []. |
|
<key> - The key to match. Default is __blank. |
|
<data> - The data expected for key. Default is __undef. |
Returns |
Returns found map or undef if not found |
Throws |
None |
|
|
Summary |
makeColumnList( <list>, <col_id>, [ filter_fn ] ); |
Purpose |
Extract a column from a list-of-lists or list-of-maps table. |
|
Examples : list = makeColumnList( enum_table, 'value' ); |
|
list = makeColumnList( grid_table, 5 ); |
|
list = makeColumnList( enum_table, 5, function ( row ) {...} ) |
Arguments |
(positional) |
|
<list> - List of maps or lists. Required. |
|
<col_id> - The key or index of the column. Required. |
|
<filter_fn> - Optional filter. Recieves the table row as argument. |
|
If it returns a non-true value the row is discarded. |
Returns |
Returns found map or undef if not found |
Throws |
None |
|
|
Summary |
getListDiff( <list1>, <list2> ); |
Purpose |
Find all elements common between two lists. |
|
This is _not_ a deep comparison; two similar lists or maps |
|
will be reported as different unless they point the the same |
|
data structure. |
Example |
getListDiff( [ 'a','b' ], [ 'b','c','d' ] ); |
|
// Returns [ 'a', 'c', 'd' ] |
Arguments |
(positional) |
|
<list1> - The first list |
|
<list2> - The second list |
Returns |
A list of unique elements found in first list |
|
followed by unique elements found in the second. |
Throws |
None |
|
|
Summary |
getListValCount( <list>, <data> ); |
Purpose |
Count the number of elements in <list> that === <data>. |
Example |
getListValCount( [ 'a','b','a' ], 'a' ); // Returns 2 |
Arguments |
(positional) |
|
<list> - The list to examine. Default is []. |
|
<data> - The data value to match. Default is __undef. |
Returns |
Match count |
Throws |
None |
|
|
Summary |
getStructData( <data>, <path_list> ); |
Purpose |
Extract a value from <data> by <path_list>. |
Example |
_getStructData_({ foo : { bar : 'hi!' }}, ['foo','bar']); |
|
// Returns 'hi!' |
|
|
|
// The key depth limit is set to 100. If this |
|
// is met, a warning is logged and undef returned. |
Arguments |
(positional) |
|
<data> - An array or map |
|
<path_list> - A list of map or array keys in order of depth |
Returns |
Success - Requested value |
|
Failure - undefined |
Throws |
None |
|
|
Summary |
getTzCode(); |
Purpose |
Return the local timezone code. |
|
|
Summary |
makeClockStr( <time_ms>, <time_idx>, <do_am>, <do_local> ); |
Purpose |
Create HH:MM:SS time string from UTC time integer in ms. |
Example |
clock_str = makeClockStr( 1465621376000 ); // '05:02:56' |
|
// Remember to use your local timezone offset if you want to |
|
// show local time. Example: |
|
// local_ms = getNowMs( __true ) |
Arguments |
(positional) |
|
<time_ms> - UTC time in milliseconds |
|
<time_idx> - Date precision. Default is __3. |
|
-3 === [DDd:]HHh:MMm:SSs |
|
-2 === [DDd:]HHh:MMm |
|
-1 === [DDd:]HHh |
|
0 === '' |
|
1 === HH |
|
2 === HH:MM |
|
3 === HH:MM:SS |
|
<do_ampm> - Do am/pm flag. Default is __false. |
|
<do_local> - Use local time. Default is __false. |
Returns |
String |
Throws |
None |
|
|
Summary |
makeCommaNumStr( <arg_map> ); |
Purpose |
Convert a number into a string optimized for readability. |
Example |
makeCommaNumStr({ _input_num_ : 1999 }) |
|
// Returns '2.0k' |
Arguments |
<arg_map> with the following keys |
|
+ _input_num_ - The number to format, e.g. 123598 |
|
+ _round_limit_exp_ - The size (10^exp) of number after which |
|
a rounded value is returned. Default is __3. |
|
+ _round_limit_str - The limit name. Default is 'k'. |
|
+ _round_unit_exp_ - The size (10^exp) of number to group as |
|
a unit. Default is __3, e.g. 1,000's. |
|
+ _round_dec_count_ - Number of decimal places to keep |
|
in the mantisa when rounding to units |
|
+ _nornd_dec_count_ - Number of decimal places to keep when |
|
NOT rounded to units |
Returns |
|
|
Success - Returns formated string |
|
Failure - Blank string |
Throws |
None |
|
|
Summary |
makeDateStr( <arg_map> ); |
Purpose |
Create a string from a date object. |
|
or a UTC time number (in milliseconds). |
Example |
|
|
makeDateStr({ _date_obj_ : new __Date() }); |
|
// Returns a string like '2016-09-18' |
|
|
|
makeDateStr({ _date_obj_ : new __Date(), _time_idx_ : 3 }); |
|
// Returns a string like '2016-09-18 12:45:52' |
|
|
|
makeDateStr({ _date_ms_ : 1474311626050 }) |
|
// Returns '2016-09-19' |
|
|
|
// Remember to use your local timezone offset if you want to |
|
// show local time. Example: |
|
// tz_offset_ms = date_obj.geteTimezoneOffset() * 60000; |
|
// local_ms = raw_utc_ms - tz_offset_ms; |
Arguments |
<arg_map> with these keys |
|
+ _date_obj_ : A valid date object. |
|
+ _date_ms_ : A date time in ms. |
|
If neither date_obj or date_ms is provided, will use the |
|
current date. If BOTH are provided, _date_ms_ will be used in |
|
preference to date_obj. |
|
+ _time_idx_ : See _makeClockStr_ to determine |
|
the clock string format. Default is __0. |
|
+ _order_str_ : |
|
Request '_us_' results in stupid-format: mm/dd/yyyy hh:mm:ss. |
|
Default is __blank. |
Returns |
|
|
Success - Formated string |
|
Failure - Blank string |
Throws |
None |
|
|
Summary |
makeDebounceFn( <arg_map> ); |
Purpose |
Create a function which will call a provided method a. |
|
specified time period after it was last called. Any |
|
call that occurs within the time period resets the clock. |
Example |
dbFn = makeDebounceFn({ _fn_ : myMethodFn, _delay_ms_: 250 }); |
|
dbFn( 'method argument' ); // Executes in 250ms |
|
|
|
// The method, myMethodFn, is always invoked with the latest |
|
// arguments provided. Consider this example: |
|
dbFn( 'myArgs' ); dbFn( 'Hello World' ); |
|
|
|
// The argument 'myArgs' will likely be discarded and |
|
// myMethodnFn( 'Hello World' ) will likely be invoked. |
|
|
TODO |
Provide a means to reset _do_asap_ with either |
|
a timeout or other mechanism. |
|
|
Arguments |
<arg_map> with the following keys |
|
+ _fn_ - The method to execute Required. |
|
+ _delay_ms_ - The time of inactivity Default is __0. |
|
+ _ctx_data_ - Method context Default is __undef. |
|
+ _do_asap_ - Fire method on first call Default is __false. |
Returns |
|
|
Success - The debounce function as described above. |
|
Failure - undef |
Throws |
None |
|
|
Summary |
makeEllipsisStr( <arg_map> ); |
Purpose |
Shorten a string to a maximum length and append ellipsis. |
|
if it is exceeded. |
Example |
|
|
makeEllipsisStr({ |
|
_input_str_ : 'hee haw and the boys', |
|
_char_limit_int_ : 10, |
|
_do_word_break_ : __true |
|
}); |
|
// returns 'hee haw ...' |
Arguments |
<arg_map> with the following keys |
|
+ _char_limit_int_ : Maxiumum allowed chars. Default is __0. |
|
+ _do_word_break_ : Break at word boundries. Default is __true. |
|
+ _input_str_ : The string to shorten |
Returns |
String |
Throws |
None |
|
|
Summary |
makeErrorObj( <name>, <msg> ); |
Purpose |
A convenient method to create an error object. |
Example |
makeErrorObj( 'notCool', 'This is not cool' ); |
Arguments |
(positional) |
|
<name> - Error name. Default is 'error'. |
|
<msg> - Error message. Default is __blank. |
Returns |
A newly constructed error object |
Throws |
None |
|
|
Summary |
makeGuidStr() |
Purpose |
Return a standard GUID. |
Example |
makeGuidStr(); |
Arguments |
None |
Returns |
A GUID |
Throws |
None |
|
|
Summary |
makeMapUtilObj() |
Purpose |
Creates a map utility object used to streamlining. |
|
list.map() functions and avoid nesting. |
Example |
|
|
// 1. Create a map_util object: |
|
var map_util_obj = makeMapUtilObj(); |
|
|
|
// 2. (optional) Set any data your map function will use. |
|
map_util_obj._setArgList_ = [ name_list ]; |
|
|
|
// 3. Create a function that for element of the array. |
|
// The arg_list provided is set in step 2: |
|
function mapUtil_renameFn ( field_data, idx, list, arg_list ) { |
|
var |
|
name_list = arg_list[ __0 ], |
|
field_key = name_list[ idx ], |
|
field_str = __Str( field_data ) |
|
; |
|
|
|
// Return [ key, value ] to add to map. |
|
// Return undef to not add anything. |
|
return [ field_key, field_str ]; |
|
} |
|
|
|
// 4. Set the function in the utility |
|
map_util_obj._setMapFn_( mapUtil_renameFn ); |
|
|
|
// 5. Initialize the result map. You need this pointer. |
|
result_map = {}; |
|
map_util_obj._setResultMap_( result_map ); |
|
|
|
// 6. Invoke the map function: |
|
my_list.map( map_util_obj._invokeFn_ ); |
|
|
|
// 7. result_map will now contain the key value pairs return by |
|
mapUtil_renameFn for the provided list. |
|
|
|
// A closure creates unique private variables in each |
|
// instance returned. Examples include argList, resultMap, and mapFn. |
|
|
Returns |
A mapUtil object |
|
|
Summary |
makeOptionHtml( <arg_map> ); |
Purpose |
Create an HTML string with option tags. |
Example |
makeOptionHtml({ _val_list_ : [1,2,3,4] }); |
Arguments |
<arg_map> with the following keys |
|
+ _enum_table_ : A table of _name_ and _value_. Default is []. |
|
+ _match_list_ : List of values to be selected. Default is []. |
|
This is useful for multi-select fields. |
Returns |
An HTML option select string |
Throws |
None |
|
|
Summary |
makePctStr( <ratio>, <precition_int> ); |
Purpose |
Convert a decimal ratio into a readable % string. |
Example |
makePctStr( 0.529863, 1 ); |
Arguments |
(positional) |
|
<ratio> - Ratio to convert. 1 = 100%. Default is __0. |
|
<precision_int> - Count of digits after decimal. Default is __0. |
Returns |
A percentage string |
Throws |
None |
|
|
Purpose |
Make an array of checkboxes from a list. |
|
|
Summary |
makeReplaceFn( <search_str>, <replace_str> ); |
Purpose |
Return a high-performance function that. |
|
replaces <search_str> with <value_str>. |
Example |
|
|
fn = makeReplaceFn( '_x_', 'fred' ); |
|
__logMsg( 'info', fn('you do not know {_x_}.') ); |
|
// Prints 'you do not know fred.' |
Arguments |
(positional) |
|
<search_str> - The symbol to replace. |
|
It must be wrapped in braces in template string. |
|
<replace_str> - The replacement string. |
Returns |
A replacement function |
Throws |
None |
|
|
Summary |
makeRekeyMap( <struct_data>, <key_map>, <mode_str> ); |
Purpose |
Change all keys or values in a map to the new values. |
|
Executes deep renaming through arrays and objects. |
|
A hard limit of 100 000 iterations is supported. |
Examples |
|
|
makeRekeyMap( |
|
{ a : 1, b : 2, c : [] }, |
|
{ a : '_x_', b : '_y_', c : '_z_' }, |
|
'_rekey_' |
|
); |
|
// Returns { _x_:1, _y_:2, _z_:[] } |
|
|
|
makeRekeyMap( |
|
{ a : 1, b : 2, list : [ { c : [] } ] }, |
|
{ a : '_x_', b: '_y_', c : 22 }, |
|
'_reval_' |
|
); |
|
// Returns { a : '_x_', b : '_y_', list : [ { c : 22 } ] } |
Arguments |
(positional) |
|
<struct_data> - A complex structure to rekey or revalue |
|
<key_map> - A key map pointing to values |
|
<mode_str> - '_rekey_' or '_reval_' |
Returns |
A new map, list, or other data |
|
|
Purpose |
Convert arg_key_list into a map with each key assigned. |
|
the value of arg_seen_data. Default is __true. |
|
|
Purpose |
Concatenate a number of key-values. |
|
into a single string |
|
|
Purpose |
Create a list of time labels quantitized to match. |
|
standard time intervals |
Example |
|
|
series_map = makeSeriesMap({ |
|
_max_ms_ : 1465459980000, |
|
_min_ms_ : 1465452840000, |
|
_tgt_count_ : 12 |
|
}); |
Arguments |
<arg_map> with the following keys: |
|
+ _max_ms_ : Start local-time milliseconds |
|
+ _min_ms_ : End local-time in milliseconds |
|
+ _tgt_count_ : Desired number of divisions (+/- 50%) |
|
|
Returns |
|
|
A map useful for plotting a quantized time series like so: |
|
-+-----+-----+-----+-----+-----+-----+-----+-----+-----+ |
|
23:20 23:30 23:40 23:50 00:00 00:10 00:20 00:30 00:40 00:50 |
|
2016-06-07 | 2016-06-08 |
|
Example output: |
|
{ _date_list_ : [ |
|
{ _date_str_ : '2016-06-07', |
|
_width_ratio_ : 0.38655462184873 |
|
}, |
|
{ _date_str_ : '2016-06-08', |
|
_width_ratio_ : 0.61344537815126 |
|
} |
|
], |
|
_left_ratio_ : 0.050420168067226, |
|
_time_list_ : [ |
|
'23:20', '23:30', '23:40', '23:50', '00:00', '00:10', |
|
'00:20', '00:30', '00:40', '00:50', '01:00', '01:10' |
|
], |
|
_time_idx_ : 1, |
|
_unit_count_ : 12, |
|
_unit_ms_ : 600000, |
|
_unit_name_ : '10m', |
|
_unit_ratio_ : 0.084033613445378, |
|
} |
|
|
|
// _date_list_ = list of dates and position of date labels |
|
// _left_ratio_ = starting postion of time stamps |
|
// _time_idx_ = precision to show 0 = '', 1=HH, 2=HH:MM, 3=HH:MM:SS |
|
// _time_list_ = list of time labels |
|
// _unit_count_ = number of time labels units returned |
|
// _unit_ms_ = number of ms in |
|
// _unit_ratio_ = ratio per time unit for plotting center of time |
|
|
|
// Remember to use your local timezone offset if you want to |
|
// show local time. See example on makeClockStr, above. |
Throws |
None |
|
|
Summary |
makeThrottleFn( <arg_map> ); |
Purpose |
Create a function which will call a provided method only. |
|
once per time period. |
Example |
thFn = makeThrottleFn({ _fn_ : myMethodFn, _delay_ms_: 250 }); |
|
thFn( 'method argument' ); // Executes in 250ms |
|
|
|
// The method, myMethodFn, is always invoked with the latest |
|
// arguments provided. Consider this example: |
|
thFn( 'myArgs' ); thFn( 'Hello World' ); |
|
|
|
// The argument 'myArgs' will likely be discarded and |
|
// myMethodnFn( 'Hello World' ) will likely be invoked. |
|
|
TODO |
Provide a means to reset _do_asap_ with either |
|
a timeout or other mechanism. |
|
|
Arguments |
<arg_map> with the following keys |
|
+ _fn_ - The method to execute Required. |
|
+ _delay_ms_ - The minimum time between calls Default is __0. |
|
+ _ctx_data_ - Method context Default is __undef. |
|
+ _do_asap_ - Fire method on first call Default is __false. |
Returns |
|
|
Success - The throttle function as described above. |
|
Failure - undef |
Throws |
None |
|
|
Purpose |
Replace symbols in a template surrounded by braces. |
|
'{}' with the symbol provided in the lookup map. |
Example |
|
|
makeTmpltStr({ |
|
_do_encode_html_ : __true, |
|
_input_str_ : '{_name_} says "{_saying_}"', |
|
_lookup_map_ : { _name_ : 'Fred', _saying_ : 'hello!' } |
|
}); |
|
// Returns 'Fred says hello!' |
|
|
Arguments |
<arg_map> with the following keys: |
|
+ _do_encode_html_ : When __true replaced values will be html encoded. |
|
Default is __false. |
|
+ _input_str_ : A string template like so: |
|
'This person name {_p1_} said to the other person {_p2_}'. |
|
Default is __blank. |
|
+ _lookup_map_ : A map of values to replace, like so: |
|
{ _p1_ : 'fred', _p2_ : 'barney' }. Default is {}. |
|
Keys in a lookup map can be split be to look up nested values. |
|
For example, the key '_p1_._p2_' will look up the value |
|
found at { _p1_: { _p2_ : value } } in the lookup_map. |
|
+ _tmplt_rx_ : A regular expression object to define replace patterns. |
|
Default is configMap._tmplt_rx_ |
|
+ _return_map_ : Append found keys to this map if provided. |
|
Default is __undef |
Returns |
Filled-out template string |
Throws |
None |
|
|
Purpose |
Merge properties of extend_map into base_map. |
|
// Warning : This does not deep copy the extend map. |
|
// This often provides the desired results. |
|
deep_map = cloneData( extend_map ); |
|
merge_map = mergeMaps( base_map, deep_map ); |
|
|
Purpose |
Run the <arg_fn> function every <arg_ms> milliseconds. |
|
either <arg_count> number of times or until the function |
|
returns __false, whichever comes first. |
Arguments |
(positional) |
|
<fn> - Fn to poll. Return __false to stop. Required. |
|
<ms> - Milliseconds between calls. Default is __0. |
|
<count> - Maximum count. Default is __null. |
|
<finish_fn> - Fn to run on completion. Default is __null. |
Returns |
|
|
__true : polling started |
|
__false : polling declined |
|
|
Summary |
pushUniqListVal( <arg_list>, <data> ); |
Purpose |
Push val into list IFF not already present |
|
|
Summary |
rmListVal( <base_list>, <val1>, ... <valN> ); |
Purpose |
Remove one or more values from a base_list in-place. |
Example |
rmListVal( base_list, 'a', 1, null ); |
|
|
Arguments |
(positional) |
|
<base_list> - list to operate on. |
|
<val1..n> - values to remove |
Returns |
Count of removed values. base_list is changed. |
Throws |
None |
|
|
Summary |
setConfigMap( <input_map>, <settable_map>, <config_map> ); |
Purpose |
Set configMap in consistent way across modules. |
Example |
// Use in modules as below: |
|
function setConfigMap ( arg_input_map ) { |
|
setConfigMap( input_map, settableMap, configMap ); |
|
} |
|
// Where settableMap shows allowable keys and types |
Arguments |
<arg_map> with the following keys: |
|
+ _input_map_ - Map of key-values to set in config |
|
+ _settable_map_ - Map of allowable keys with future plans |
|
to support types and ranges (see agrc) |
|
+ _config_map_ - Resulting config map |
Returns |
Modified config_map |
Throws |
None |
|
|
Purpose |
Get all unique keys in a data structure as list or map. |
Example |
_makeDeepData_({ foo:{ bar:1 }, bar:2}); |
|
// Returns [ 'foo', 'bar' ] |
|
_makeDeepData_({ foo:{ bar:1 }, bar:2}, '_map_'); |
|
// Returns { bar: 2 } // flattens map |
|
|
|
// The key depth limit is set to __100. If this |
|
// is met, a warning is logged and __undef returned |
Arguments |
(positional) |
|
<base_struct> - An array or map |
|
<mode_str> - _list_ or _map_. Default is _list_ |
Returns |
|
|
Success - A list of a unique keys sorted |
|
Failure - An empty list |
|
|
Purpose |
Set a deep structure attribute value. |
Example |
_setStructData_({ foo:{ bar:1 }}, [ 'foo','bar' ], 99 ); |
|
// Returns __true and set the and adjusts the structure: |
|
// { foo : { bar : 99 } } |
Example |
_setStructData_( [ { car : [ 'seats', 'tyres' ] } ], |
|
[ 0, 'car', 1 ], 'Meyers!' ] ); |
|
// Returns __true and set the and adjusts the structure: |
|
// [ { car : [ 'seats', 'Meyers!' ] } ] |
Example |
_setStructData_( [], [ null, 'car', null ], 'Meyers!' ); |
|
// Returns __true and adjust the structure: |
|
// [ { car : [ 'Meyers!' ] } ] |
Example |
_setStructData_( [], [ 'car', null ], 'Meyers!' ); |
|
// Returns __false, as 'car' cannot be a property of the |
|
// base structure. It must be null which means "next |
|
// available array item" or an integer. |
|
|
|
// The key depth limit is set to __100. If this |
|
// is met, a warning is logged and __undef returned |
Arguments |
(positional) |
|
<base_struct> - An array or map to add a value |
|
<path_list> - A list of map or array keys in order of depth |
|
<val_data> - A data value to set |
Returns |
|
|
Success - __true |
|
Failure - __false |
|
|
Summary |
ShuffleList( <list> ) |
Purpose |
Shuffle a list in-place. |
Example |
shuffleList( [1,2,3,4] ) returns [ 3,1,4,2 ] |
|
Technique : |
|
1. Count down from end of array with last_idx |
|
2. Randomly pick element from between 0 and last_idx |
|
3. Swap pick element with last_idx element |
Arguments |
(positional) |
|
<list> - The list to shuffle |
Returns |
__true on success |
Throws |
None |
|
|
Summary |
trimStrList( <list> ) |
Purpose |
Trim all strings in a list. |
Example |
trimStrList( [ ' padd string ', 'anudder '] ); |
Arguments |
(positional) |
|
<list> - The list to trim leading and ending whitespace |
Returns |
A new list of trimmed strings |
Throws |
None |