Skip to content

Commit

Permalink
Freeze UTFSequence
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sahrens authored and facebook-github-bot committed Feb 13, 2018
1 parent 74e54cb commit d220118
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Libraries/UTFSequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@

'use strict';

const deepFreezeAndThrowOnMutationInDev = require('deepFreezeAndThrowOnMutationInDev');

/**
* A collection of Unicode sequences for various characters and emoji.
*
* - More explicit than using the sequences directly in code.
* - Source code should be limitted to ASCII.
* - Less chance of typos.
*/
const UTFSequence = {
const UTFSequence = deepFreezeAndThrowOnMutationInDev({
MIDDOT: '\u00B7', // normal middle dot: ·
MIDDOT_SP: '\u00A0\u00B7\u00A0', //  · 
MIDDOT_KATAKANA: '\u30FB', // katakana middle dot
Expand All @@ -30,6 +32,6 @@ const UTFSequence = {
NDASH_SP: '\u00A0\u2013\u00A0', //  – 
NBSP: '\u00A0', // non-breaking space:  
PIZZA: '\uD83C\uDF55',
};
});

module.exports = UTFSequence;
5 changes: 3 additions & 2 deletions Libraries/Utilities/deepFreezeAndThrowOnMutationInDev.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
* Freezing the object and adding the throw mechanism is expensive and will
* only be used in DEV.
*/
function deepFreezeAndThrowOnMutationInDev(object: Object) {
function deepFreezeAndThrowOnMutationInDev<T: Object>(object: T): T {
if (__DEV__) {
if (typeof object !== 'object' ||
object === null ||
Object.isFrozen(object) ||
Object.isSealed(object)) {
return;
return object;
}

var keys = Object.keys(object);
Expand All @@ -58,6 +58,7 @@ function deepFreezeAndThrowOnMutationInDev(object: Object) {
}
}
}
return object;
}

function throwOnImmutableMutation(key, value) {
Expand Down

0 comments on commit d220118

Please sign in to comment.