|
| 1 | +// import {describe, it} from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import {jest} from '@jest/globals'; |
| 4 | +import util from 'node:util'; |
| 5 | +import {style} from './styles.js'; |
| 6 | +util.inspect.defaultOptions.depth = null; // show full objects |
| 7 | +// util.inspect.defaultOptions.depth = 0; // show truncated objects |
| 8 | +// util.inspect.defaultOptions.compact = true; // dont break objects to new lines |
| 9 | +// util.inspect.defaultOptions.compact = false; // break objects to new lines |
| 10 | + |
| 11 | +// suppress jests tracing console logs |
| 12 | +import console from 'console'; |
| 13 | +const jestConsole = console; |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + global.console = console; |
| 17 | + console.log(style.color(255,0,255),'▷',style.reset,style.color(39),expect.getState().currentTestName,style.reset,'\n'); }); |
| 18 | + |
| 19 | +afterEach(() => { |
| 20 | + global.console = jestConsole; |
| 21 | + console.log(style.color(99), style.hr.double, style.reset); |
| 22 | +}); |
| 23 | + |
| 24 | +describe('shallowVSDeepCopy', () => { |
| 25 | + it('Shallow-Copy VS Deep-Copy concept in JS', () => { |
| 26 | + |
| 27 | + const originalObject = { |
| 28 | + somePrimitive: 3, |
| 29 | + someReference: { |
| 30 | + someNestedPrim: 7, |
| 31 | + someNestedRef: ["a", "b"] |
| 32 | + }, |
| 33 | + anotherPrimitive: "hello" |
| 34 | + }; |
| 35 | + |
| 36 | + // Create a shallow copy using the spread syntax |
| 37 | + const shallowCopy = { ...originalObject }; |
| 38 | + |
| 39 | + console.log(style.bold,style.underline,style.blue,"--- Initial State ---",style.reset); |
| 40 | + console.log("Original Object:", originalObject); |
| 41 | + console.log("Shallow Copy:", shallowCopy, "\n"); |
| 42 | + |
| 43 | + console.log(style.bold,style.underline,style.blue,"--- Modifying the shallow copy ---",style.reset); |
| 44 | + |
| 45 | + // 1. Changing a root-level primitive property in the shallow copy |
| 46 | + console.log(style.red,"1. Changing a root-level primitive property in the shallow copy", style.reset); |
| 47 | + |
| 48 | + shallowCopy.somePrimitive = 10; |
| 49 | + console.log("shallowCopy.somePrimitive changed to:", shallowCopy.somePrimitive); |
| 50 | + console.log("Original Object's somePrimitive:", originalObject.somePrimitive, style.italic, style.magenta, "// Original unaffected\n", style.reset); // Original unaffected |
| 51 | + |
| 52 | + // 2. Changing a root-level primitive property that was added to illustrate the point |
| 53 | + console.log(style.red,"2. Changing a root-level primitive property that was added to illustrate the point", style.reset); |
| 54 | + |
| 55 | + shallowCopy.anotherPrimitive = "world"; |
| 56 | + console.log("shallowCopy.anotherPrimitive changed to:", shallowCopy.anotherPrimitive); |
| 57 | + console.log("Original Object's anotherPrimitive:", originalObject.anotherPrimitive, style.italic, style.magenta, "// Original unaffected\n", style.reset); // Original unaffected |
| 58 | + |
| 59 | + |
| 60 | + // 3. Modifying a property *within* a nested object in the shallow copy |
| 61 | + console.log(style.red,"3. Modifying a property *within* a nested object in the shallow copy", style.reset); |
| 62 | + |
| 63 | + shallowCopy.someReference.someNestedPrim = 15; |
| 64 | + console.log("shallowCopy.someReference.someNestedPrim changed to:", shallowCopy.someReference.someNestedPrim); |
| 65 | + console.log("Original Object's someReference.someNestedPrim:", originalObject.someReference.someNestedPrim, style.italic, style.magenta, "// Original AFFECTED\n", style.reset); // Original AFFECTED |
| 66 | + |
| 67 | + |
| 68 | + // 4. Modifying a nested array in the shallow copy |
| 69 | + console.log(style.red,"4. Modifying a nested array in the shallow copy", style.reset); |
| 70 | + |
| 71 | + shallowCopy.someReference.someNestedRef.push("c"); |
| 72 | + console.log("shallowCopy.someReference.someNestedRef after push:", shallowCopy.someReference.someNestedRef); |
| 73 | + console.log("Original Object's someReference.someNestedRef after push:", originalObject.someReference.someNestedRef, style.italic, style.magenta, "// Original AFFECTED\n", style.reset); // Original AFFECTED |
| 74 | + |
| 75 | + // 5. Reassigning a root-level reference property in the shallow copy |
| 76 | + console.log(style.red,"5. Reassigning a root-level reference property in the shallow copy", style.reset); |
| 77 | + |
| 78 | + shallowCopy.someReference = { newProperty: true }; |
| 79 | + console.log("shallowCopy.someReference reassigned to:", shallowCopy.someReference); |
| 80 | + console.log("Original Object's someReference:", originalObject.someReference, style.italic, style.magenta, "// Original still points to the old object", style.reset); // Original still points to the old object |
| 81 | + }); |
| 82 | + |
| 83 | +}); |
| 84 | + |
| 85 | + |
0 commit comments