-
Notifications
You must be signed in to change notification settings - Fork 1
/
concat.js
40 lines (34 loc) 路 1.03 KB
/
concat.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
import isArray from './isArray'
import isString from './isString'
/**
* Returns the result of concatenating the given lists or strings.
*
* @param {Array|String} f The first list or string.
* @param {Array|String} s The second list or string.
* @return {Array|String} A list or string consisting of the elements of `f`
* followed by the elements of `s`.
* @example
*
* concat('ABC', 'DEF') // "ABCDEF"
* concat([1, 2, 3], [4, 5, 6]) // [1, 2, 3, 4, 5, 6]
* concat('', '') // ""
* concat([], []) // []
* concat({}, '123') // TypeError: [object Object] does not have a method named "concat"
*
**/
const concat = (f, s) => {
if (isArray(f)) {
if (isArray(s)) {
return f.concat(s)
}
throw new TypeError(s.toString() + ' is not an Array')
}
if (isString(f)) {
if (isString(s)) {
return f + s
}
throw new TypeError(s.toString() + ' is not a String')
}
throw new TypeError(f.toString() + ' does not have a method named "concat"')
}
export default concat