index.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. /**
  2. * Lodash (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
  5. * Released under MIT license <https://lodash.com/license>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. */
  9. var reInterpolate = require('lodash._reinterpolate'),
  10. templateSettings = require('lodash.templatesettings');
  11. /** Used to detect hot functions by number of calls within a span of milliseconds. */
  12. var HOT_COUNT = 800,
  13. HOT_SPAN = 16;
  14. /** Used as references for various `Number` constants. */
  15. var INFINITY = 1 / 0,
  16. MAX_SAFE_INTEGER = 9007199254740991;
  17. /** `Object#toString` result references. */
  18. var argsTag = '[object Arguments]',
  19. arrayTag = '[object Array]',
  20. asyncTag = '[object AsyncFunction]',
  21. boolTag = '[object Boolean]',
  22. dateTag = '[object Date]',
  23. domExcTag = '[object DOMException]',
  24. errorTag = '[object Error]',
  25. funcTag = '[object Function]',
  26. genTag = '[object GeneratorFunction]',
  27. mapTag = '[object Map]',
  28. numberTag = '[object Number]',
  29. nullTag = '[object Null]',
  30. objectTag = '[object Object]',
  31. proxyTag = '[object Proxy]',
  32. regexpTag = '[object RegExp]',
  33. setTag = '[object Set]',
  34. stringTag = '[object String]',
  35. symbolTag = '[object Symbol]',
  36. undefinedTag = '[object Undefined]',
  37. weakMapTag = '[object WeakMap]';
  38. var arrayBufferTag = '[object ArrayBuffer]',
  39. dataViewTag = '[object DataView]',
  40. float32Tag = '[object Float32Array]',
  41. float64Tag = '[object Float64Array]',
  42. int8Tag = '[object Int8Array]',
  43. int16Tag = '[object Int16Array]',
  44. int32Tag = '[object Int32Array]',
  45. uint8Tag = '[object Uint8Array]',
  46. uint8ClampedTag = '[object Uint8ClampedArray]',
  47. uint16Tag = '[object Uint16Array]',
  48. uint32Tag = '[object Uint32Array]';
  49. /** Used to match empty string literals in compiled template source. */
  50. var reEmptyStringLeading = /\b__p \+= '';/g,
  51. reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
  52. reEmptyStringTrailing = /(__e\(.*?\)|\b__t\)) \+\n'';/g;
  53. /**
  54. * Used to match `RegExp`
  55. * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
  56. */
  57. var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
  58. /**
  59. * Used to match
  60. * [ES template delimiters](http://ecma-international.org/ecma-262/7.0/#sec-template-literal-lexical-components).
  61. */
  62. var reEsTemplate = /\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g;
  63. /** Used to detect host constructors (Safari). */
  64. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  65. /** Used to detect unsigned integer values. */
  66. var reIsUint = /^(?:0|[1-9]\d*)$/;
  67. /** Used to ensure capturing order of template delimiters. */
  68. var reNoMatch = /($^)/;
  69. /** Used to match unescaped characters in compiled string literals. */
  70. var reUnescapedString = /['\n\r\u2028\u2029\\]/g;
  71. /** Used to identify `toStringTag` values of typed arrays. */
  72. var typedArrayTags = {};
  73. typedArrayTags[float32Tag] = typedArrayTags[float64Tag] =
  74. typedArrayTags[int8Tag] = typedArrayTags[int16Tag] =
  75. typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =
  76. typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =
  77. typedArrayTags[uint32Tag] = true;
  78. typedArrayTags[argsTag] = typedArrayTags[arrayTag] =
  79. typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =
  80. typedArrayTags[dataViewTag] = typedArrayTags[dateTag] =
  81. typedArrayTags[errorTag] = typedArrayTags[funcTag] =
  82. typedArrayTags[mapTag] = typedArrayTags[numberTag] =
  83. typedArrayTags[objectTag] = typedArrayTags[regexpTag] =
  84. typedArrayTags[setTag] = typedArrayTags[stringTag] =
  85. typedArrayTags[weakMapTag] = false;
  86. /** Used to escape characters for inclusion in compiled string literals. */
  87. var stringEscapes = {
  88. '\\': '\\',
  89. "'": "'",
  90. '\n': 'n',
  91. '\r': 'r',
  92. '\u2028': 'u2028',
  93. '\u2029': 'u2029'
  94. };
  95. /** Detect free variable `global` from Node.js. */
  96. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  97. /** Detect free variable `self`. */
  98. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  99. /** Used as a reference to the global object. */
  100. var root = freeGlobal || freeSelf || Function('return this')();
  101. /** Detect free variable `exports`. */
  102. var freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;
  103. /** Detect free variable `module`. */
  104. var freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;
  105. /** Detect the popular CommonJS extension `module.exports`. */
  106. var moduleExports = freeModule && freeModule.exports === freeExports;
  107. /** Detect free variable `process` from Node.js. */
  108. var freeProcess = moduleExports && freeGlobal.process;
  109. /** Used to access faster Node.js helpers. */
  110. var nodeUtil = (function() {
  111. try {
  112. // Use `util.types` for Node.js 10+.
  113. var types = freeModule && freeModule.require && freeModule.require('util').types;
  114. if (types) {
  115. return types;
  116. }
  117. // Legacy `process.binding('util')` for Node.js < 10.
  118. return freeProcess && freeProcess.binding && freeProcess.binding('util');
  119. } catch (e) {}
  120. }());
  121. /* Node.js helper references. */
  122. var nodeIsTypedArray = nodeUtil && nodeUtil.isTypedArray;
  123. /**
  124. * A faster alternative to `Function#apply`, this function invokes `func`
  125. * with the `this` binding of `thisArg` and the arguments of `args`.
  126. *
  127. * @private
  128. * @param {Function} func The function to invoke.
  129. * @param {*} thisArg The `this` binding of `func`.
  130. * @param {Array} args The arguments to invoke `func` with.
  131. * @returns {*} Returns the result of `func`.
  132. */
  133. function apply(func, thisArg, args) {
  134. switch (args.length) {
  135. case 0: return func.call(thisArg);
  136. case 1: return func.call(thisArg, args[0]);
  137. case 2: return func.call(thisArg, args[0], args[1]);
  138. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  139. }
  140. return func.apply(thisArg, args);
  141. }
  142. /**
  143. * A specialized version of `_.map` for arrays without support for iteratee
  144. * shorthands.
  145. *
  146. * @private
  147. * @param {Array} [array] The array to iterate over.
  148. * @param {Function} iteratee The function invoked per iteration.
  149. * @returns {Array} Returns the new mapped array.
  150. */
  151. function arrayMap(array, iteratee) {
  152. var index = -1,
  153. length = array == null ? 0 : array.length,
  154. result = Array(length);
  155. while (++index < length) {
  156. result[index] = iteratee(array[index], index, array);
  157. }
  158. return result;
  159. }
  160. /**
  161. * The base implementation of `_.times` without support for iteratee shorthands
  162. * or max array length checks.
  163. *
  164. * @private
  165. * @param {number} n The number of times to invoke `iteratee`.
  166. * @param {Function} iteratee The function invoked per iteration.
  167. * @returns {Array} Returns the array of results.
  168. */
  169. function baseTimes(n, iteratee) {
  170. var index = -1,
  171. result = Array(n);
  172. while (++index < n) {
  173. result[index] = iteratee(index);
  174. }
  175. return result;
  176. }
  177. /**
  178. * The base implementation of `_.unary` without support for storing metadata.
  179. *
  180. * @private
  181. * @param {Function} func The function to cap arguments for.
  182. * @returns {Function} Returns the new capped function.
  183. */
  184. function baseUnary(func) {
  185. return function(value) {
  186. return func(value);
  187. };
  188. }
  189. /**
  190. * The base implementation of `_.values` and `_.valuesIn` which creates an
  191. * array of `object` property values corresponding to the property names
  192. * of `props`.
  193. *
  194. * @private
  195. * @param {Object} object The object to query.
  196. * @param {Array} props The property names to get values for.
  197. * @returns {Object} Returns the array of property values.
  198. */
  199. function baseValues(object, props) {
  200. return arrayMap(props, function(key) {
  201. return object[key];
  202. });
  203. }
  204. /**
  205. * Used by `_.template` to escape characters for inclusion in compiled string literals.
  206. *
  207. * @private
  208. * @param {string} chr The matched character to escape.
  209. * @returns {string} Returns the escaped character.
  210. */
  211. function escapeStringChar(chr) {
  212. return '\\' + stringEscapes[chr];
  213. }
  214. /**
  215. * Gets the value at `key` of `object`.
  216. *
  217. * @private
  218. * @param {Object} [object] The object to query.
  219. * @param {string} key The key of the property to get.
  220. * @returns {*} Returns the property value.
  221. */
  222. function getValue(object, key) {
  223. return object == null ? undefined : object[key];
  224. }
  225. /**
  226. * Creates a unary function that invokes `func` with its argument transformed.
  227. *
  228. * @private
  229. * @param {Function} func The function to wrap.
  230. * @param {Function} transform The argument transform.
  231. * @returns {Function} Returns the new function.
  232. */
  233. function overArg(func, transform) {
  234. return function(arg) {
  235. return func(transform(arg));
  236. };
  237. }
  238. /** Used for built-in method references. */
  239. var funcProto = Function.prototype,
  240. objectProto = Object.prototype;
  241. /** Used to detect overreaching core-js shims. */
  242. var coreJsData = root['__core-js_shared__'];
  243. /** Used to resolve the decompiled source of functions. */
  244. var funcToString = funcProto.toString;
  245. /** Used to check objects for own properties. */
  246. var hasOwnProperty = objectProto.hasOwnProperty;
  247. /** Used to detect methods masquerading as native. */
  248. var maskSrcKey = (function() {
  249. var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');
  250. return uid ? ('Symbol(src)_1.' + uid) : '';
  251. }());
  252. /**
  253. * Used to resolve the
  254. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  255. * of values.
  256. */
  257. var nativeObjectToString = objectProto.toString;
  258. /** Used to infer the `Object` constructor. */
  259. var objectCtorString = funcToString.call(Object);
  260. /** Used to detect if a method is native. */
  261. var reIsNative = RegExp('^' +
  262. funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
  263. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  264. );
  265. /** Built-in value references. */
  266. var Buffer = moduleExports ? root.Buffer : undefined,
  267. Symbol = root.Symbol,
  268. getPrototype = overArg(Object.getPrototypeOf, Object),
  269. propertyIsEnumerable = objectProto.propertyIsEnumerable,
  270. symToStringTag = Symbol ? Symbol.toStringTag : undefined;
  271. var defineProperty = (function() {
  272. try {
  273. var func = getNative(Object, 'defineProperty');
  274. func({}, '', {});
  275. return func;
  276. } catch (e) {}
  277. }());
  278. /* Built-in method references for those with the same name as other `lodash` methods. */
  279. var nativeIsBuffer = Buffer ? Buffer.isBuffer : undefined,
  280. nativeKeys = overArg(Object.keys, Object),
  281. nativeMax = Math.max,
  282. nativeNow = Date.now;
  283. /** Used to convert symbols to primitives and strings. */
  284. var symbolProto = Symbol ? Symbol.prototype : undefined,
  285. symbolToString = symbolProto ? symbolProto.toString : undefined;
  286. /**
  287. * Creates an array of the enumerable property names of the array-like `value`.
  288. *
  289. * @private
  290. * @param {*} value The value to query.
  291. * @param {boolean} inherited Specify returning inherited property names.
  292. * @returns {Array} Returns the array of property names.
  293. */
  294. function arrayLikeKeys(value, inherited) {
  295. var isArr = isArray(value),
  296. isArg = !isArr && isArguments(value),
  297. isBuff = !isArr && !isArg && isBuffer(value),
  298. isType = !isArr && !isArg && !isBuff && isTypedArray(value),
  299. skipIndexes = isArr || isArg || isBuff || isType,
  300. result = skipIndexes ? baseTimes(value.length, String) : [],
  301. length = result.length;
  302. for (var key in value) {
  303. if ((inherited || hasOwnProperty.call(value, key)) &&
  304. !(skipIndexes && (
  305. // Safari 9 has enumerable `arguments.length` in strict mode.
  306. key == 'length' ||
  307. // Node.js 0.10 has enumerable non-index properties on buffers.
  308. (isBuff && (key == 'offset' || key == 'parent')) ||
  309. // PhantomJS 2 has enumerable non-index properties on typed arrays.
  310. (isType && (key == 'buffer' || key == 'byteLength' || key == 'byteOffset')) ||
  311. // Skip index properties.
  312. isIndex(key, length)
  313. ))) {
  314. result.push(key);
  315. }
  316. }
  317. return result;
  318. }
  319. /**
  320. * Assigns `value` to `key` of `object` if the existing value is not equivalent
  321. * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  322. * for equality comparisons.
  323. *
  324. * @private
  325. * @param {Object} object The object to modify.
  326. * @param {string} key The key of the property to assign.
  327. * @param {*} value The value to assign.
  328. */
  329. function assignValue(object, key, value) {
  330. var objValue = object[key];
  331. if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) ||
  332. (value === undefined && !(key in object))) {
  333. baseAssignValue(object, key, value);
  334. }
  335. }
  336. /**
  337. * The base implementation of `assignValue` and `assignMergeValue` without
  338. * value checks.
  339. *
  340. * @private
  341. * @param {Object} object The object to modify.
  342. * @param {string} key The key of the property to assign.
  343. * @param {*} value The value to assign.
  344. */
  345. function baseAssignValue(object, key, value) {
  346. if (key == '__proto__' && defineProperty) {
  347. defineProperty(object, key, {
  348. 'configurable': true,
  349. 'enumerable': true,
  350. 'value': value,
  351. 'writable': true
  352. });
  353. } else {
  354. object[key] = value;
  355. }
  356. }
  357. /**
  358. * The base implementation of `getTag` without fallbacks for buggy environments.
  359. *
  360. * @private
  361. * @param {*} value The value to query.
  362. * @returns {string} Returns the `toStringTag`.
  363. */
  364. function baseGetTag(value) {
  365. if (value == null) {
  366. return value === undefined ? undefinedTag : nullTag;
  367. }
  368. return (symToStringTag && symToStringTag in Object(value))
  369. ? getRawTag(value)
  370. : objectToString(value);
  371. }
  372. /**
  373. * The base implementation of `_.isArguments`.
  374. *
  375. * @private
  376. * @param {*} value The value to check.
  377. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  378. */
  379. function baseIsArguments(value) {
  380. return isObjectLike(value) && baseGetTag(value) == argsTag;
  381. }
  382. /**
  383. * The base implementation of `_.isNative` without bad shim checks.
  384. *
  385. * @private
  386. * @param {*} value The value to check.
  387. * @returns {boolean} Returns `true` if `value` is a native function,
  388. * else `false`.
  389. */
  390. function baseIsNative(value) {
  391. if (!isObject(value) || isMasked(value)) {
  392. return false;
  393. }
  394. var pattern = isFunction(value) ? reIsNative : reIsHostCtor;
  395. return pattern.test(toSource(value));
  396. }
  397. /**
  398. * The base implementation of `_.isTypedArray` without Node.js optimizations.
  399. *
  400. * @private
  401. * @param {*} value The value to check.
  402. * @returns {boolean} Returns `true` if `value` is a typed array, else `false`.
  403. */
  404. function baseIsTypedArray(value) {
  405. return isObjectLike(value) &&
  406. isLength(value.length) && !!typedArrayTags[baseGetTag(value)];
  407. }
  408. /**
  409. * The base implementation of `_.keys` which doesn't treat sparse arrays as dense.
  410. *
  411. * @private
  412. * @param {Object} object The object to query.
  413. * @returns {Array} Returns the array of property names.
  414. */
  415. function baseKeys(object) {
  416. if (!isPrototype(object)) {
  417. return nativeKeys(object);
  418. }
  419. var result = [];
  420. for (var key in Object(object)) {
  421. if (hasOwnProperty.call(object, key) && key != 'constructor') {
  422. result.push(key);
  423. }
  424. }
  425. return result;
  426. }
  427. /**
  428. * The base implementation of `_.keysIn` which doesn't treat sparse arrays as dense.
  429. *
  430. * @private
  431. * @param {Object} object The object to query.
  432. * @returns {Array} Returns the array of property names.
  433. */
  434. function baseKeysIn(object) {
  435. if (!isObject(object)) {
  436. return nativeKeysIn(object);
  437. }
  438. var isProto = isPrototype(object),
  439. result = [];
  440. for (var key in object) {
  441. if (!(key == 'constructor' && (isProto || !hasOwnProperty.call(object, key)))) {
  442. result.push(key);
  443. }
  444. }
  445. return result;
  446. }
  447. /**
  448. * The base implementation of `_.rest` which doesn't validate or coerce arguments.
  449. *
  450. * @private
  451. * @param {Function} func The function to apply a rest parameter to.
  452. * @param {number} [start=func.length-1] The start position of the rest parameter.
  453. * @returns {Function} Returns the new function.
  454. */
  455. function baseRest(func, start) {
  456. return setToString(overRest(func, start, identity), func + '');
  457. }
  458. /**
  459. * The base implementation of `setToString` without support for hot loop shorting.
  460. *
  461. * @private
  462. * @param {Function} func The function to modify.
  463. * @param {Function} string The `toString` result.
  464. * @returns {Function} Returns `func`.
  465. */
  466. var baseSetToString = !defineProperty ? identity : function(func, string) {
  467. return defineProperty(func, 'toString', {
  468. 'configurable': true,
  469. 'enumerable': false,
  470. 'value': constant(string),
  471. 'writable': true
  472. });
  473. };
  474. /**
  475. * The base implementation of `_.toString` which doesn't convert nullish
  476. * values to empty strings.
  477. *
  478. * @private
  479. * @param {*} value The value to process.
  480. * @returns {string} Returns the string.
  481. */
  482. function baseToString(value) {
  483. // Exit early for strings to avoid a performance hit in some environments.
  484. if (typeof value == 'string') {
  485. return value;
  486. }
  487. if (isArray(value)) {
  488. // Recursively convert values (susceptible to call stack limits).
  489. return arrayMap(value, baseToString) + '';
  490. }
  491. if (isSymbol(value)) {
  492. return symbolToString ? symbolToString.call(value) : '';
  493. }
  494. var result = (value + '');
  495. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  496. }
  497. /**
  498. * Copies properties of `source` to `object`.
  499. *
  500. * @private
  501. * @param {Object} source The object to copy properties from.
  502. * @param {Array} props The property identifiers to copy.
  503. * @param {Object} [object={}] The object to copy properties to.
  504. * @param {Function} [customizer] The function to customize copied values.
  505. * @returns {Object} Returns `object`.
  506. */
  507. function copyObject(source, props, object, customizer) {
  508. var isNew = !object;
  509. object || (object = {});
  510. var index = -1,
  511. length = props.length;
  512. while (++index < length) {
  513. var key = props[index];
  514. var newValue = customizer
  515. ? customizer(object[key], source[key], key, object, source)
  516. : undefined;
  517. if (newValue === undefined) {
  518. newValue = source[key];
  519. }
  520. if (isNew) {
  521. baseAssignValue(object, key, newValue);
  522. } else {
  523. assignValue(object, key, newValue);
  524. }
  525. }
  526. return object;
  527. }
  528. /**
  529. * Creates a function like `_.assign`.
  530. *
  531. * @private
  532. * @param {Function} assigner The function to assign values.
  533. * @returns {Function} Returns the new assigner function.
  534. */
  535. function createAssigner(assigner) {
  536. return baseRest(function(object, sources) {
  537. var index = -1,
  538. length = sources.length,
  539. customizer = length > 1 ? sources[length - 1] : undefined,
  540. guard = length > 2 ? sources[2] : undefined;
  541. customizer = (assigner.length > 3 && typeof customizer == 'function')
  542. ? (length--, customizer)
  543. : undefined;
  544. if (guard && isIterateeCall(sources[0], sources[1], guard)) {
  545. customizer = length < 3 ? undefined : customizer;
  546. length = 1;
  547. }
  548. object = Object(object);
  549. while (++index < length) {
  550. var source = sources[index];
  551. if (source) {
  552. assigner(object, source, index, customizer);
  553. }
  554. }
  555. return object;
  556. });
  557. }
  558. /**
  559. * Used by `_.defaults` to customize its `_.assignIn` use to assign properties
  560. * of source objects to the destination object for all destination properties
  561. * that resolve to `undefined`.
  562. *
  563. * @private
  564. * @param {*} objValue The destination value.
  565. * @param {*} srcValue The source value.
  566. * @param {string} key The key of the property to assign.
  567. * @param {Object} object The parent object of `objValue`.
  568. * @returns {*} Returns the value to assign.
  569. */
  570. function customDefaultsAssignIn(objValue, srcValue, key, object) {
  571. if (objValue === undefined ||
  572. (eq(objValue, objectProto[key]) && !hasOwnProperty.call(object, key))) {
  573. return srcValue;
  574. }
  575. return objValue;
  576. }
  577. /**
  578. * Gets the native function at `key` of `object`.
  579. *
  580. * @private
  581. * @param {Object} object The object to query.
  582. * @param {string} key The key of the method to get.
  583. * @returns {*} Returns the function if it's native, else `undefined`.
  584. */
  585. function getNative(object, key) {
  586. var value = getValue(object, key);
  587. return baseIsNative(value) ? value : undefined;
  588. }
  589. /**
  590. * A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values.
  591. *
  592. * @private
  593. * @param {*} value The value to query.
  594. * @returns {string} Returns the raw `toStringTag`.
  595. */
  596. function getRawTag(value) {
  597. var isOwn = hasOwnProperty.call(value, symToStringTag),
  598. tag = value[symToStringTag];
  599. try {
  600. value[symToStringTag] = undefined;
  601. var unmasked = true;
  602. } catch (e) {}
  603. var result = nativeObjectToString.call(value);
  604. if (unmasked) {
  605. if (isOwn) {
  606. value[symToStringTag] = tag;
  607. } else {
  608. delete value[symToStringTag];
  609. }
  610. }
  611. return result;
  612. }
  613. /**
  614. * Checks if `value` is a valid array-like index.
  615. *
  616. * @private
  617. * @param {*} value The value to check.
  618. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  619. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  620. */
  621. function isIndex(value, length) {
  622. var type = typeof value;
  623. length = length == null ? MAX_SAFE_INTEGER : length;
  624. return !!length &&
  625. (type == 'number' ||
  626. (type != 'symbol' && reIsUint.test(value))) &&
  627. (value > -1 && value % 1 == 0 && value < length);
  628. }
  629. /**
  630. * Checks if the given arguments are from an iteratee call.
  631. *
  632. * @private
  633. * @param {*} value The potential iteratee value argument.
  634. * @param {*} index The potential iteratee index or key argument.
  635. * @param {*} object The potential iteratee object argument.
  636. * @returns {boolean} Returns `true` if the arguments are from an iteratee call,
  637. * else `false`.
  638. */
  639. function isIterateeCall(value, index, object) {
  640. if (!isObject(object)) {
  641. return false;
  642. }
  643. var type = typeof index;
  644. if (type == 'number'
  645. ? (isArrayLike(object) && isIndex(index, object.length))
  646. : (type == 'string' && index in object)
  647. ) {
  648. return eq(object[index], value);
  649. }
  650. return false;
  651. }
  652. /**
  653. * Checks if `func` has its source masked.
  654. *
  655. * @private
  656. * @param {Function} func The function to check.
  657. * @returns {boolean} Returns `true` if `func` is masked, else `false`.
  658. */
  659. function isMasked(func) {
  660. return !!maskSrcKey && (maskSrcKey in func);
  661. }
  662. /**
  663. * Checks if `value` is likely a prototype object.
  664. *
  665. * @private
  666. * @param {*} value The value to check.
  667. * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
  668. */
  669. function isPrototype(value) {
  670. var Ctor = value && value.constructor,
  671. proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
  672. return value === proto;
  673. }
  674. /**
  675. * This function is like
  676. * [`Object.keys`](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
  677. * except that it includes inherited enumerable properties.
  678. *
  679. * @private
  680. * @param {Object} object The object to query.
  681. * @returns {Array} Returns the array of property names.
  682. */
  683. function nativeKeysIn(object) {
  684. var result = [];
  685. if (object != null) {
  686. for (var key in Object(object)) {
  687. result.push(key);
  688. }
  689. }
  690. return result;
  691. }
  692. /**
  693. * Converts `value` to a string using `Object.prototype.toString`.
  694. *
  695. * @private
  696. * @param {*} value The value to convert.
  697. * @returns {string} Returns the converted string.
  698. */
  699. function objectToString(value) {
  700. return nativeObjectToString.call(value);
  701. }
  702. /**
  703. * A specialized version of `baseRest` which transforms the rest array.
  704. *
  705. * @private
  706. * @param {Function} func The function to apply a rest parameter to.
  707. * @param {number} [start=func.length-1] The start position of the rest parameter.
  708. * @param {Function} transform The rest array transform.
  709. * @returns {Function} Returns the new function.
  710. */
  711. function overRest(func, start, transform) {
  712. start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
  713. return function() {
  714. var args = arguments,
  715. index = -1,
  716. length = nativeMax(args.length - start, 0),
  717. array = Array(length);
  718. while (++index < length) {
  719. array[index] = args[start + index];
  720. }
  721. index = -1;
  722. var otherArgs = Array(start + 1);
  723. while (++index < start) {
  724. otherArgs[index] = args[index];
  725. }
  726. otherArgs[start] = transform(array);
  727. return apply(func, this, otherArgs);
  728. };
  729. }
  730. /**
  731. * Sets the `toString` method of `func` to return `string`.
  732. *
  733. * @private
  734. * @param {Function} func The function to modify.
  735. * @param {Function} string The `toString` result.
  736. * @returns {Function} Returns `func`.
  737. */
  738. var setToString = shortOut(baseSetToString);
  739. /**
  740. * Creates a function that'll short out and invoke `identity` instead
  741. * of `func` when it's called `HOT_COUNT` or more times in `HOT_SPAN`
  742. * milliseconds.
  743. *
  744. * @private
  745. * @param {Function} func The function to restrict.
  746. * @returns {Function} Returns the new shortable function.
  747. */
  748. function shortOut(func) {
  749. var count = 0,
  750. lastCalled = 0;
  751. return function() {
  752. var stamp = nativeNow(),
  753. remaining = HOT_SPAN - (stamp - lastCalled);
  754. lastCalled = stamp;
  755. if (remaining > 0) {
  756. if (++count >= HOT_COUNT) {
  757. return arguments[0];
  758. }
  759. } else {
  760. count = 0;
  761. }
  762. return func.apply(undefined, arguments);
  763. };
  764. }
  765. /**
  766. * Converts `func` to its source code.
  767. *
  768. * @private
  769. * @param {Function} func The function to convert.
  770. * @returns {string} Returns the source code.
  771. */
  772. function toSource(func) {
  773. if (func != null) {
  774. try {
  775. return funcToString.call(func);
  776. } catch (e) {}
  777. try {
  778. return (func + '');
  779. } catch (e) {}
  780. }
  781. return '';
  782. }
  783. /**
  784. * Performs a
  785. * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  786. * comparison between two values to determine if they are equivalent.
  787. *
  788. * @static
  789. * @memberOf _
  790. * @since 4.0.0
  791. * @category Lang
  792. * @param {*} value The value to compare.
  793. * @param {*} other The other value to compare.
  794. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  795. * @example
  796. *
  797. * var object = { 'a': 1 };
  798. * var other = { 'a': 1 };
  799. *
  800. * _.eq(object, object);
  801. * // => true
  802. *
  803. * _.eq(object, other);
  804. * // => false
  805. *
  806. * _.eq('a', 'a');
  807. * // => true
  808. *
  809. * _.eq('a', Object('a'));
  810. * // => false
  811. *
  812. * _.eq(NaN, NaN);
  813. * // => true
  814. */
  815. function eq(value, other) {
  816. return value === other || (value !== value && other !== other);
  817. }
  818. /**
  819. * Checks if `value` is likely an `arguments` object.
  820. *
  821. * @static
  822. * @memberOf _
  823. * @since 0.1.0
  824. * @category Lang
  825. * @param {*} value The value to check.
  826. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  827. * else `false`.
  828. * @example
  829. *
  830. * _.isArguments(function() { return arguments; }());
  831. * // => true
  832. *
  833. * _.isArguments([1, 2, 3]);
  834. * // => false
  835. */
  836. var isArguments = baseIsArguments(function() { return arguments; }()) ? baseIsArguments : function(value) {
  837. return isObjectLike(value) && hasOwnProperty.call(value, 'callee') &&
  838. !propertyIsEnumerable.call(value, 'callee');
  839. };
  840. /**
  841. * Checks if `value` is classified as an `Array` object.
  842. *
  843. * @static
  844. * @memberOf _
  845. * @since 0.1.0
  846. * @category Lang
  847. * @param {*} value The value to check.
  848. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  849. * @example
  850. *
  851. * _.isArray([1, 2, 3]);
  852. * // => true
  853. *
  854. * _.isArray(document.body.children);
  855. * // => false
  856. *
  857. * _.isArray('abc');
  858. * // => false
  859. *
  860. * _.isArray(_.noop);
  861. * // => false
  862. */
  863. var isArray = Array.isArray;
  864. /**
  865. * Checks if `value` is array-like. A value is considered array-like if it's
  866. * not a function and has a `value.length` that's an integer greater than or
  867. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  868. *
  869. * @static
  870. * @memberOf _
  871. * @since 4.0.0
  872. * @category Lang
  873. * @param {*} value The value to check.
  874. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  875. * @example
  876. *
  877. * _.isArrayLike([1, 2, 3]);
  878. * // => true
  879. *
  880. * _.isArrayLike(document.body.children);
  881. * // => true
  882. *
  883. * _.isArrayLike('abc');
  884. * // => true
  885. *
  886. * _.isArrayLike(_.noop);
  887. * // => false
  888. */
  889. function isArrayLike(value) {
  890. return value != null && isLength(value.length) && !isFunction(value);
  891. }
  892. /**
  893. * Checks if `value` is a buffer.
  894. *
  895. * @static
  896. * @memberOf _
  897. * @since 4.3.0
  898. * @category Lang
  899. * @param {*} value The value to check.
  900. * @returns {boolean} Returns `true` if `value` is a buffer, else `false`.
  901. * @example
  902. *
  903. * _.isBuffer(new Buffer(2));
  904. * // => true
  905. *
  906. * _.isBuffer(new Uint8Array(2));
  907. * // => false
  908. */
  909. var isBuffer = nativeIsBuffer || stubFalse;
  910. /**
  911. * Checks if `value` is an `Error`, `EvalError`, `RangeError`, `ReferenceError`,
  912. * `SyntaxError`, `TypeError`, or `URIError` object.
  913. *
  914. * @static
  915. * @memberOf _
  916. * @since 3.0.0
  917. * @category Lang
  918. * @param {*} value The value to check.
  919. * @returns {boolean} Returns `true` if `value` is an error object, else `false`.
  920. * @example
  921. *
  922. * _.isError(new Error);
  923. * // => true
  924. *
  925. * _.isError(Error);
  926. * // => false
  927. */
  928. function isError(value) {
  929. if (!isObjectLike(value)) {
  930. return false;
  931. }
  932. var tag = baseGetTag(value);
  933. return tag == errorTag || tag == domExcTag ||
  934. (typeof value.message == 'string' && typeof value.name == 'string' && !isPlainObject(value));
  935. }
  936. /**
  937. * Checks if `value` is classified as a `Function` object.
  938. *
  939. * @static
  940. * @memberOf _
  941. * @since 0.1.0
  942. * @category Lang
  943. * @param {*} value The value to check.
  944. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  945. * @example
  946. *
  947. * _.isFunction(_);
  948. * // => true
  949. *
  950. * _.isFunction(/abc/);
  951. * // => false
  952. */
  953. function isFunction(value) {
  954. if (!isObject(value)) {
  955. return false;
  956. }
  957. // The use of `Object#toString` avoids issues with the `typeof` operator
  958. // in Safari 9 which returns 'object' for typed arrays and other constructors.
  959. var tag = baseGetTag(value);
  960. return tag == funcTag || tag == genTag || tag == asyncTag || tag == proxyTag;
  961. }
  962. /**
  963. * Checks if `value` is a valid array-like length.
  964. *
  965. * **Note:** This method is loosely based on
  966. * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
  967. *
  968. * @static
  969. * @memberOf _
  970. * @since 4.0.0
  971. * @category Lang
  972. * @param {*} value The value to check.
  973. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  974. * @example
  975. *
  976. * _.isLength(3);
  977. * // => true
  978. *
  979. * _.isLength(Number.MIN_VALUE);
  980. * // => false
  981. *
  982. * _.isLength(Infinity);
  983. * // => false
  984. *
  985. * _.isLength('3');
  986. * // => false
  987. */
  988. function isLength(value) {
  989. return typeof value == 'number' &&
  990. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  991. }
  992. /**
  993. * Checks if `value` is the
  994. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  995. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  996. *
  997. * @static
  998. * @memberOf _
  999. * @since 0.1.0
  1000. * @category Lang
  1001. * @param {*} value The value to check.
  1002. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  1003. * @example
  1004. *
  1005. * _.isObject({});
  1006. * // => true
  1007. *
  1008. * _.isObject([1, 2, 3]);
  1009. * // => true
  1010. *
  1011. * _.isObject(_.noop);
  1012. * // => true
  1013. *
  1014. * _.isObject(null);
  1015. * // => false
  1016. */
  1017. function isObject(value) {
  1018. var type = typeof value;
  1019. return value != null && (type == 'object' || type == 'function');
  1020. }
  1021. /**
  1022. * Checks if `value` is object-like. A value is object-like if it's not `null`
  1023. * and has a `typeof` result of "object".
  1024. *
  1025. * @static
  1026. * @memberOf _
  1027. * @since 4.0.0
  1028. * @category Lang
  1029. * @param {*} value The value to check.
  1030. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  1031. * @example
  1032. *
  1033. * _.isObjectLike({});
  1034. * // => true
  1035. *
  1036. * _.isObjectLike([1, 2, 3]);
  1037. * // => true
  1038. *
  1039. * _.isObjectLike(_.noop);
  1040. * // => false
  1041. *
  1042. * _.isObjectLike(null);
  1043. * // => false
  1044. */
  1045. function isObjectLike(value) {
  1046. return value != null && typeof value == 'object';
  1047. }
  1048. /**
  1049. * Checks if `value` is a plain object, that is, an object created by the
  1050. * `Object` constructor or one with a `[[Prototype]]` of `null`.
  1051. *
  1052. * @static
  1053. * @memberOf _
  1054. * @since 0.8.0
  1055. * @category Lang
  1056. * @param {*} value The value to check.
  1057. * @returns {boolean} Returns `true` if `value` is a plain object, else `false`.
  1058. * @example
  1059. *
  1060. * function Foo() {
  1061. * this.a = 1;
  1062. * }
  1063. *
  1064. * _.isPlainObject(new Foo);
  1065. * // => false
  1066. *
  1067. * _.isPlainObject([1, 2, 3]);
  1068. * // => false
  1069. *
  1070. * _.isPlainObject({ 'x': 0, 'y': 0 });
  1071. * // => true
  1072. *
  1073. * _.isPlainObject(Object.create(null));
  1074. * // => true
  1075. */
  1076. function isPlainObject(value) {
  1077. if (!isObjectLike(value) || baseGetTag(value) != objectTag) {
  1078. return false;
  1079. }
  1080. var proto = getPrototype(value);
  1081. if (proto === null) {
  1082. return true;
  1083. }
  1084. var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor;
  1085. return typeof Ctor == 'function' && Ctor instanceof Ctor &&
  1086. funcToString.call(Ctor) == objectCtorString;
  1087. }
  1088. /**
  1089. * Checks if `value` is classified as a `Symbol` primitive or object.
  1090. *
  1091. * @static
  1092. * @memberOf _
  1093. * @since 4.0.0
  1094. * @category Lang
  1095. * @param {*} value The value to check.
  1096. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  1097. * @example
  1098. *
  1099. * _.isSymbol(Symbol.iterator);
  1100. * // => true
  1101. *
  1102. * _.isSymbol('abc');
  1103. * // => false
  1104. */
  1105. function isSymbol(value) {
  1106. return typeof value == 'symbol' ||
  1107. (isObjectLike(value) && baseGetTag(value) == symbolTag);
  1108. }
  1109. /**
  1110. * Checks if `value` is classified as a typed array.
  1111. *
  1112. * @static
  1113. * @memberOf _
  1114. * @since 3.0.0
  1115. * @category Lang
  1116. * @param {*} value The value to check.
  1117. * @returns {boolean} Returns `true` if `value` is a typed array, else `false`.
  1118. * @example
  1119. *
  1120. * _.isTypedArray(new Uint8Array);
  1121. * // => true
  1122. *
  1123. * _.isTypedArray([]);
  1124. * // => false
  1125. */
  1126. var isTypedArray = nodeIsTypedArray ? baseUnary(nodeIsTypedArray) : baseIsTypedArray;
  1127. /**
  1128. * Converts `value` to a string. An empty string is returned for `null`
  1129. * and `undefined` values. The sign of `-0` is preserved.
  1130. *
  1131. * @static
  1132. * @memberOf _
  1133. * @since 4.0.0
  1134. * @category Lang
  1135. * @param {*} value The value to convert.
  1136. * @returns {string} Returns the converted string.
  1137. * @example
  1138. *
  1139. * _.toString(null);
  1140. * // => ''
  1141. *
  1142. * _.toString(-0);
  1143. * // => '-0'
  1144. *
  1145. * _.toString([1, 2, 3]);
  1146. * // => '1,2,3'
  1147. */
  1148. function toString(value) {
  1149. return value == null ? '' : baseToString(value);
  1150. }
  1151. /**
  1152. * This method is like `_.assignIn` except that it accepts `customizer`
  1153. * which is invoked to produce the assigned values. If `customizer` returns
  1154. * `undefined`, assignment is handled by the method instead. The `customizer`
  1155. * is invoked with five arguments: (objValue, srcValue, key, object, source).
  1156. *
  1157. * **Note:** This method mutates `object`.
  1158. *
  1159. * @static
  1160. * @memberOf _
  1161. * @since 4.0.0
  1162. * @alias extendWith
  1163. * @category Object
  1164. * @param {Object} object The destination object.
  1165. * @param {...Object} sources The source objects.
  1166. * @param {Function} [customizer] The function to customize assigned values.
  1167. * @returns {Object} Returns `object`.
  1168. * @see _.assignWith
  1169. * @example
  1170. *
  1171. * function customizer(objValue, srcValue) {
  1172. * return _.isUndefined(objValue) ? srcValue : objValue;
  1173. * }
  1174. *
  1175. * var defaults = _.partialRight(_.assignInWith, customizer);
  1176. *
  1177. * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
  1178. * // => { 'a': 1, 'b': 2 }
  1179. */
  1180. var assignInWith = createAssigner(function(object, source, srcIndex, customizer) {
  1181. copyObject(source, keysIn(source), object, customizer);
  1182. });
  1183. /**
  1184. * Creates an array of the own enumerable property names of `object`.
  1185. *
  1186. * **Note:** Non-object values are coerced to objects. See the
  1187. * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
  1188. * for more details.
  1189. *
  1190. * @static
  1191. * @since 0.1.0
  1192. * @memberOf _
  1193. * @category Object
  1194. * @param {Object} object The object to query.
  1195. * @returns {Array} Returns the array of property names.
  1196. * @example
  1197. *
  1198. * function Foo() {
  1199. * this.a = 1;
  1200. * this.b = 2;
  1201. * }
  1202. *
  1203. * Foo.prototype.c = 3;
  1204. *
  1205. * _.keys(new Foo);
  1206. * // => ['a', 'b'] (iteration order is not guaranteed)
  1207. *
  1208. * _.keys('hi');
  1209. * // => ['0', '1']
  1210. */
  1211. function keys(object) {
  1212. return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object);
  1213. }
  1214. /**
  1215. * Creates an array of the own and inherited enumerable property names of `object`.
  1216. *
  1217. * **Note:** Non-object values are coerced to objects.
  1218. *
  1219. * @static
  1220. * @memberOf _
  1221. * @since 3.0.0
  1222. * @category Object
  1223. * @param {Object} object The object to query.
  1224. * @returns {Array} Returns the array of property names.
  1225. * @example
  1226. *
  1227. * function Foo() {
  1228. * this.a = 1;
  1229. * this.b = 2;
  1230. * }
  1231. *
  1232. * Foo.prototype.c = 3;
  1233. *
  1234. * _.keysIn(new Foo);
  1235. * // => ['a', 'b', 'c'] (iteration order is not guaranteed)
  1236. */
  1237. function keysIn(object) {
  1238. return isArrayLike(object) ? arrayLikeKeys(object, true) : baseKeysIn(object);
  1239. }
  1240. /**
  1241. * Creates a compiled template function that can interpolate data properties
  1242. * in "interpolate" delimiters, HTML-escape interpolated data properties in
  1243. * "escape" delimiters, and execute JavaScript in "evaluate" delimiters. Data
  1244. * properties may be accessed as free variables in the template. If a setting
  1245. * object is given, it takes precedence over `_.templateSettings` values.
  1246. *
  1247. * **Note:** In the development build `_.template` utilizes
  1248. * [sourceURLs](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl)
  1249. * for easier debugging.
  1250. *
  1251. * For more information on precompiling templates see
  1252. * [lodash's custom builds documentation](https://lodash.com/custom-builds).
  1253. *
  1254. * For more information on Chrome extension sandboxes see
  1255. * [Chrome's extensions documentation](https://developer.chrome.com/extensions/sandboxingEval).
  1256. *
  1257. * @static
  1258. * @since 0.1.0
  1259. * @memberOf _
  1260. * @category String
  1261. * @param {string} [string=''] The template string.
  1262. * @param {Object} [options={}] The options object.
  1263. * @param {RegExp} [options.escape=_.templateSettings.escape]
  1264. * The HTML "escape" delimiter.
  1265. * @param {RegExp} [options.evaluate=_.templateSettings.evaluate]
  1266. * The "evaluate" delimiter.
  1267. * @param {Object} [options.imports=_.templateSettings.imports]
  1268. * An object to import into the template as free variables.
  1269. * @param {RegExp} [options.interpolate=_.templateSettings.interpolate]
  1270. * The "interpolate" delimiter.
  1271. * @param {string} [options.sourceURL='templateSources[n]']
  1272. * The sourceURL of the compiled template.
  1273. * @param {string} [options.variable='obj']
  1274. * The data object variable name.
  1275. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  1276. * @returns {Function} Returns the compiled template function.
  1277. * @example
  1278. *
  1279. * // Use the "interpolate" delimiter to create a compiled template.
  1280. * var compiled = _.template('hello <%= user %>!');
  1281. * compiled({ 'user': 'fred' });
  1282. * // => 'hello fred!'
  1283. *
  1284. * // Use the HTML "escape" delimiter to escape data property values.
  1285. * var compiled = _.template('<b><%- value %></b>');
  1286. * compiled({ 'value': '<script>' });
  1287. * // => '<b>&lt;script&gt;</b>'
  1288. *
  1289. * // Use the "evaluate" delimiter to execute JavaScript and generate HTML.
  1290. * var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
  1291. * compiled({ 'users': ['fred', 'barney'] });
  1292. * // => '<li>fred</li><li>barney</li>'
  1293. *
  1294. * // Use the internal `print` function in "evaluate" delimiters.
  1295. * var compiled = _.template('<% print("hello " + user); %>!');
  1296. * compiled({ 'user': 'barney' });
  1297. * // => 'hello barney!'
  1298. *
  1299. * // Use the ES template literal delimiter as an "interpolate" delimiter.
  1300. * // Disable support by replacing the "interpolate" delimiter.
  1301. * var compiled = _.template('hello ${ user }!');
  1302. * compiled({ 'user': 'pebbles' });
  1303. * // => 'hello pebbles!'
  1304. *
  1305. * // Use backslashes to treat delimiters as plain text.
  1306. * var compiled = _.template('<%= "\\<%- value %\\>" %>');
  1307. * compiled({ 'value': 'ignored' });
  1308. * // => '<%- value %>'
  1309. *
  1310. * // Use the `imports` option to import `jQuery` as `jq`.
  1311. * var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
  1312. * var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
  1313. * compiled({ 'users': ['fred', 'barney'] });
  1314. * // => '<li>fred</li><li>barney</li>'
  1315. *
  1316. * // Use the `sourceURL` option to specify a custom sourceURL for the template.
  1317. * var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
  1318. * compiled(data);
  1319. * // => Find the source of "greeting.jst" under the Sources tab or Resources panel of the web inspector.
  1320. *
  1321. * // Use the `variable` option to ensure a with-statement isn't used in the compiled template.
  1322. * var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
  1323. * compiled.source;
  1324. * // => function(data) {
  1325. * // var __t, __p = '';
  1326. * // __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
  1327. * // return __p;
  1328. * // }
  1329. *
  1330. * // Use custom template delimiters.
  1331. * _.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
  1332. * var compiled = _.template('hello {{ user }}!');
  1333. * compiled({ 'user': 'mustache' });
  1334. * // => 'hello mustache!'
  1335. *
  1336. * // Use the `source` property to inline compiled templates for meaningful
  1337. * // line numbers in error messages and stack traces.
  1338. * fs.writeFileSync(path.join(process.cwd(), 'jst.js'), '\
  1339. * var JST = {\
  1340. * "main": ' + _.template(mainText).source + '\
  1341. * };\
  1342. * ');
  1343. */
  1344. function template(string, options, guard) {
  1345. // Based on John Resig's `tmpl` implementation
  1346. // (http://ejohn.org/blog/javascript-micro-templating/)
  1347. // and Laura Doktorova's doT.js (https://github.com/olado/doT).
  1348. var settings = templateSettings.imports._.templateSettings || templateSettings;
  1349. if (guard && isIterateeCall(string, options, guard)) {
  1350. options = undefined;
  1351. }
  1352. string = toString(string);
  1353. options = assignInWith({}, options, settings, customDefaultsAssignIn);
  1354. var imports = assignInWith({}, options.imports, settings.imports, customDefaultsAssignIn),
  1355. importsKeys = keys(imports),
  1356. importsValues = baseValues(imports, importsKeys);
  1357. var isEscaping,
  1358. isEvaluating,
  1359. index = 0,
  1360. interpolate = options.interpolate || reNoMatch,
  1361. source = "__p += '";
  1362. // Compile the regexp to match each delimiter.
  1363. var reDelimiters = RegExp(
  1364. (options.escape || reNoMatch).source + '|' +
  1365. interpolate.source + '|' +
  1366. (interpolate === reInterpolate ? reEsTemplate : reNoMatch).source + '|' +
  1367. (options.evaluate || reNoMatch).source + '|$'
  1368. , 'g');
  1369. // Use a sourceURL for easier debugging.
  1370. // The sourceURL gets injected into the source that's eval-ed, so be careful
  1371. // with lookup (in case of e.g. prototype pollution), and strip newlines if any.
  1372. // A newline wouldn't be a valid sourceURL anyway, and it'd enable code injection.
  1373. var sourceURL = hasOwnProperty.call(options, 'sourceURL')
  1374. ? ('//# sourceURL=' +
  1375. (options.sourceURL + '').replace(/[\r\n]/g, ' ') +
  1376. '\n')
  1377. : '';
  1378. string.replace(reDelimiters, function(match, escapeValue, interpolateValue, esTemplateValue, evaluateValue, offset) {
  1379. interpolateValue || (interpolateValue = esTemplateValue);
  1380. // Escape characters that can't be included in string literals.
  1381. source += string.slice(index, offset).replace(reUnescapedString, escapeStringChar);
  1382. // Replace delimiters with snippets.
  1383. if (escapeValue) {
  1384. isEscaping = true;
  1385. source += "' +\n__e(" + escapeValue + ") +\n'";
  1386. }
  1387. if (evaluateValue) {
  1388. isEvaluating = true;
  1389. source += "';\n" + evaluateValue + ";\n__p += '";
  1390. }
  1391. if (interpolateValue) {
  1392. source += "' +\n((__t = (" + interpolateValue + ")) == null ? '' : __t) +\n'";
  1393. }
  1394. index = offset + match.length;
  1395. // The JS engine embedded in Adobe products needs `match` returned in
  1396. // order to produce the correct `offset` value.
  1397. return match;
  1398. });
  1399. source += "';\n";
  1400. // If `variable` is not specified wrap a with-statement around the generated
  1401. // code to add the data object to the top of the scope chain.
  1402. // Like with sourceURL, we take care to not check the option's prototype,
  1403. // as this configuration is a code injection vector.
  1404. var variable = hasOwnProperty.call(options, 'variable') && options.variable;
  1405. if (!variable) {
  1406. source = 'with (obj) {\n' + source + '\n}\n';
  1407. }
  1408. // Cleanup code by stripping empty strings.
  1409. source = (isEvaluating ? source.replace(reEmptyStringLeading, '') : source)
  1410. .replace(reEmptyStringMiddle, '$1')
  1411. .replace(reEmptyStringTrailing, '$1;');
  1412. // Frame code as the function body.
  1413. source = 'function(' + (variable || 'obj') + ') {\n' +
  1414. (variable
  1415. ? ''
  1416. : 'obj || (obj = {});\n'
  1417. ) +
  1418. "var __t, __p = ''" +
  1419. (isEscaping
  1420. ? ', __e = _.escape'
  1421. : ''
  1422. ) +
  1423. (isEvaluating
  1424. ? ', __j = Array.prototype.join;\n' +
  1425. "function print() { __p += __j.call(arguments, '') }\n"
  1426. : ';\n'
  1427. ) +
  1428. source +
  1429. 'return __p\n}';
  1430. var result = attempt(function() {
  1431. return Function(importsKeys, sourceURL + 'return ' + source)
  1432. .apply(undefined, importsValues);
  1433. });
  1434. // Provide the compiled function's source by its `toString` method or
  1435. // the `source` property as a convenience for inlining compiled templates.
  1436. result.source = source;
  1437. if (isError(result)) {
  1438. throw result;
  1439. }
  1440. return result;
  1441. }
  1442. /**
  1443. * Attempts to invoke `func`, returning either the result or the caught error
  1444. * object. Any additional arguments are provided to `func` when it's invoked.
  1445. *
  1446. * @static
  1447. * @memberOf _
  1448. * @since 3.0.0
  1449. * @category Util
  1450. * @param {Function} func The function to attempt.
  1451. * @param {...*} [args] The arguments to invoke `func` with.
  1452. * @returns {*} Returns the `func` result or error object.
  1453. * @example
  1454. *
  1455. * // Avoid throwing errors for invalid selectors.
  1456. * var elements = _.attempt(function(selector) {
  1457. * return document.querySelectorAll(selector);
  1458. * }, '>_>');
  1459. *
  1460. * if (_.isError(elements)) {
  1461. * elements = [];
  1462. * }
  1463. */
  1464. var attempt = baseRest(function(func, args) {
  1465. try {
  1466. return apply(func, undefined, args);
  1467. } catch (e) {
  1468. return isError(e) ? e : new Error(e);
  1469. }
  1470. });
  1471. /**
  1472. * Creates a function that returns `value`.
  1473. *
  1474. * @static
  1475. * @memberOf _
  1476. * @since 2.4.0
  1477. * @category Util
  1478. * @param {*} value The value to return from the new function.
  1479. * @returns {Function} Returns the new constant function.
  1480. * @example
  1481. *
  1482. * var objects = _.times(2, _.constant({ 'a': 1 }));
  1483. *
  1484. * console.log(objects);
  1485. * // => [{ 'a': 1 }, { 'a': 1 }]
  1486. *
  1487. * console.log(objects[0] === objects[1]);
  1488. * // => true
  1489. */
  1490. function constant(value) {
  1491. return function() {
  1492. return value;
  1493. };
  1494. }
  1495. /**
  1496. * This method returns the first argument it receives.
  1497. *
  1498. * @static
  1499. * @since 0.1.0
  1500. * @memberOf _
  1501. * @category Util
  1502. * @param {*} value Any value.
  1503. * @returns {*} Returns `value`.
  1504. * @example
  1505. *
  1506. * var object = { 'a': 1 };
  1507. *
  1508. * console.log(_.identity(object) === object);
  1509. * // => true
  1510. */
  1511. function identity(value) {
  1512. return value;
  1513. }
  1514. /**
  1515. * This method returns `false`.
  1516. *
  1517. * @static
  1518. * @memberOf _
  1519. * @since 4.13.0
  1520. * @category Util
  1521. * @returns {boolean} Returns `false`.
  1522. * @example
  1523. *
  1524. * _.times(2, _.stubFalse);
  1525. * // => [false, false]
  1526. */
  1527. function stubFalse() {
  1528. return false;
  1529. }
  1530. module.exports = template;