1 | /*!
|
2 | * preserve <https://github.com/jonschlinkert/preserve>
|
3 | *
|
4 | * Copyright (c) 2014-2015, Jon Schlinkert.
|
5 | * Licensed under the MIT license.
|
6 | */
|
7 |
|
8 | ;
|
9 |
|
10 | /**
|
11 | * Replace tokens in `str` with a temporary, heuristic placeholder.
|
12 | *
|
13 | * ```js
|
14 | * tokens.before('{a\\,b}');
|
15 | * //=> '{__ID1__}'
|
16 | * ```
|
17 | *
|
18 | * @param {String} `str`
|
19 | * @return {String} String with placeholders.
|
20 | * @api public
|
21 | */
|
22 |
|
23 | exports.before = function before(str, re) {
|
24 | return str.replace(re, function (match) {
|
25 | var id = randomize();
|
26 | cache[id] = match;
|
27 | return '__ID' + id + '__';
|
28 | });
|
29 | };
|
30 |
|
31 | /**
|
32 | * Replace placeholders in `str` with original tokens.
|
33 | *
|
34 | * ```js
|
35 | * tokens.after('{__ID1__}');
|
36 | * //=> '{a\\,b}'
|
37 | * ```
|
38 | *
|
39 | * @param {String} `str` String with placeholders
|
40 | * @return {String} `str` String with original tokens.
|
41 | * @api public
|
42 | */
|
43 |
|
44 | exports.after = function after(str) {
|
45 | return str.replace(/__ID(.{5})__/g, function (_, id) {
|
46 | return cache[id];
|
47 | });
|
48 | };
|
49 |
|
50 | function randomize() {
|
51 | return Math.random().toString().slice(2, 7);
|
52 | }
|
53 |
|
54 | var cache = {}; |
\ | No newline at end of file |