1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.combine = exports.divide = exports.unique = exports.isFlatArray = exports.lastOf = exports.firstOf = exports.transpose = exports.indexOf = exports.mapObject = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | function mapObject(object, callbackfn) {
|
9 | return Object.entries(object).reduce((obj, [key, value]) => {
|
10 | obj[key] = callbackfn(value, key, object);
|
11 | return obj;
|
12 | }, {});
|
13 | }
|
14 | exports.mapObject = mapObject;
|
15 | function indexOf(array) {
|
16 | return array.map((_, i) => i);
|
17 | }
|
18 | exports.indexOf = indexOf;
|
19 |
|
20 |
|
21 |
|
22 | function transpose(matrix) {
|
23 | const row = matrix.length;
|
24 | const col = matrix[0].length;
|
25 |
|
26 |
|
27 | const transposed = new Array(col).fill(0).map(() => new Array(row));
|
28 | for (let i = 0; i < col; i++) {
|
29 | for (let j = 0; j < row; j++) {
|
30 | transposed[i][j] = matrix[j][i];
|
31 | }
|
32 | }
|
33 | return transposed;
|
34 | }
|
35 | exports.transpose = transpose;
|
36 | function firstOf(array) {
|
37 | return array[0];
|
38 | }
|
39 | exports.firstOf = firstOf;
|
40 | function lastOf(array) {
|
41 | return array[array.length - 1];
|
42 | }
|
43 | exports.lastOf = lastOf;
|
44 | function isFlatArray(array) {
|
45 | return !array.some(Array.isArray);
|
46 | }
|
47 | exports.isFlatArray = isFlatArray;
|
48 | function unique(array) {
|
49 | return Array.from(new Set(array));
|
50 | }
|
51 | exports.unique = unique;
|
52 | function divide(array, callbackfn) {
|
53 | const result = [[], []];
|
54 | array.forEach((item) => {
|
55 | result[callbackfn(item) ? 0 : 1].push(item);
|
56 | });
|
57 | return result;
|
58 | }
|
59 | exports.divide = divide;
|
60 | function comb(array, len = array.length) {
|
61 | if (len === 1)
|
62 | return array.map((item) => [item]);
|
63 | const result = [];
|
64 | for (let i = 0; i < array.length; i++) {
|
65 | const rest = array.slice(i + 1);
|
66 | const restComb = comb(rest, len - 1);
|
67 | restComb.forEach((comb) => {
|
68 | result.push([array[i], ...comb]);
|
69 | });
|
70 | }
|
71 | return result;
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function combine(array) {
|
80 | if (array.length === 1)
|
81 | return [array];
|
82 | const result = [];
|
83 | for (let i = 1; i <= array.length; i++) {
|
84 | result.push(...comb(array, i));
|
85 | }
|
86 | return result;
|
87 | }
|
88 | exports.combine = combine;
|
89 |
|
\ | No newline at end of file |