UNPKG

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