Remove console.warn and extra comments to reduce code noise

This commit is contained in:
Ola Hungerford
2025-02-27 07:28:13 -07:00
parent 8ac7ef0985
commit 720480cbbb

View File

@@ -16,20 +16,16 @@ export function updateValueAtPath(
): JsonValue { ): JsonValue {
if (path.length === 0) return value; if (path.length === 0) return value;
// Initialize if null/undefined
if (obj === null || obj === undefined) { if (obj === null || obj === undefined) {
obj = !isNaN(Number(path[0])) ? [] : {}; obj = !isNaN(Number(path[0])) ? [] : {};
} }
// Handle arrays
if (Array.isArray(obj)) { if (Array.isArray(obj)) {
return updateArray(obj, path, value); return updateArray(obj, path, value);
} }
// Handle objects
else if (typeof obj === "object" && obj !== null) { else if (typeof obj === "object" && obj !== null) {
return updateObject(obj as JsonObject, path, value); return updateObject(obj as JsonObject, path, value);
} }
// Cannot update primitives
else { else {
console.error( console.error(
`Cannot update path ${path.join(".")} in non-object/array value:`, `Cannot update path ${path.join(".")} in non-object/array value:`,
@@ -50,27 +46,22 @@ function updateArray(
const [index, ...restPath] = path; const [index, ...restPath] = path;
const arrayIndex = Number(index); const arrayIndex = Number(index);
// Validate array index
if (isNaN(arrayIndex)) { if (isNaN(arrayIndex)) {
console.error(`Invalid array index: ${index}`); console.error(`Invalid array index: ${index}`);
return array; return array;
} }
// Check array bounds
if (arrayIndex < 0) { if (arrayIndex < 0) {
console.error(`Array index out of bounds: ${arrayIndex} < 0`); console.error(`Array index out of bounds: ${arrayIndex} < 0`);
return array; return array;
} }
// Create a dense copy of the array, filling holes with null
let newArray: JsonValue[] = []; let newArray: JsonValue[] = [];
for (let i = 0; i < array.length; i++) { for (let i = 0; i < array.length; i++) {
newArray[i] = i in array ? array[i] : null; newArray[i] = i in array ? array[i] : null;
} }
// If the desired index is out of bounds, build a new array explicitly filled with nulls
if (arrayIndex >= newArray.length) { if (arrayIndex >= newArray.length) {
console.warn(`Extending array to index ${arrayIndex}`);
const extendedArray: JsonValue[] = new Array(arrayIndex).fill(null); const extendedArray: JsonValue[] = new Array(arrayIndex).fill(null);
// Copy over the existing elements (now guaranteed to be dense) // Copy over the existing elements (now guaranteed to be dense)
for (let i = 0; i < newArray.length; i++) { for (let i = 0; i < newArray.length; i++) {