source: node_modules/ramda/src/swap.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[d24f17c]1var _curry3 =
2/*#__PURE__*/
3require("./internal/_curry3.js");
4
5var _isArray =
6/*#__PURE__*/
7require("./internal/_isArray.js");
8
9var _isString =
10/*#__PURE__*/
11require("./internal/_isString.js");
12
13var clone =
14/*#__PURE__*/
15require("./clone.js");
16
17var 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
30var 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
54var 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
80var 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
92module.exports = swap;
Note: See TracBrowser for help on using the repository browser.