From 6d6484b60ec774c27ccc372591953615a6de66e5 Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Fri, 4 Feb 2022 07:27:23 +0100 Subject: [PATCH] Update typescript --- fn-tools.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/fn-tools.ts b/fn-tools.ts index 8d488f4..c547adf 100644 --- a/fn-tools.ts +++ b/fn-tools.ts @@ -1,27 +1,26 @@ - /** * Returns a JSON with an updated value * @param {string} key * @param {string} newValue * @param {JSON} json * @return {JSON} + * @example * - * Example * const person = {name: 'James', age: 20, pets: ['dog', 'cat']}; * updateJSON('name', 'maria', person); * // {"age": 20, "name": "maria", "pets": ["dog", "cat"]} * * Others: "modify" in Ramda */ -function updateJSON(key, newValue, json) { +function updateJSON(key: string, newValue: any, json: JSON): JSON { // JSON to Array return Object.entries(json).map( - function (row) { + function (row: any[]): any[] { // Replace value return row[0] == key ? row.slice(0, 1).concat(newValue) : row } ).reduce( - function (jsonUpdate, valueUpdate) { + function (jsonUpdate: any, valueUpdate: any): JSON { // Array to JSON jsonUpdate[valueUpdate[0]] = valueUpdate[1]; return jsonUpdate; @@ -42,9 +41,9 @@ export default updateJSON * range(1, 4) * // => [1, 2, 3, 4] */ -function range(start: number, stop: number=undefined, step: number=1): number[] { - const startArray = stop === undefined ? 0 : start; - const stopArray = stop === undefined ? start : stop; +function range(start: number, stop: number | null=null, step: number=1): number[] { + const startArray = stop === null ? 0 : start; + const stopArray = stop === null ? start : stop; return Array.from({ length: (stopArray - startArray) / step + 1}, (_, i) => startArray + (i * step)); }