second
[josuexyz/.git] / node_modules / ejs / lib / utils.js
1 /*
2  * EJS Embedded JavaScript templates
3  * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */
18
19 /**
20  * Private utility functions
21  * @module utils
22  * @private
23  */
24
25 'use strict';
26
27 var regExpChars = /[|\\{}()[\]^$+*?.]/g;
28
29 /**
30  * Escape characters reserved in regular expressions.
31  *
32  * If `string` is `undefined` or `null`, the empty string is returned.
33  *
34  * @param {String} string Input string
35  * @return {String} Escaped string
36  * @static
37  * @private
38  */
39 exports.escapeRegExpChars = function (string) {
40   // istanbul ignore if
41   if (!string) {
42     return '';
43   }
44   return String(string).replace(regExpChars, '\\$&');
45 };
46
47 var _ENCODE_HTML_RULES = {
48   '&': '&',
49   '<': '&lt;',
50   '>': '&gt;',
51   '"': '&#34;',
52   "'": '&#39;'
53 };
54 var _MATCH_HTML = /[&<>'"]/g;
55
56 function encode_char(c) {
57   return _ENCODE_HTML_RULES[c] || c;
58 }
59
60 /**
61  * Stringified version of constants used by {@link module:utils.escapeXML}.
62  *
63  * It is used in the process of generating {@link ClientFunction}s.
64  *
65  * @readonly
66  * @type {String}
67  */
68
69 var escapeFuncStr =
70   'var _ENCODE_HTML_RULES = {\n'
71 + '      "&": "&amp;"\n'
72 + '    , "<": "&lt;"\n'
73 + '    , ">": "&gt;"\n'
74 + '    , \'"\': "&#34;"\n'
75 + '    , "\'": "&#39;"\n'
76 + '    }\n'
77 + '  , _MATCH_HTML = /[&<>\'"]/g;\n'
78 + 'function encode_char(c) {\n'
79 + '  return _ENCODE_HTML_RULES[c] || c;\n'
80 + '};\n';
81
82 /**
83  * Escape characters reserved in XML.
84  *
85  * If `markup` is `undefined` or `null`, the empty string is returned.
86  *
87  * @implements {EscapeCallback}
88  * @param {String} markup Input string
89  * @return {String} Escaped string
90  * @static
91  * @private
92  */
93
94 exports.escapeXML = function (markup) {
95   return markup == undefined
96     ? ''
97     : String(markup)
98       .replace(_MATCH_HTML, encode_char);
99 };
100 exports.escapeXML.toString = function () {
101   return Function.prototype.toString.call(this) + ';\n' + escapeFuncStr;
102 };
103
104 /**
105  * Naive copy of properties from one object to another.
106  * Does not recurse into non-scalar properties
107  * Does not check to see if the property has a value before copying
108  *
109  * @param  {Object} to   Destination object
110  * @param  {Object} from Source object
111  * @return {Object}      Destination object
112  * @static
113  * @private
114  */
115 exports.shallowCopy = function (to, from) {
116   from = from || {};
117   for (var p in from) {
118     to[p] = from[p];
119   }
120   return to;
121 };
122
123 /**
124  * Naive copy of a list of key names, from one object to another.
125  * Only copies property if it is actually defined
126  * Does not recurse into non-scalar properties
127  *
128  * @param  {Object} to   Destination object
129  * @param  {Object} from Source object
130  * @param  {Array} list List of properties to copy
131  * @return {Object}      Destination object
132  * @static
133  * @private
134  */
135 exports.shallowCopyFromList = function (to, from, list) {
136   for (var i = 0; i < list.length; i++) {
137     var p = list[i];
138     if (typeof from[p] != 'undefined') {
139       to[p] = from[p];
140     }
141   }
142   return to;
143 };
144
145 /**
146  * Simple in-process cache implementation. Does not implement limits of any
147  * sort.
148  *
149  * @implements Cache
150  * @static
151  * @private
152  */
153 exports.cache = {
154   _data: {},
155   set: function (key, val) {
156     this._data[key] = val;
157   },
158   get: function (key) {
159     return this._data[key];
160   },
161   remove: function (key) {
162     delete this._data[key];
163   },
164   reset: function () {
165     this._data = {};
166   }
167 };