Update typescript

This commit is contained in:
Andros Fenollosa 2022-02-04 07:27:23 +01:00
parent 3fed91ef59
commit 6d6484b60e

View File

@ -1,27 +1,26 @@
/** /**
* Returns a JSON with an updated value * Returns a JSON with an updated value
* @param {string} key * @param {string} key
* @param {string} newValue * @param {string} newValue
* @param {JSON} json * @param {JSON} json
* @return {JSON} * @return {JSON}
* @example
* *
* Example
* const person = {name: 'James', age: 20, pets: ['dog', 'cat']}; * const person = {name: 'James', age: 20, pets: ['dog', 'cat']};
* updateJSON('name', 'maria', person); * updateJSON('name', 'maria', person);
* // {"age": 20, "name": "maria", "pets": ["dog", "cat"]} * // {"age": 20, "name": "maria", "pets": ["dog", "cat"]}
* *
* Others: "modify" in Ramda * Others: "modify" in Ramda
*/ */
function updateJSON(key, newValue, json) { function updateJSON(key: string, newValue: any, json: JSON): JSON {
// JSON to Array // JSON to Array
return Object.entries(json).map( return Object.entries(json).map(
function (row) { function (row: any[]): any[] {
// Replace value // Replace value
return row[0] == key ? row.slice(0, 1).concat(newValue) : row return row[0] == key ? row.slice(0, 1).concat(newValue) : row
} }
).reduce( ).reduce(
function (jsonUpdate, valueUpdate) { function (jsonUpdate: any, valueUpdate: any): JSON {
// Array to JSON // Array to JSON
jsonUpdate[valueUpdate[0]] = valueUpdate[1]; jsonUpdate[valueUpdate[0]] = valueUpdate[1];
return jsonUpdate; return jsonUpdate;
@ -42,9 +41,9 @@ export default updateJSON
* range(1, 4) * range(1, 4)
* // => [1, 2, 3, 4] * // => [1, 2, 3, 4]
*/ */
function range(start: number, stop: number=undefined, step: number=1): number[] { function range(start: number, stop: number | null=null, step: number=1): number[] {
const startArray = stop === undefined ? 0 : start; const startArray = stop === null ? 0 : start;
const stopArray = stop === undefined ? start : stop; const stopArray = stop === null ? start : stop;
return Array.from({ length: (stopArray - startArray) / step + 1}, (_, i) => startArray + (i * step)); return Array.from({ length: (stopArray - startArray) / step + 1}, (_, i) => startArray + (i * step));
} }