Initial commit
[yaffs-website] / node_modules / lodash.escape / index.js
1 /**
2  * lodash 3.2.0 (Custom Build) <https://lodash.com/>
3  * Build: `lodash modularize exports="npm" -o ./`
4  * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
5  * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
6  * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7  * Available under MIT license <https://lodash.com/license>
8  */
9 var root = require('lodash._root');
10
11 /** Used as references for various `Number` constants. */
12 var INFINITY = 1 / 0;
13
14 /** `Object#toString` result references. */
15 var symbolTag = '[object Symbol]';
16
17 /** Used to match HTML entities and HTML characters. */
18 var reUnescapedHtml = /[&<>"'`]/g,
19     reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
20
21 /** Used to map characters to HTML entities. */
22 var htmlEscapes = {
23   '&': '&amp;',
24   '<': '&lt;',
25   '>': '&gt;',
26   '"': '&quot;',
27   "'": '&#39;',
28   '`': '&#96;'
29 };
30
31 /**
32  * Used by `_.escape` to convert characters to HTML entities.
33  *
34  * @private
35  * @param {string} chr The matched character to escape.
36  * @returns {string} Returns the escaped character.
37  */
38 function escapeHtmlChar(chr) {
39   return htmlEscapes[chr];
40 }
41
42 /** Used for built-in method references. */
43 var objectProto = Object.prototype;
44
45 /**
46  * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
47  * of values.
48  */
49 var objectToString = objectProto.toString;
50
51 /** Built-in value references. */
52 var Symbol = root.Symbol;
53
54 /** Used to convert symbols to primitives and strings. */
55 var symbolProto = Symbol ? Symbol.prototype : undefined,
56     symbolToString = Symbol ? symbolProto.toString : undefined;
57
58 /**
59  * Checks if `value` is object-like. A value is object-like if it's not `null`
60  * and has a `typeof` result of "object".
61  *
62  * @static
63  * @memberOf _
64  * @category Lang
65  * @param {*} value The value to check.
66  * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
67  * @example
68  *
69  * _.isObjectLike({});
70  * // => true
71  *
72  * _.isObjectLike([1, 2, 3]);
73  * // => true
74  *
75  * _.isObjectLike(_.noop);
76  * // => false
77  *
78  * _.isObjectLike(null);
79  * // => false
80  */
81 function isObjectLike(value) {
82   return !!value && typeof value == 'object';
83 }
84
85 /**
86  * Checks if `value` is classified as a `Symbol` primitive or object.
87  *
88  * @static
89  * @memberOf _
90  * @category Lang
91  * @param {*} value The value to check.
92  * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
93  * @example
94  *
95  * _.isSymbol(Symbol.iterator);
96  * // => true
97  *
98  * _.isSymbol('abc');
99  * // => false
100  */
101 function isSymbol(value) {
102   return typeof value == 'symbol' ||
103     (isObjectLike(value) && objectToString.call(value) == symbolTag);
104 }
105
106 /**
107  * Converts `value` to a string if it's not one. An empty string is returned
108  * for `null` and `undefined` values. The sign of `-0` is preserved.
109  *
110  * @static
111  * @memberOf _
112  * @category Lang
113  * @param {*} value The value to process.
114  * @returns {string} Returns the string.
115  * @example
116  *
117  * _.toString(null);
118  * // => ''
119  *
120  * _.toString(-0);
121  * // => '-0'
122  *
123  * _.toString([1, 2, 3]);
124  * // => '1,2,3'
125  */
126 function toString(value) {
127   // Exit early for strings to avoid a performance hit in some environments.
128   if (typeof value == 'string') {
129     return value;
130   }
131   if (value == null) {
132     return '';
133   }
134   if (isSymbol(value)) {
135     return Symbol ? symbolToString.call(value) : '';
136   }
137   var result = (value + '');
138   return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
139 }
140
141 /**
142  * Converts the characters "&", "<", ">", '"', "'", and "\`" in `string` to
143  * their corresponding HTML entities.
144  *
145  * **Note:** No other characters are escaped. To escape additional
146  * characters use a third-party library like [_he_](https://mths.be/he).
147  *
148  * Though the ">" character is escaped for symmetry, characters like
149  * ">" and "/" don't need escaping in HTML and have no special meaning
150  * unless they're part of a tag or unquoted attribute value.
151  * See [Mathias Bynens's article](https://mathiasbynens.be/notes/ambiguous-ampersands)
152  * (under "semi-related fun fact") for more details.
153  *
154  * Backticks are escaped because in IE < 9, they can break out of
155  * attribute values or HTML comments. See [#59](https://html5sec.org/#59),
156  * [#102](https://html5sec.org/#102), [#108](https://html5sec.org/#108), and
157  * [#133](https://html5sec.org/#133) of the [HTML5 Security Cheatsheet](https://html5sec.org/)
158  * for more details.
159  *
160  * When working with HTML you should always [quote attribute values](http://wonko.com/post/html-escaping)
161  * to reduce XSS vectors.
162  *
163  * @static
164  * @memberOf _
165  * @category String
166  * @param {string} [string=''] The string to escape.
167  * @returns {string} Returns the escaped string.
168  * @example
169  *
170  * _.escape('fred, barney, & pebbles');
171  * // => 'fred, barney, &amp; pebbles'
172  */
173 function escape(string) {
174   string = toString(string);
175   return (string && reHasUnescapedHtml.test(string))
176     ? string.replace(reUnescapedHtml, escapeHtmlChar)
177     : string;
178 }
179
180 module.exports = escape;