Skip to content

Instantly share code, notes, and snippets.

@ilopX
Created November 10, 2019 08:17
Show Gist options
  • Save ilopX/83db282ddbab748375dc04d819236d0e to your computer and use it in GitHub Desktop.
Save ilopX/83db282ddbab748375dc04d819236d0e to your computer and use it in GitHub Desktop.
const box = (obj) => {
return {
get isBox() {
return true
},
get prop() {
return obj
},
unbox() {
let o = obj
while(o.isBox) {
o = o.prop
}
return o
}
}
}
const box1 = box({ a: 10 })
const box2 = box(box1)
const box3 = box(box2)
assert(box1.prop).toBe({ a: 10 });
assert(box2.prop.prop).toBe({ a: 10 });
assert(box3.prop.prop.prop).toBe({ a: 10 });
assert(box1.unbox()).toBe({ a: 10 });
assert(box2.unbox()).toBe({ a: 10 });
assert(box3.unbox()).toBe({ a: 10 });
const box4 = box('Nu privet!')
const box5 = box(box4)
assert(box4.prop).toBe('Nu privet!');
assert(box5.prop.prop).toBe('Nu privet!');
assert(box4.unbox()).toBe('Nu privet!');
assert(box5.unbox()).toBe('Nu privet!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment