Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. var mapping = require('./_mapping'),
  2. fallbackHolder = require('./placeholder');
  3. /** Built-in value reference. */
  4. var push = Array.prototype.push;
  5. /**
  6. * Creates a function, with an arity of `n`, that invokes `func` with the
  7. * arguments it receives.
  8. *
  9. * @private
  10. * @param {Function} func The function to wrap.
  11. * @param {number} n The arity of the new function.
  12. * @returns {Function} Returns the new function.
  13. */
  14. function baseArity(func, n) {
  15. return n == 2
  16. ? function(a, b) { return func.apply(undefined, arguments); }
  17. : function(a) { return func.apply(undefined, arguments); };
  18. }
  19. /**
  20. * Creates a function that invokes `func`, with up to `n` arguments, ignoring
  21. * any additional arguments.
  22. *
  23. * @private
  24. * @param {Function} func The function to cap arguments for.
  25. * @param {number} n The arity cap.
  26. * @returns {Function} Returns the new function.
  27. */
  28. function baseAry(func, n) {
  29. return n == 2
  30. ? function(a, b) { return func(a, b); }
  31. : function(a) { return func(a); };
  32. }
  33. /**
  34. * Creates a clone of `array`.
  35. *
  36. * @private
  37. * @param {Array} array The array to clone.
  38. * @returns {Array} Returns the cloned array.
  39. */
  40. function cloneArray(array) {
  41. var length = array ? array.length : 0,
  42. result = Array(length);
  43. while (length--) {
  44. result[length] = array[length];
  45. }
  46. return result;
  47. }
  48. /**
  49. * Creates a function that clones a given object using the assignment `func`.
  50. *
  51. * @private
  52. * @param {Function} func The assignment function.
  53. * @returns {Function} Returns the new cloner function.
  54. */
  55. function createCloner(func) {
  56. return function(object) {
  57. return func({}, object);
  58. };
  59. }
  60. /**
  61. * A specialized version of `_.spread` which flattens the spread array into
  62. * the arguments of the invoked `func`.
  63. *
  64. * @private
  65. * @param {Function} func The function to spread arguments over.
  66. * @param {number} start The start position of the spread.
  67. * @returns {Function} Returns the new function.
  68. */
  69. function flatSpread(func, start) {
  70. return function() {
  71. var length = arguments.length,
  72. lastIndex = length - 1,
  73. args = Array(length);
  74. while (length--) {
  75. args[length] = arguments[length];
  76. }
  77. var array = args[start],
  78. otherArgs = args.slice(0, start);
  79. if (array) {
  80. push.apply(otherArgs, array);
  81. }
  82. if (start != lastIndex) {
  83. push.apply(otherArgs, args.slice(start + 1));
  84. }
  85. return func.apply(this, otherArgs);
  86. };
  87. }
  88. /**
  89. * Creates a function that wraps `func` and uses `cloner` to clone the first
  90. * argument it receives.
  91. *
  92. * @private
  93. * @param {Function} func The function to wrap.
  94. * @param {Function} cloner The function to clone arguments.
  95. * @returns {Function} Returns the new immutable function.
  96. */
  97. function wrapImmutable(func, cloner) {
  98. return function() {
  99. var length = arguments.length;
  100. if (!length) {
  101. return;
  102. }
  103. var args = Array(length);
  104. while (length--) {
  105. args[length] = arguments[length];
  106. }
  107. var result = args[0] = cloner.apply(undefined, args);
  108. func.apply(undefined, args);
  109. return result;
  110. };
  111. }
  112. /**
  113. * The base implementation of `convert` which accepts a `util` object of methods
  114. * required to perform conversions.
  115. *
  116. * @param {Object} util The util object.
  117. * @param {string} name The name of the function to convert.
  118. * @param {Function} func The function to convert.
  119. * @param {Object} [options] The options object.
  120. * @param {boolean} [options.cap=true] Specify capping iteratee arguments.
  121. * @param {boolean} [options.curry=true] Specify currying.
  122. * @param {boolean} [options.fixed=true] Specify fixed arity.
  123. * @param {boolean} [options.immutable=true] Specify immutable operations.
  124. * @param {boolean} [options.rearg=true] Specify rearranging arguments.
  125. * @returns {Function|Object} Returns the converted function or object.
  126. */
  127. function baseConvert(util, name, func, options) {
  128. var isLib = typeof name == 'function',
  129. isObj = name === Object(name);
  130. if (isObj) {
  131. options = func;
  132. func = name;
  133. name = undefined;
  134. }
  135. if (func == null) {
  136. throw new TypeError;
  137. }
  138. options || (options = {});
  139. var config = {
  140. 'cap': 'cap' in options ? options.cap : true,
  141. 'curry': 'curry' in options ? options.curry : true,
  142. 'fixed': 'fixed' in options ? options.fixed : true,
  143. 'immutable': 'immutable' in options ? options.immutable : true,
  144. 'rearg': 'rearg' in options ? options.rearg : true
  145. };
  146. var defaultHolder = isLib ? func : fallbackHolder,
  147. forceCurry = ('curry' in options) && options.curry,
  148. forceFixed = ('fixed' in options) && options.fixed,
  149. forceRearg = ('rearg' in options) && options.rearg,
  150. pristine = isLib ? func.runInContext() : undefined;
  151. var helpers = isLib ? func : {
  152. 'ary': util.ary,
  153. 'assign': util.assign,
  154. 'clone': util.clone,
  155. 'curry': util.curry,
  156. 'forEach': util.forEach,
  157. 'isArray': util.isArray,
  158. 'isError': util.isError,
  159. 'isFunction': util.isFunction,
  160. 'isWeakMap': util.isWeakMap,
  161. 'iteratee': util.iteratee,
  162. 'keys': util.keys,
  163. 'rearg': util.rearg,
  164. 'toInteger': util.toInteger,
  165. 'toPath': util.toPath
  166. };
  167. var ary = helpers.ary,
  168. assign = helpers.assign,
  169. clone = helpers.clone,
  170. curry = helpers.curry,
  171. each = helpers.forEach,
  172. isArray = helpers.isArray,
  173. isError = helpers.isError,
  174. isFunction = helpers.isFunction,
  175. isWeakMap = helpers.isWeakMap,
  176. keys = helpers.keys,
  177. rearg = helpers.rearg,
  178. toInteger = helpers.toInteger,
  179. toPath = helpers.toPath;
  180. var aryMethodKeys = keys(mapping.aryMethod);
  181. var wrappers = {
  182. 'castArray': function(castArray) {
  183. return function() {
  184. var value = arguments[0];
  185. return isArray(value)
  186. ? castArray(cloneArray(value))
  187. : castArray.apply(undefined, arguments);
  188. };
  189. },
  190. 'iteratee': function(iteratee) {
  191. return function() {
  192. var func = arguments[0],
  193. arity = arguments[1],
  194. result = iteratee(func, arity),
  195. length = result.length;
  196. if (config.cap && typeof arity == 'number') {
  197. arity = arity > 2 ? (arity - 2) : 1;
  198. return (length && length <= arity) ? result : baseAry(result, arity);
  199. }
  200. return result;
  201. };
  202. },
  203. 'mixin': function(mixin) {
  204. return function(source) {
  205. var func = this;
  206. if (!isFunction(func)) {
  207. return mixin(func, Object(source));
  208. }
  209. var pairs = [];
  210. each(keys(source), function(key) {
  211. if (isFunction(source[key])) {
  212. pairs.push([key, func.prototype[key]]);
  213. }
  214. });
  215. mixin(func, Object(source));
  216. each(pairs, function(pair) {
  217. var value = pair[1];
  218. if (isFunction(value)) {
  219. func.prototype[pair[0]] = value;
  220. } else {
  221. delete func.prototype[pair[0]];
  222. }
  223. });
  224. return func;
  225. };
  226. },
  227. 'nthArg': function(nthArg) {
  228. return function(n) {
  229. var arity = n < 0 ? 1 : (toInteger(n) + 1);
  230. return curry(nthArg(n), arity);
  231. };
  232. },
  233. 'rearg': function(rearg) {
  234. return function(func, indexes) {
  235. var arity = indexes ? indexes.length : 0;
  236. return curry(rearg(func, indexes), arity);
  237. };
  238. },
  239. 'runInContext': function(runInContext) {
  240. return function(context) {
  241. return baseConvert(util, runInContext(context), options);
  242. };
  243. }
  244. };
  245. /*--------------------------------------------------------------------------*/
  246. /**
  247. * Casts `func` to a function with an arity capped iteratee if needed.
  248. *
  249. * @private
  250. * @param {string} name The name of the function to inspect.
  251. * @param {Function} func The function to inspect.
  252. * @returns {Function} Returns the cast function.
  253. */
  254. function castCap(name, func) {
  255. if (config.cap) {
  256. var indexes = mapping.iterateeRearg[name];
  257. if (indexes) {
  258. return iterateeRearg(func, indexes);
  259. }
  260. var n = !isLib && mapping.iterateeAry[name];
  261. if (n) {
  262. return iterateeAry(func, n);
  263. }
  264. }
  265. return func;
  266. }
  267. /**
  268. * Casts `func` to a curried function if needed.
  269. *
  270. * @private
  271. * @param {string} name The name of the function to inspect.
  272. * @param {Function} func The function to inspect.
  273. * @param {number} n The arity of `func`.
  274. * @returns {Function} Returns the cast function.
  275. */
  276. function castCurry(name, func, n) {
  277. return (forceCurry || (config.curry && n > 1))
  278. ? curry(func, n)
  279. : func;
  280. }
  281. /**
  282. * Casts `func` to a fixed arity function if needed.
  283. *
  284. * @private
  285. * @param {string} name The name of the function to inspect.
  286. * @param {Function} func The function to inspect.
  287. * @param {number} n The arity cap.
  288. * @returns {Function} Returns the cast function.
  289. */
  290. function castFixed(name, func, n) {
  291. if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
  292. var data = mapping.methodSpread[name],
  293. start = data && data.start;
  294. return start === undefined ? ary(func, n) : flatSpread(func, start);
  295. }
  296. return func;
  297. }
  298. /**
  299. * Casts `func` to an rearged function if needed.
  300. *
  301. * @private
  302. * @param {string} name The name of the function to inspect.
  303. * @param {Function} func The function to inspect.
  304. * @param {number} n The arity of `func`.
  305. * @returns {Function} Returns the cast function.
  306. */
  307. function castRearg(name, func, n) {
  308. return (config.rearg && n > 1 && (forceRearg || !mapping.skipRearg[name]))
  309. ? rearg(func, mapping.methodRearg[name] || mapping.aryRearg[n])
  310. : func;
  311. }
  312. /**
  313. * Creates a clone of `object` by `path`.
  314. *
  315. * @private
  316. * @param {Object} object The object to clone.
  317. * @param {Array|string} path The path to clone by.
  318. * @returns {Object} Returns the cloned object.
  319. */
  320. function cloneByPath(object, path) {
  321. path = toPath(path);
  322. var index = -1,
  323. length = path.length,
  324. lastIndex = length - 1,
  325. result = clone(Object(object)),
  326. nested = result;
  327. while (nested != null && ++index < length) {
  328. var key = path[index],
  329. value = nested[key];
  330. if (value != null &&
  331. !(isFunction(value) || isError(value) || isWeakMap(value))) {
  332. nested[key] = clone(index == lastIndex ? value : Object(value));
  333. }
  334. nested = nested[key];
  335. }
  336. return result;
  337. }
  338. /**
  339. * Converts `lodash` to an immutable auto-curried iteratee-first data-last
  340. * version with conversion `options` applied.
  341. *
  342. * @param {Object} [options] The options object. See `baseConvert` for more details.
  343. * @returns {Function} Returns the converted `lodash`.
  344. */
  345. function convertLib(options) {
  346. return _.runInContext.convert(options)(undefined);
  347. }
  348. /**
  349. * Create a converter function for `func` of `name`.
  350. *
  351. * @param {string} name The name of the function to convert.
  352. * @param {Function} func The function to convert.
  353. * @returns {Function} Returns the new converter function.
  354. */
  355. function createConverter(name, func) {
  356. var realName = mapping.aliasToReal[name] || name,
  357. methodName = mapping.remap[realName] || realName,
  358. oldOptions = options;
  359. return function(options) {
  360. var newUtil = isLib ? pristine : helpers,
  361. newFunc = isLib ? pristine[methodName] : func,
  362. newOptions = assign(assign({}, oldOptions), options);
  363. return baseConvert(newUtil, realName, newFunc, newOptions);
  364. };
  365. }
  366. /**
  367. * Creates a function that wraps `func` to invoke its iteratee, with up to `n`
  368. * arguments, ignoring any additional arguments.
  369. *
  370. * @private
  371. * @param {Function} func The function to cap iteratee arguments for.
  372. * @param {number} n The arity cap.
  373. * @returns {Function} Returns the new function.
  374. */
  375. function iterateeAry(func, n) {
  376. return overArg(func, function(func) {
  377. return typeof func == 'function' ? baseAry(func, n) : func;
  378. });
  379. }
  380. /**
  381. * Creates a function that wraps `func` to invoke its iteratee with arguments
  382. * arranged according to the specified `indexes` where the argument value at
  383. * the first index is provided as the first argument, the argument value at
  384. * the second index is provided as the second argument, and so on.
  385. *
  386. * @private
  387. * @param {Function} func The function to rearrange iteratee arguments for.
  388. * @param {number[]} indexes The arranged argument indexes.
  389. * @returns {Function} Returns the new function.
  390. */
  391. function iterateeRearg(func, indexes) {
  392. return overArg(func, function(func) {
  393. var n = indexes.length;
  394. return baseArity(rearg(baseAry(func, n), indexes), n);
  395. });
  396. }
  397. /**
  398. * Creates a function that invokes `func` with its first argument transformed.
  399. *
  400. * @private
  401. * @param {Function} func The function to wrap.
  402. * @param {Function} transform The argument transform.
  403. * @returns {Function} Returns the new function.
  404. */
  405. function overArg(func, transform) {
  406. return function() {
  407. var length = arguments.length;
  408. if (!length) {
  409. return func();
  410. }
  411. var args = Array(length);
  412. while (length--) {
  413. args[length] = arguments[length];
  414. }
  415. var index = config.rearg ? 0 : (length - 1);
  416. args[index] = transform(args[index]);
  417. return func.apply(undefined, args);
  418. };
  419. }
  420. /**
  421. * Creates a function that wraps `func` and applys the conversions
  422. * rules by `name`.
  423. *
  424. * @private
  425. * @param {string} name The name of the function to wrap.
  426. * @param {Function} func The function to wrap.
  427. * @returns {Function} Returns the converted function.
  428. */
  429. function wrap(name, func, placeholder) {
  430. var result,
  431. realName = mapping.aliasToReal[name] || name,
  432. wrapped = func,
  433. wrapper = wrappers[realName];
  434. if (wrapper) {
  435. wrapped = wrapper(func);
  436. }
  437. else if (config.immutable) {
  438. if (mapping.mutate.array[realName]) {
  439. wrapped = wrapImmutable(func, cloneArray);
  440. }
  441. else if (mapping.mutate.object[realName]) {
  442. wrapped = wrapImmutable(func, createCloner(func));
  443. }
  444. else if (mapping.mutate.set[realName]) {
  445. wrapped = wrapImmutable(func, cloneByPath);
  446. }
  447. }
  448. each(aryMethodKeys, function(aryKey) {
  449. each(mapping.aryMethod[aryKey], function(otherName) {
  450. if (realName == otherName) {
  451. var data = mapping.methodSpread[realName],
  452. afterRearg = data && data.afterRearg;
  453. result = afterRearg
  454. ? castFixed(realName, castRearg(realName, wrapped, aryKey), aryKey)
  455. : castRearg(realName, castFixed(realName, wrapped, aryKey), aryKey);
  456. result = castCap(realName, result);
  457. result = castCurry(realName, result, aryKey);
  458. return false;
  459. }
  460. });
  461. return !result;
  462. });
  463. result || (result = wrapped);
  464. if (result == func) {
  465. result = forceCurry ? curry(result, 1) : function() {
  466. return func.apply(this, arguments);
  467. };
  468. }
  469. result.convert = createConverter(realName, func);
  470. result.placeholder = func.placeholder = placeholder;
  471. return result;
  472. }
  473. /*--------------------------------------------------------------------------*/
  474. if (!isObj) {
  475. return wrap(name, func, defaultHolder);
  476. }
  477. var _ = func;
  478. // Convert methods by ary cap.
  479. var pairs = [];
  480. each(aryMethodKeys, function(aryKey) {
  481. each(mapping.aryMethod[aryKey], function(key) {
  482. var func = _[mapping.remap[key] || key];
  483. if (func) {
  484. pairs.push([key, wrap(key, func, _)]);
  485. }
  486. });
  487. });
  488. // Convert remaining methods.
  489. each(keys(_), function(key) {
  490. var func = _[key];
  491. if (typeof func == 'function') {
  492. var length = pairs.length;
  493. while (length--) {
  494. if (pairs[length][0] == key) {
  495. return;
  496. }
  497. }
  498. func.convert = createConverter(key, func);
  499. pairs.push([key, func]);
  500. }
  501. });
  502. // Assign to `_` leaving `_.prototype` unchanged to allow chaining.
  503. each(pairs, function(pair) {
  504. _[pair[0]] = pair[1];
  505. });
  506. _.convert = convertLib;
  507. _.placeholder = _;
  508. // Assign aliases.
  509. each(keys(_), function(key) {
  510. each(mapping.realToAlias[key] || [], function(alias) {
  511. _[alias] = _[key];
  512. });
  513. });
  514. return _;
  515. }
  516. module.exports = baseConvert;