swap.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import _curry3 from "./internal/_curry3.js";
  2. import _isArray from "./internal/_isArray.js";
  3. import _isString from "./internal/_isString.js";
  4. import clone from "./clone.js";
  5. var swapObject = function (indexA, indexB, o) {
  6. var copy = clone(o);
  7. var properties = Object.getOwnPropertyNames(copy);
  8. if (properties.includes(indexA) && properties.includes(indexB)) {
  9. var tmp = copy[indexA];
  10. copy[indexA] = copy[indexB];
  11. copy[indexB] = tmp;
  12. }
  13. return copy;
  14. };
  15. var swapList = function (indexA, indexB, list) {
  16. var length = list.length;
  17. var result = list.slice();
  18. var positiveIndexA = indexA < 0 ? length + indexA : indexA;
  19. var positiveIndexB = indexB < 0 ? length + indexB : indexB;
  20. var positiveMin = Math.min(positiveIndexA, positiveIndexB);
  21. var positiveMax = Math.max(positiveIndexA, positiveIndexB);
  22. if (positiveIndexA < 0 || positiveIndexA > length) {
  23. return result;
  24. }
  25. if (positiveIndexB < 0 || positiveIndexB > length) {
  26. return result;
  27. }
  28. if (positiveIndexA === positiveIndexB) {
  29. return result;
  30. }
  31. result = [].concat(result.slice(0, positiveMin)).concat(result[positiveMax]).concat(result.slice(positiveMin + 1, positiveMax)).concat(result[positiveMin]).concat(result.slice(positiveMax + 1, length));
  32. return result;
  33. };
  34. var swapString = function (indexA, indexB, s) {
  35. var result = swapList(indexA, indexB, s);
  36. return _isArray(result) ? result.join('') : result;
  37. };
  38. /**
  39. * Swap an item, at index `indexA` with another item, at index `indexB`, in an object or a list of elements.
  40. * A new result will be created containing the new elements order.
  41. *
  42. * @func
  43. * @memberOf R
  44. * @since v0.29.0
  45. * @category List
  46. * @sig Number -> Number -> [a] -> [a]
  47. * @param {Number|string|Object} indexA The first index
  48. * @param {Number|string|Object} indexB The second index
  49. * @param {Array|Object} o Either the object or list which will serve to realise the swap
  50. * @return {Array|Object} The new object or list reordered
  51. * @example
  52. *
  53. * R.swap(0, 2, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['c', 'b', 'a', 'd', 'e', 'f']
  54. * R.swap(-1, 0, ['a', 'b', 'c', 'd', 'e', 'f']); //=> ['f', 'b', 'c', 'd', 'e', 'a'] list rotation
  55. * R.swap('a', 'b', {a: 1, b: 2}); //=> {a: 2, b: 2}
  56. * R.swap(0, 2, 'foo'); //=> 'oof'
  57. */
  58. var swap =
  59. /*#__PURE__*/
  60. _curry3(function (indexA, indexB, o) {
  61. if (_isArray(o)) {
  62. return swapList(indexA, indexB, o);
  63. } else if (_isString(o)) {
  64. return swapString(indexA, indexB, o);
  65. } else {
  66. return swapObject(indexA, indexB, o);
  67. }
  68. });
  69. export default swap;