File size: 6,415 Bytes
8a37e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { moveOneToEnd, moveOneToStart, moveToEnd, moveToStart } from 'common/util/arrayUtils';
import { describe, expect, it } from 'vitest';

describe('Array Manipulation Functions', () => {
  const originalArray = ['a', 'b', 'c', 'd'];

  describe('moveOneToEnd', () => {
    describe('with callback', () => {
      it('should move an item forward by one position', () => {
        const array = [...originalArray];
        const result = moveOneToEnd(array, (item) => item === 'b');
        expect(result).toEqual(['a', 'c', 'b', 'd']);
      });

      it('should do nothing if the item is at the end', () => {
        const array = [...originalArray];
        const result = moveOneToEnd(array, (item) => item === 'd');
        expect(result).toEqual(['a', 'b', 'c', 'd']);
      });

      it("should leave the array unchanged if the item isn't in the array", () => {
        const array = [...originalArray];
        const result = moveOneToEnd(array, (item) => item === 'z');
        expect(result).toEqual(originalArray);
      });
    });
    describe('with item', () => {
      it('should move an item forward by one position', () => {
        const array = [...originalArray];
        const result = moveOneToEnd(array, (item) => item === 'b');
        expect(result).toEqual(['a', 'c', 'b', 'd']);
      });

      it('should do nothing if the item is at the end', () => {
        const array = [...originalArray];
        const result = moveOneToEnd(array, (item) => item === 'd');
        expect(result).toEqual(['a', 'b', 'c', 'd']);
      });

      it("should leave the array unchanged if the item isn't in the array", () => {
        const array = [...originalArray];
        const result = moveOneToEnd(array, (item) => item === 'z');
        expect(result).toEqual(originalArray);
      });
    });
  });

  describe('moveToStart', () => {
    describe('with callback', () => {
      it('should move an item to the front', () => {
        const array = [...originalArray];
        const result = moveToStart(array, (item) => item === 'c');
        expect(result).toEqual(['c', 'a', 'b', 'd']);
      });

      it('should do nothing if the item is already at the front', () => {
        const array = [...originalArray];
        const result = moveToStart(array, (item) => item === 'a');
        expect(result).toEqual(['a', 'b', 'c', 'd']);
      });

      it("should leave the array unchanged if the item isn't in the array", () => {
        const array = [...originalArray];
        const result = moveToStart(array, (item) => item === 'z');
        expect(result).toEqual(originalArray);
      });
    });
    describe('with item', () => {
      it('should move an item to the front', () => {
        const array = [...originalArray];
        const result = moveToStart(array, 'c');
        expect(result).toEqual(['c', 'a', 'b', 'd']);
      });

      it('should do nothing if the item is already at the front', () => {
        const array = [...originalArray];
        const result = moveToStart(array, 'a');
        expect(result).toEqual(['a', 'b', 'c', 'd']);
      });

      it("should leave the array unchanged if the item isn't in the array", () => {
        const array = [...originalArray];
        const result = moveToStart(array, 'z');
        expect(result).toEqual(originalArray);
      });
    });
  });

  describe('moveOneToStart', () => {
    describe('with callback', () => {
      it('should move an item backward by one position', () => {
        const array = [...originalArray];
        const result = moveOneToStart(array, (item) => item === 'c');
        expect(result).toEqual(['a', 'c', 'b', 'd']);
      });

      it('should do nothing if the item is at the beginning', () => {
        const array = [...originalArray];
        const result = moveOneToStart(array, (item) => item === 'a');
        expect(result).toEqual(['a', 'b', 'c', 'd']);
      });

      it("should leave the array unchanged if the item isn't in the array", () => {
        const array = [...originalArray];
        const result = moveOneToStart(array, (item) => item === 'z');
        expect(result).toEqual(originalArray);
      });
    });
    describe('with item', () => {
      it('should move an item backward by one position', () => {
        const array = [...originalArray];
        const result = moveOneToStart(array, 'c');
        expect(result).toEqual(['a', 'c', 'b', 'd']);
      });

      it('should do nothing if the item is at the beginning', () => {
        const array = [...originalArray];
        const result = moveOneToStart(array, 'a');
        expect(result).toEqual(['a', 'b', 'c', 'd']);
      });

      it("should leave the array unchanged if the item isn't in the array", () => {
        const array = [...originalArray];
        const result = moveOneToStart(array, 'z');
        expect(result).toEqual(originalArray);
      });
    });
  });

  describe('moveToEnd', () => {
    describe('with callback', () => {
      it('should move an item to the back', () => {
        const array = [...originalArray];
        const result = moveToEnd(array, (item) => item === 'b');
        expect(result).toEqual(['a', 'c', 'd', 'b']);
      });

      it('should do nothing if the item is already at the back', () => {
        const array = [...originalArray];
        const result = moveToEnd(array, (item) => item === 'd');
        expect(result).toEqual(['a', 'b', 'c', 'd']);
      });

      it("should leave the array unchanged if the item isn't in the array", () => {
        const array = [...originalArray];
        const result = moveToEnd(array, (item) => item === 'z');
        expect(result).toEqual(originalArray);
      });
    });
    describe('with item', () => {
      it('should move an item to the back', () => {
        const array = [...originalArray];
        const result = moveToEnd(array, 'b');
        expect(result).toEqual(['a', 'c', 'd', 'b']);
      });

      it('should do nothing if the item is already at the back', () => {
        const array = [...originalArray];
        const result = moveToEnd(array, 'd');
        expect(result).toEqual(['a', 'b', 'c', 'd']);
      });

      it("should leave the array unchanged if the item isn't in the array", () => {
        const array = [...originalArray];
        const result = moveToEnd(array, 'z');
        expect(result).toEqual(originalArray);
      });
    });
  });
});