-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helper for escaping and unescpaing component keys (#6500)
- Abstract escaping - Provide human readible same key warnings (cherry picked from commit dc6fc8c)
- Loading branch information
Showing
5 changed files
with
109 additions
and
39 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule KeyEscapeUtils | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Escape and wrap key so it is safe to use as a reactid | ||
* | ||
* @param {*} key to be escaped. | ||
* @return {string} the escaped key. | ||
*/ | ||
function escape(key) { | ||
var escapeRegex = /[=:]/g; | ||
var escaperLookup = { | ||
'=': '=0', | ||
':': '=2', | ||
}; | ||
var escapedString = ('' + key).replace( | ||
escapeRegex, | ||
function(match) { | ||
return escaperLookup[match]; | ||
} | ||
); | ||
|
||
return '$' + escapedString; | ||
} | ||
|
||
/** | ||
* Unescape and unwrap key for human-readable display | ||
* | ||
* @param {string} key to unescape. | ||
* @return {string} the unescaped key. | ||
*/ | ||
function unescape(key) { | ||
var unescapeRegex = /(=0|=2)/g; | ||
var unescaperLookup = { | ||
'=0': '=', | ||
'=2': ':', | ||
}; | ||
var keySubstring = (key[0] === '.' && key[1] === '$') | ||
? key.substring(2) : key.substring(1); | ||
|
||
return ('' + keySubstring).replace( | ||
unescapeRegex, | ||
function(match) { | ||
return unescaperLookup[match]; | ||
} | ||
); | ||
} | ||
|
||
var KeyEscapeUtils = { | ||
escape: escape, | ||
unescape: unescape, | ||
}; | ||
|
||
module.exports = KeyEscapeUtils; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var KeyEscapeUtils; | ||
|
||
describe('KeyEscapeUtils', () => { | ||
beforeEach(() => { | ||
jest.resetModuleRegistry(); | ||
|
||
KeyEscapeUtils = require('KeyEscapeUtils'); | ||
}); | ||
|
||
describe('escape', () => { | ||
it('should properly escape and wrap user defined keys', () => { | ||
expect(KeyEscapeUtils.escape('1')).toBe('$1'); | ||
expect(KeyEscapeUtils.escape('1=::=2')).toBe('$1=0=2=2=02'); | ||
}); | ||
}); | ||
|
||
describe('unescape', () => { | ||
it('should properly unescape and unwrap user defined keys', () => { | ||
expect(KeyEscapeUtils.unescape('.1')).toBe('1'); | ||
expect(KeyEscapeUtils.unescape('$1')).toBe('1'); | ||
expect(KeyEscapeUtils.unescape('.$1')).toBe('1'); | ||
expect(KeyEscapeUtils.unescape('$1=0=2=2=02')).toBe('1=::=2'); | ||
}); | ||
}); | ||
}); |
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
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