Methods
(static) call(key, keys, values) → {any}
Calls a function from a 'destination' list using a 'key' index found in a 'source' list.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
any | a JavaScript value viewed as a 'key' in the 'source' list. |
keys |
Array.<any> | a JavaScript array of values viewed as 'keys' in the 'source' list. |
values |
Array.<any> | a JavaScript array of values viewed as 'values' in the 'destination' list. |
Returns:
the value from the function in 'destination' list corresponding to the 'key' in the 'source' list.
- Type
- any
Example
let keys = [1, 2, 3, 4, 5, null];
let values = [function1, function2, function3, function4, function5, default];
let key = 3;
let value = mcode.swap(key, keys, values); // value = (return value of function3)
(static) ready()
Logs a message to the Console when the module is loaded to show version.
(static) swap(key, keys, values) → {any}
Swaps a 'key' from a 'source' list with a 'value' in a 'destination' list.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
any | a JavaScript value viewed as a 'key' in the 'source' list. |
keys |
Array.<any> | a JavaScript array of values viewed as 'keys' in the 'source' list. |
values |
Array.<any> | a JavaScript array of values viewed as 'values' in the 'destination' list. |
Returns:
the value from the 'destination' list corresponding to the 'key' in the 'source' list.
- Type
- any
Example
let list1 = [1, 2, 3, 4, 5, 0];
let list2 = ['one', 'two', 'three', 'four', 'five', 'default'];
let list3 = [false, true, false, true, false, false];
let list4 = [{ key: 1, property: ONE },
{ key: 2, property: TWO },
{ key: 3, property: THREE },
{ key: 4, property: FOUR },
{ key: 5, property: FIVE },
{ key: 0, property: DEFAULT }];
let key = null;
let value = null;
1) straight swap of two values...
key = 3;
value = mcode.swap(key, list1, list2); // value = 'three'
key = 6;
value = mcode.swap(key, list1, list2); // value = 'default'
key = 0;
value = mcode.swap(key, list1, list2); // value = 'default'
2) the same lists can be used the other way around...
key = 'three';
value = mcode.swap(key, list2, list1); // value = 3
key = 'six';
value = mcode.swap(key, list2, list1); // value = 0
3) any two lists on the same subject (i.e.: the same length) can be used...
key = 3;
value = mcode.swap(key, list1, list3); // value = false
key = 'three';
value = mcode.swap(key, list2, list4); // value = { key: 4, property: FOUR }
...seems simple enough, but keep in mind that the 'key' and 'value' in the lists can be any JavaScript value, including:
- a number
- a string
- a boolean
- an object
- a function
- a class
- a module
- a BigInt
- a Promise
- an Array
- a JSON object
- null
- undefined
etc.