forked from buxlabs/pure-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.js
45 lines (37 loc) · 1.16 KB
/
collection.js
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
function append (collection, ...args) {
if (args.length === 0) {
return collection
}
if (typeof collection === 'string') {
return collection + args.join('')
} else if (Array.isArray(collection)) {
return [...collection, ...args]
}
throw new TypeError("[ERROR]: 'append' filter processes only strings or arrays")
}
function prepend (collection, ...args) {
if (args.length === 0) {
return collection
}
if (typeof collection === 'string') {
return args.join('') + collection
} else if (Array.isArray(collection)) {
return [...args, ...collection]
}
throw new TypeError("[ERROR]: 'prepend' filter processes only strings or arrays")
}
function reverse (collection) {
return Array.isArray(collection) ? collection.reverse() : [...collection].reverse().join('')
}
function size (collection) {
const type = Object.prototype.toString.call(collection).substr(8).replace(']', '')
if (type === 'Array' || type === 'String') return collection.length
if (type === 'Object') return Object.keys(collection).length
if (type === 'Map' || type === 'Set') return collection.size
}
module.exports = {
append,
prepend,
reverse,
size
}