Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed proxiTest.js
Empty file.
60 changes: 60 additions & 0 deletions proxiTest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
let handler = {
get: (target, property) => {
return target[property];
},
set: (target, property, val) => {
if (typeof val == 'object') {
target[property] = makeItemAProxy(val);
} else {
target[property] = val;
}
return true;
}
}

export function makeItemAProxy(object) {
object = makeLoggable(object);
return new Proxy(object, handler);

}

function makeLoggable(object) {
if (object instanceof Array) {
for (let element of object) {
let index = 0;
if (typeof element == 'object') {
object[index] = generateProxy(element);
index++;
} else {
loggableObj.push(element);
}
}
} else {
for (const key of Object.keys(object)) {
if (object[key] instanceof Object) {
object[key] = generateProxy(object[key])
} else {
object[key] = object[key];
}
}
}
return object;
}

function generateProxy(value) {
if (hasObject(value)) {
return new Proxy(makeLoggable(value), handler);
} else {
return new Proxy(value, handler);
}
}

function hasObject(object) {
let hasObject = false;
for (const key of Object.keys(object)) {
if (typeof object[key] == 'object') {
hasObject = true;
}
}
return hasObject;
}
65 changes: 65 additions & 0 deletions testCases.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { makeItemAProxy } from "./proxiTest.mjs";

let mockObj = {
"username": "Fis Geci",
"pasword": "*****",
"roles": ["LEGEND"],
"posts": [{ "post_id": 5, "content": "Hello" }],
"account": {
"setting": "blahblah"
}
}

let clear = {
"username": "Fis Geci",
"pasword": "*****",
"roles": ["LEGEND"],
"posts": [{ "post_id": 5, "content": "Hello" }],
"account": {
"setting": "blahblah"
}
}

function shouldTurnTheRootIntoProxy() {
let proxyObj = makeItemAProxy(mockObj);
proxyObj.username = "fis";

console.assert(proxyObj.username === mockObj.username, "Object is a proxy")
mockObj = clear;
}

function shouldTurnNestedObjectIntoProxies() {
let proxy = makeItemAProxy(mockObj)

proxy.account.setting = "New Setting";

console.assert(proxy.account.setting === mockObj.account.setting, "Object is a proxy");

mockObj = clear;
}

function shouldTurnNestedArrayIntoProxies() {
let proxy = makeItemAProxy(mockObj)

proxy.roles[1] = "New Setting";

console.assert(proxy.roles[1] === mockObj.roles[1], "Array is a proxy")

mockObj = clear;
}

function shouldTurnNestedObjectsInArraysIntoProxies() {
let proxy = makeItemAProxy(mockObj)

proxy.posts[0].content = "New Content";

console.assert(proxy.posts[0].content == "New Content", "Value Inside proxy has changed")
console.assert(proxy.posts[0].content === mockObj.posts[0].content, "Object inside array is a proxy")

mockObj = clear;
}

shouldTurnTheRootIntoProxy();
shouldTurnNestedObjectIntoProxies();
shouldTurnNestedArrayIntoProxies();
shouldTurnNestedObjectsInArraysIntoProxies();
Loading