@util.js/arrays 0.41.6 → 0.41.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Arrays.js +117 -0
- package/package.json +5 -1
- package/lib/Arrays.js +0 -99
- package/test/ArraysTest.js +0 -45
package/dist/Arrays.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* JavaScript utility methods for [arrays]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array}
|
|
5
|
+
* @public
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
9
|
+
var _from = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/from"));
|
|
10
|
+
var _isArray = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/is-array"));
|
|
11
|
+
var _of = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/of"));
|
|
12
|
+
var _sort = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/sort"));
|
|
13
|
+
var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
|
|
14
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
|
|
15
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
|
|
16
|
+
var Arrays = /*#__PURE__*/function () {
|
|
17
|
+
function Arrays() {
|
|
18
|
+
(0, _classCallCheck2["default"])(this, Arrays);
|
|
19
|
+
}
|
|
20
|
+
return (0, _createClass2["default"])(Arrays, [{
|
|
21
|
+
key: "from",
|
|
22
|
+
value:
|
|
23
|
+
/**
|
|
24
|
+
* Creates a new, shallow-copied Array instance from an array-like or iterable object.
|
|
25
|
+
*
|
|
26
|
+
* See MDN's documentation about [from]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from}.
|
|
27
|
+
*
|
|
28
|
+
* @param arrayLike An array-like or iterable object to convert to an array
|
|
29
|
+
* @param {Function} [mapFn] Map function to call on every element of the array
|
|
30
|
+
* @param {Object} [thisArg] Value to use as this when executing mapFn
|
|
31
|
+
* @return {Array} A new Array instance
|
|
32
|
+
* @public
|
|
33
|
+
* @instance
|
|
34
|
+
* @function
|
|
35
|
+
*/
|
|
36
|
+
function from() {
|
|
37
|
+
return _from["default"].apply(Array, arguments);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Determines whether the passed value is an Array.
|
|
42
|
+
*
|
|
43
|
+
* See MDN's documentation about [isArray]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray}.
|
|
44
|
+
*
|
|
45
|
+
* @param {*} obj The object to be checked
|
|
46
|
+
* @return {boolean} true if the object is an Array or false otherwise
|
|
47
|
+
* @public
|
|
48
|
+
* @instance
|
|
49
|
+
* @function
|
|
50
|
+
*/
|
|
51
|
+
}, {
|
|
52
|
+
key: "isArray",
|
|
53
|
+
value: function isArray() {
|
|
54
|
+
return _isArray["default"].apply(Array, arguments);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments
|
|
59
|
+
*
|
|
60
|
+
* See MDN's documentation about [of]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of}.
|
|
61
|
+
*
|
|
62
|
+
* @param {...*} elementN Elements of which to create the array
|
|
63
|
+
* @return {Array} A new Array instance
|
|
64
|
+
* @public
|
|
65
|
+
* @instance
|
|
66
|
+
* @function
|
|
67
|
+
*/
|
|
68
|
+
}, {
|
|
69
|
+
key: "of",
|
|
70
|
+
value: function of() {
|
|
71
|
+
return _of["default"].apply(Array, arguments);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Shuffles the elements of the specified array.
|
|
76
|
+
*
|
|
77
|
+
* See [knuth-shuffle]{@link https://www.npmjs.com/package/knuth-shuffle}.
|
|
78
|
+
*
|
|
79
|
+
* @param {Array} array The array to shuffle
|
|
80
|
+
* @return {Array} A new Array instance with shuffled elements
|
|
81
|
+
* @public
|
|
82
|
+
* @instance
|
|
83
|
+
* @function
|
|
84
|
+
*/
|
|
85
|
+
}, {
|
|
86
|
+
key: "shuffle",
|
|
87
|
+
value: function shuffle(array) {
|
|
88
|
+
return knuthshuffle().knuthShuffle(this.from(array));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Sorts the elements of an array and returns a **new array** with the sorted elements. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
|
|
93
|
+
*
|
|
94
|
+
* See MDN's documentation about [sort]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort}.
|
|
95
|
+
*
|
|
96
|
+
* @param {Array} array The array to sort
|
|
97
|
+
* @return {Array} A new Array instance with sorted elements
|
|
98
|
+
* @public
|
|
99
|
+
* @instance
|
|
100
|
+
* @function
|
|
101
|
+
*/
|
|
102
|
+
}, {
|
|
103
|
+
key: "sort",
|
|
104
|
+
value: function sort(array, compareFunction) {
|
|
105
|
+
var _context;
|
|
106
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
|
107
|
+
// https://stackoverflow.com/questions/9592740/how-can-you-sort-an-array-without-mutating-the-original-array
|
|
108
|
+
// https://stackoverflow.com/a/9592755
|
|
109
|
+
return (0, _sort["default"])(_context = (0, _slice["default"])(Array.prototype).call(array)).call(_context, compareFunction);
|
|
110
|
+
}
|
|
111
|
+
}]);
|
|
112
|
+
}();
|
|
113
|
+
module.exports = Arrays;
|
|
114
|
+
var dependencies = {};
|
|
115
|
+
function knuthshuffle() {
|
|
116
|
+
return dependencies["knuth-shuffle"] || (dependencies["knuth-shuffle"] = require("knuth-shuffle"));
|
|
117
|
+
}
|
package/package.json
CHANGED
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
"work in https://npm.runkit.com."
|
|
10
10
|
],
|
|
11
11
|
"description": "JavaScript utility methods for arrays",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"index.js"
|
|
15
|
+
],
|
|
12
16
|
"keywords": [
|
|
13
17
|
"array",
|
|
14
18
|
"arrays",
|
|
@@ -30,5 +34,5 @@
|
|
|
30
34
|
"type": "git",
|
|
31
35
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-arrays"
|
|
32
36
|
},
|
|
33
|
-
"version": "0.41.
|
|
37
|
+
"version": "0.41.8"
|
|
34
38
|
}
|
package/lib/Arrays.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* JavaScript utility methods for [arrays]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array}
|
|
5
|
-
* @public
|
|
6
|
-
* @class
|
|
7
|
-
*/
|
|
8
|
-
class Arrays {
|
|
9
|
-
/**
|
|
10
|
-
* Creates a new, shallow-copied Array instance from an array-like or iterable object.
|
|
11
|
-
*
|
|
12
|
-
* See MDN's documentation about [from]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from}.
|
|
13
|
-
*
|
|
14
|
-
* @param arrayLike An array-like or iterable object to convert to an array
|
|
15
|
-
* @param {Function} [mapFn] Map function to call on every element of the array
|
|
16
|
-
* @param {Object} [thisArg] Value to use as this when executing mapFn
|
|
17
|
-
* @return {Array} A new Array instance
|
|
18
|
-
* @public
|
|
19
|
-
* @instance
|
|
20
|
-
* @function
|
|
21
|
-
*/
|
|
22
|
-
from() {
|
|
23
|
-
return Array.from(...arguments);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Determines whether the passed value is an Array.
|
|
28
|
-
*
|
|
29
|
-
* See MDN's documentation about [isArray]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray}.
|
|
30
|
-
*
|
|
31
|
-
* @param {*} obj The object to be checked
|
|
32
|
-
* @return {boolean} true if the object is an Array or false otherwise
|
|
33
|
-
* @public
|
|
34
|
-
* @instance
|
|
35
|
-
* @function
|
|
36
|
-
*/
|
|
37
|
-
isArray() {
|
|
38
|
-
return Array.isArray(...arguments);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments
|
|
43
|
-
*
|
|
44
|
-
* See MDN's documentation about [of]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of}.
|
|
45
|
-
*
|
|
46
|
-
* @param {...*} elementN Elements of which to create the array
|
|
47
|
-
* @return {Array} A new Array instance
|
|
48
|
-
* @public
|
|
49
|
-
* @instance
|
|
50
|
-
* @function
|
|
51
|
-
*/
|
|
52
|
-
of() {
|
|
53
|
-
return Array.of(...arguments);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Shuffles the elements of the specified array.
|
|
58
|
-
*
|
|
59
|
-
* See [knuth-shuffle]{@link https://www.npmjs.com/package/knuth-shuffle}.
|
|
60
|
-
*
|
|
61
|
-
* @param {Array} array The array to shuffle
|
|
62
|
-
* @return {Array} A new Array instance
|
|
63
|
-
* @public
|
|
64
|
-
* @instance
|
|
65
|
-
* @function
|
|
66
|
-
*/
|
|
67
|
-
shuffle(array) {
|
|
68
|
-
return knuthshuffle().knuthShuffle(this.from(array));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Sorts the elements of an array and (unlike returns a **new array** with the sorted elements. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
|
|
73
|
-
*
|
|
74
|
-
* See [knuth-shuffle]{@link https://www.npmjs.com/package/knuth-shuffle}.
|
|
75
|
-
*
|
|
76
|
-
* @param {Array} array The array to shuffle
|
|
77
|
-
* @return {Array} A new Array instance
|
|
78
|
-
* @public
|
|
79
|
-
* @instance
|
|
80
|
-
* @function
|
|
81
|
-
*/
|
|
82
|
-
sort(array, compareFunction) {
|
|
83
|
-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
|
84
|
-
// https://stackoverflow.com/questions/9592740/how-can-you-sort-an-array-without-mutating-the-original-array
|
|
85
|
-
// https://stackoverflow.com/a/9592755
|
|
86
|
-
return Array.prototype.slice.call(array).sort(compareFunction);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
module.exports = Arrays;
|
|
91
|
-
|
|
92
|
-
const dependencies = {};
|
|
93
|
-
|
|
94
|
-
function knuthshuffle() {
|
|
95
|
-
return (
|
|
96
|
-
dependencies["knuth-shuffle"] ||
|
|
97
|
-
(dependencies["knuth-shuffle"] = require("knuth-shuffle"))
|
|
98
|
-
);
|
|
99
|
-
}
|
package/test/ArraysTest.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const arrays = require("..");
|
|
4
|
-
const { expect } = require("chai");
|
|
5
|
-
|
|
6
|
-
describe("Arrays", () => {
|
|
7
|
-
describe("#from", () => {
|
|
8
|
-
it("should create a shallow copy", () => {
|
|
9
|
-
const a = [1, 2, 3];
|
|
10
|
-
const b = arrays.from(a);
|
|
11
|
-
expect(b === a).to.be.false;
|
|
12
|
-
expect(b[0]).to.eql(1);
|
|
13
|
-
expect(b[1]).to.eql(2);
|
|
14
|
-
expect(b[2]).to.eql(3);
|
|
15
|
-
expect(b.length).to.eql(3);
|
|
16
|
-
});
|
|
17
|
-
it("should throw an error an undefined or null input", () => {
|
|
18
|
-
expect(() => arrays.from()).to.throw(Error);
|
|
19
|
-
expect(() => arrays.from(null)).to.throw(Error);
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
describe("#sort", () => {
|
|
24
|
-
it("should sort an array", () => {
|
|
25
|
-
const a = [2, 1, 3];
|
|
26
|
-
const b = arrays.sort(a);
|
|
27
|
-
expect(b).to.not.eql(a);
|
|
28
|
-
expect(b).to.eql([1, 2, 3]);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
describe("#shuffle", () => {
|
|
33
|
-
it("should shuffle array contents", () => {
|
|
34
|
-
const a = [1, 2, 3];
|
|
35
|
-
const b = arrays.shuffle(a);
|
|
36
|
-
expect(b === a).to.be.false;
|
|
37
|
-
expect(b.length).to.eql(3);
|
|
38
|
-
// console.log(b)
|
|
39
|
-
});
|
|
40
|
-
it("should throw an error an undefined or null input", () => {
|
|
41
|
-
expect(() => arrays.shuffle()).to.throw(Error);
|
|
42
|
-
expect(() => arrays.shuffle(null)).to.throw(Error);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
});
|