Summary:
Don't want anyone accidentally mutating it.

Also make deepFreezeAndThrowOnMutationInDev easier to use with nice flow typing.

Reviewed By: yungsters

Differential Revision: D6974089

fbshipit-source-id: 0f90e7939cb726893fa353a4f2a6bbba701205bc
This commit is contained in:
Spencer Ahrens 2018-02-13 14:04:49 -08:00 коммит произвёл Facebook Github Bot
Родитель 74e54cbcc4
Коммит d220118dbd
2 изменённых файлов: 7 добавлений и 4 удалений

Просмотреть файл

@ -13,6 +13,8 @@
'use strict'; 'use strict';
const deepFreezeAndThrowOnMutationInDev = require('deepFreezeAndThrowOnMutationInDev');
/** /**
* A collection of Unicode sequences for various characters and emoji. * A collection of Unicode sequences for various characters and emoji.
* *
@ -20,7 +22,7 @@
* - Source code should be limitted to ASCII. * - Source code should be limitted to ASCII.
* - Less chance of typos. * - Less chance of typos.
*/ */
const UTFSequence = { const UTFSequence = deepFreezeAndThrowOnMutationInDev({
MIDDOT: '\u00B7', // normal middle dot: · MIDDOT: '\u00B7', // normal middle dot: ·
MIDDOT_SP: '\u00A0\u00B7\u00A0', //  ·  MIDDOT_SP: '\u00A0\u00B7\u00A0', //  · 
MIDDOT_KATAKANA: '\u30FB', // katakana middle dot MIDDOT_KATAKANA: '\u30FB', // katakana middle dot
@ -30,6 +32,6 @@ const UTFSequence = {
NDASH_SP: '\u00A0\u2013\u00A0', //  –  NDASH_SP: '\u00A0\u2013\u00A0', //  – 
NBSP: '\u00A0', // non-breaking space:   NBSP: '\u00A0', // non-breaking space:  
PIZZA: '\uD83C\uDF55', PIZZA: '\uD83C\uDF55',
}; });
module.exports = UTFSequence; module.exports = UTFSequence;

Просмотреть файл

@ -29,13 +29,13 @@
* Freezing the object and adding the throw mechanism is expensive and will * Freezing the object and adding the throw mechanism is expensive and will
* only be used in DEV. * only be used in DEV.
*/ */
function deepFreezeAndThrowOnMutationInDev(object: Object) { function deepFreezeAndThrowOnMutationInDev<T: Object>(object: T): T {
if (__DEV__) { if (__DEV__) {
if (typeof object !== 'object' || if (typeof object !== 'object' ||
object === null || object === null ||
Object.isFrozen(object) || Object.isFrozen(object) ||
Object.isSealed(object)) { Object.isSealed(object)) {
return; return object;
} }
var keys = Object.keys(object); var keys = Object.keys(object);
@ -58,6 +58,7 @@ function deepFreezeAndThrowOnMutationInDev(object: Object) {
} }
} }
} }
return object;
} }
function throwOnImmutableMutation(key, value) { function throwOnImmutableMutation(key, value) {