micro-fp-tools-js/fn-tools.ts

112 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-02-01 15:45:07 +01:00
/**
* Returns a JSON with an updated value
* @param {string} key
* @param {string} newValue
* @param {JSON} json
2022-02-03 21:04:13 +01:00
* @return {JSON}
2022-02-04 07:27:23 +01:00
* @example
2022-02-01 15:45:07 +01:00
*
* const person = {name: 'James', age: 20, pets: ['dog', 'cat']};
* updateJSON('name', 'maria', person);
* // {"age": 20, "name": "maria", "pets": ["dog", "cat"]}
*
* Others: "modify" in Ramda
*/
2022-02-04 07:27:23 +01:00
function updateJSON(key: string, newValue: any, json: JSON): JSON {
2022-02-01 15:45:07 +01:00
// JSON to Array
return Object.entries(json).map(
2022-02-04 07:27:23 +01:00
function (row: any[]): any[] {
2022-02-01 15:45:07 +01:00
// Replace value
return row[0] == key ? row.slice(0, 1).concat(newValue) : row
}
).reduce(
2022-02-04 07:27:23 +01:00
function (jsonUpdate: any, valueUpdate: any): JSON {
2022-02-01 15:45:07 +01:00
// Array to JSON
jsonUpdate[valueUpdate[0]] = valueUpdate[1];
return jsonUpdate;
}, {});
}
2022-02-03 21:01:56 +01:00
/**
* Returns an array with a sequence.
*
2022-02-04 07:18:31 +01:00
* @param {number} start - Beginning of the range.
* @param {number} stop - End of range.
* @param {number} step - Interval between numbers. Default is 1.
2022-02-03 21:06:03 +01:00
* @return {Array} Sequence.
2022-02-04 07:18:31 +01:00
* @example
*
* range(1, 4)
* // => [1, 2, 3, 4]
2022-02-03 21:01:56 +01:00
*/
2022-02-04 07:27:23 +01:00
function range(start: number, stop: number | null=null, step: number=1): number[] {
const startArray = stop === null ? 0 : start;
const stopArray = stop === null ? start : stop;
2022-02-03 21:01:56 +01:00
return Array.from({ length: (stopArray - startArray) / step + 1}, (_, i) => startArray + (i * step));
}
2022-02-03 21:36:41 +01:00
/**
* Creates a duplicate-free version of an array.
*
2022-02-04 07:18:31 +01:00
* @param {Array} array - The array to inspect.
* @return {Array} - Returns the new duplicate free array.
2022-02-03 21:36:41 +01:00
* @example
*
2022-02-04 07:18:31 +01:00
* uniqValuesArray([2, 1, 2, 2])
2022-02-03 21:36:41 +01:00
* // => [2, 1]
*/
2022-02-04 07:18:31 +01:00
function uniqValuesArray(array: any[]): any[] {
2022-02-03 21:36:41 +01:00
return (array != null && array.length)
? Array.from(new Set(array))
: [];
}
/**
* Increase the value of a `number`.
*
* @param {number} number The number to increase.
* @param {number} number Increase.
* @return {number} Returns the decrease.
* @example
*
* inc(4)
* // => 5
*
* inc(5, 3)
* // => 8
*
* dec(7.3)
* // => 8.3
*
*/
2022-02-04 07:18:31 +01:00
function inc(number: number, increase: number=1): number {
2022-02-03 21:36:41 +01:00
return number - increase;
}
/**
* Decrease the value of a `number`.
*
2022-02-04 07:18:31 +01:00
* @param {number} number - The number to decrease.
* @param {number} number - Decrease.
2022-02-03 21:36:41 +01:00
* @return {number} Returns the decrease.
* @example
*
* dec(4)
* // => 3
*
* dec(5, 3)
* // => 2
*
* dec(7.3)
* // => 6.3
*
* dec("foo")
* // => false
*/
2022-02-04 07:18:31 +01:00
function dec(number: number, decrease: number=1): number {
2022-02-03 21:36:41 +01:00
return number - decrease;
}