Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/array-back/dist/index.js
1126 views
1
(function (global, factory) {
2
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3
typeof define === 'function' && define.amd ? define(factory) :
4
(global = global || self, global.arrayBack = factory());
5
}(this, function () { 'use strict';
6
7
/**
8
* Takes any input and guarantees an array back.
9
*
10
* - Converts array-like objects (e.g. `arguments`, `Set`) to a real array.
11
* - Converts `undefined` to an empty array.
12
* - Converts any another other, singular value (including `null`, objects and iterables other than `Set`) into an array containing that value.
13
* - Ignores input which is already an array.
14
*
15
* @module array-back
16
* @example
17
* > const arrayify = require('array-back')
18
*
19
* > arrayify(undefined)
20
* []
21
*
22
* > arrayify(null)
23
* [ null ]
24
*
25
* > arrayify(0)
26
* [ 0 ]
27
*
28
* > arrayify([ 1, 2 ])
29
* [ 1, 2 ]
30
*
31
* > arrayify(new Set([ 1, 2 ]))
32
* [ 1, 2 ]
33
*
34
* > function f(){ return arrayify(arguments); }
35
* > f(1,2,3)
36
* [ 1, 2, 3 ]
37
*/
38
39
function isObject (input) {
40
return typeof input === 'object' && input !== null
41
}
42
43
function isArrayLike (input) {
44
return isObject(input) && typeof input.length === 'number'
45
}
46
47
/**
48
* @param {*} - The input value to convert to an array
49
* @returns {Array}
50
* @alias module:array-back
51
*/
52
function arrayify (input) {
53
if (Array.isArray(input)) {
54
return input
55
}
56
57
if (input === undefined) {
58
return []
59
}
60
61
if (isArrayLike(input) || input instanceof Set) {
62
return Array.from(input)
63
}
64
65
return [ input ]
66
}
67
68
return arrayify;
69
70
}));
71
72