Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Object.prototype.toSafeProxy = function() {
- const getter = (target, name, receiver) => {
- const value = target[name]
- if (value == null) {
- return nullProxy(value)
- } else if (typeof value === 'object') {
- return value.toSafeProxy()
- } else if (typeof value === 'function') {
- return value
- } else {
- return primitiveProxy(value)
- }
- }
- const nullProxy = (value) => {
- return new Proxy({
- value: () => value,
- fallbackTo: fallback => fallback,
- flatMap: () => value
- }, { get: getter })
- }
- const primitiveProxy = (value) => {
- return new Proxy({
- value: () => value,
- fallbackTo: () => value,
- flatMap: map => map(value)
- }, { get: getter })
- }
- return new Proxy(Object.assign({}, this, {
- fallbackTo: () => this,
- flatMap: map => map(this)
- }), {
- get: getter
- })
- }
Add Comment
Please, Sign In to add comment