Files
ejs/lib/utils.js
Timothy Gu 319cac097c Add JSDoc annotation
Some minor changes regarding `var compile` is needed to make JSDoc
recognize the function.
2015-01-31 21:36:29 -08:00

85 lines
2.0 KiB
JavaScript

/*
* EJS Embedded JavaScript templates
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* Private utility functions
* @module utils
* @private
*/
'use strict';
var regExpChars = /[|\\{}()[\]^$+*?.]/g;
/**
* Escape characters reserved in regular expressions.
*
* If `string` is `undefined` or `null`, the empty string is returned.
*
* @param {String} string Input string
* @return {String} Escaped string
* @static
*/
exports.escapeRegExpChars = function (string) {
// istanbul ignore if
if (!string) {
return '';
}
return String(string).replace(regExpChars, '\\$&');
};
/**
* Escape characters reserved in XML.
*
* If `markup` is `undefined` or `null`, the empty string is returned.
*
* @param {String} markup Input string
* @return {String} Escaped string
* @static
*/
exports.escapeXML = function (markup) {
// Handle stupid JS `undefined` and `null`
// Yes, we want double-equal here to catch both
// jshint eqnull: true
if (markup == null) {
return '';
}
return String(markup)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/'/g, '&#39;')
.replace(/"/g, '&quot;');
};
/**
* Copy all properties from one object to another, in a shallow fashion.
*
* @param {Object} to Destination object
* @param {Object} from Source object
* @return {Object} Destination object
* @private
* @static
*/
exports.shallowCopy = function (to, from) {
for (var p in from) {
to[p] = from[p];
}
return to;
};