UNPKG

1.17 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 */
9
10'use strict';
11
12/**
13 * Escape and wrap key so it is safe to use as a reactid
14 *
15 * @param {string} key to be escaped.
16 * @return {string} the escaped key.
17 */
18
19function escape(key) {
20 var escapeRegex = /[=:]/g;
21 var escaperLookup = {
22 '=': '=0',
23 ':': '=2'
24 };
25 var escapedString = ('' + key).replace(escapeRegex, function (match) {
26 return escaperLookup[match];
27 });
28
29 return '$' + escapedString;
30}
31
32/**
33 * Unescape and unwrap key for human-readable display
34 *
35 * @param {string} key to unescape.
36 * @return {string} the unescaped key.
37 */
38function unescape(key) {
39 var unescapeRegex = /(=0|=2)/g;
40 var unescaperLookup = {
41 '=0': '=',
42 '=2': ':'
43 };
44 var keySubstring = key[0] === '.' && key[1] === '$' ? key.substring(2) : key.substring(1);
45
46 return ('' + keySubstring).replace(unescapeRegex, function (match) {
47 return unescaperLookup[match];
48 });
49}
50
51var KeyEscapeUtils = {
52 escape: escape,
53 unescape: unescape
54};
55
56module.exports = KeyEscapeUtils;
\No newline at end of file