forked from Tinkoff/utils.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse.ts
More file actions
27 lines (25 loc) · 731 Bytes
/
reverse.ts
File metadata and controls
27 lines (25 loc) · 731 Bytes
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
import { reverse } from '../typings/types';
import isString from '../is/string';
/**
* Returns a new list or string with the elements or characters in reverse
* order.
*
* @param {Array|String} list
* @return {Array|String}
* @example
*
* reverse([1, 2, 3]); //=> [3, 2, 1]
* reverse([1, 2]); //=> [2, 1]
* reverse([1]); //=> [1]
* reverse([]); //=> []
*
* reverse('abc'); //=> 'cba'
* reverse('ab'); //=> 'ba'
* reverse('a'); //=> 'a'
* reverse(''); //=> ''
*/
export default (
list => isString(list) ?
list.split('').reverse().join('') :
Array.prototype.slice.call(list, 0).reverse()
) as typeof reverse