Created
July 19, 2017 08:49
-
-
Save matthieuprat/a6cee57e4a350514224ff041780ea82d to your computer and use it in GitHub Desktop.
Getting read of Immutable.js smoothly
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
/* eslint-disable no-extend-native */ | |
// Cf https://facebook.github.io/immutable-js/docs/#/Map. | |
function notifySentry(method) { | |
const error = new Error(`Warning: '${method}' was called on an Object`) | |
Raven.captureException(error, { level: 'warning', tags: { objectUnknownMethod: method } }) | |
} | |
const DEFAULTS = { | |
configurable: true, | |
enumerable: false, | |
writable: true, | |
} | |
Object.defineProperty(Object.prototype, 'length', { | |
...DEFAULTS, | |
get() { | |
notifySentry('length') | |
return Object.keys(this).length | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'map', { | |
...DEFAULTS, | |
value: function map(mapper, context) { | |
notifySentry('map') | |
return Object.entries(this).reduce( | |
(result, [key, value]) => Object.assign(result, { [key]: mapper.call(context, value, key, this) }), | |
{}, | |
) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'reduce', { | |
...DEFAULTS, | |
value: function reduce(reducer, initialReduction, context) { | |
notifySentry('reduce') | |
return Object.entries(this).reduce( | |
(reduction, [key, value]) => reducer.call(context, reduction, value, key, this), | |
initialReduction, | |
) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'filter', { | |
...DEFAULTS, | |
value: function filter(predicate, context) { | |
notifySentry('filter') | |
return Object.entries(this).reduce((result, [key, value]) => { | |
if (predicate.call(context, value, key, this)) { | |
result[key] = value | |
} | |
return result | |
}, {}) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'sort', { | |
...DEFAULTS, | |
value: function sort(comparator) { | |
notifySentry('sort') | |
return Object.values(this).sort(comparator) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'reverse', { | |
...DEFAULTS, | |
value: function reverse() { | |
notifySentry('reverse') | |
return Object.values(this) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'includes', { | |
...DEFAULTS, | |
value: function includes(value) { | |
notifySentry('includes') | |
return Object.values(this).includes(value) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'join', { | |
...DEFAULTS, | |
value: function join(value) { | |
notifySentry('join') | |
return Object.values(this).join(value) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'some', { | |
...DEFAULTS, | |
value: function some(predicate, context) { | |
notifySentry('some') | |
return Object.entries(this).some(([key, value]) => predicate.call(context, value, key, this)) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'every', { | |
...DEFAULTS, | |
value: function every(predicate, context) { | |
notifySentry('every') | |
return Object.entries(this).every(([key, value]) => predicate.call(context, value, key, this)) | |
}, | |
}) | |
Object.defineProperty(Object.prototype, 'find', { | |
...DEFAULTS, | |
value: function find(predicate, context, notSetValue) { | |
notifySentry('find') | |
return Object.entries(this).find(([key, value]) => predicate.call(context, value, key, this)) || notSetValue | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment