Created
November 30, 2018 13:05
-
-
Save mkropat/3be5d0887b9446056c0e190024c7f227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nullableDinero = ({ amount, ...rest }) => { | |
if (amount === null) { | |
return nullDinero; | |
} | |
let realDinero = Dinero({ amount, ...rest }); | |
let wrapper = Object.create(realDinero); // delegate to realDinero by default | |
for (let method of ['add', 'subtract']) { // extend specific methods | |
wrapper[method] = other => other === nullDinero | |
? nullDinero | |
: realDinero[method].call(wrapper, other); | |
} | |
return wrapper; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nullableDinero = ({ amount, ...rest }) => { | |
if (amount === null) { | |
return nullDinero; | |
} | |
let realDinero = Dinero({ amount, ...rest }); | |
let wrappedMethods = ['add', 'subtract']; | |
let passthroughProperties = Object.getOwnPropertyNames(realDinero) | |
.filter(name => !wrappedMethods.includes(name)); | |
let proxy = Object.create( | |
Object.getPrototypeOf(realDinero), | |
passthroughProperties.map(name => Object.getOwnPropertyDescriptor(name)) | |
); | |
for (let method of wrappedMethods) { | |
proxy[method] = other => other === nullDinero | |
? nullDinero | |
: realDinero[method].call(proxy, other); | |
} | |
return proxy; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment