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