cafe-utility 4.17.0 → 4.18.0

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.
Files changed (3) hide show
  1. package/index.d.ts +3 -1
  2. package/index.js +17 -5
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -12,7 +12,8 @@ declare function onlyOrThrow<T>(array: T[]): T;
12
12
  declare function onlyOrNull<T>(array: T[]): T | null;
13
13
  declare function firstOrNull<T>(array: T[]): T | null;
14
14
  declare function initializeArray<T>(count: number, initializer: (index: number) => T): T[];
15
- declare function initialize2DArray<T>(x: number, y: number, initialValue: T): T[][];
15
+ declare function rotate2DArray<T>(array: T[][]): T[][];
16
+ declare function initialize2DArray<T>(width: number, height: number, initialValue: T): T[][];
16
17
  declare function containsShape<T>(array2D: T[][], shape: T[][], x: number, y: number): boolean;
17
18
  declare function takeRandomly<T>(array: T[], count: number): T[];
18
19
  declare function pickRandomIndices<T>(array: T[], count: number): number[];
@@ -312,6 +313,7 @@ export declare const Arrays: {
312
313
  pickRandomIndices: typeof pickRandomIndices;
313
314
  initialize: typeof initializeArray;
314
315
  initialize2D: typeof initialize2DArray;
316
+ rotate2D: typeof rotate2DArray;
315
317
  containsShape: typeof containsShape;
316
318
  glue: typeof glue;
317
319
  pluck: typeof pluck;
package/index.js CHANGED
@@ -70,11 +70,22 @@ function initializeArray(count, initializer) {
70
70
  return results
71
71
  }
72
72
 
73
- function initialize2DArray(x, y, initialValue) {
73
+ function rotate2DArray(array) {
74
+ const newArray = []
75
+ for (let i = 0; i < array[0].length; i++) {
76
+ newArray.push([])
77
+ for (let j = 0; j < array.length; j++) {
78
+ newArray[i].push(array[j][i])
79
+ }
80
+ }
81
+ return newArray
82
+ }
83
+
84
+ function initialize2DArray(width, height, initialValue) {
74
85
  const array = []
75
- for (let i = 0; i < x; i++) {
86
+ for (let i = 0; i < width; i++) {
76
87
  array.push([])
77
- for (let j = 0; j < y; j++) {
88
+ for (let j = 0; j < height; j++) {
78
89
  array[i].push(initialValue)
79
90
  }
80
91
  }
@@ -82,7 +93,7 @@ function initialize2DArray(x, y, initialValue) {
82
93
  }
83
94
 
84
95
  function containsShape(array2D, shape, x, y) {
85
- if (x < 0 || y < 0 || y + shape.length - 1 > array2D.length || x + shape[0].length - 1 > array2D[0].length) {
96
+ if (x < 0 || y < 0 || y + shape[0].length - 1 > array2D[0].length || x + shape.length - 1 > array2D.length) {
86
97
  return false
87
98
  }
88
99
  for (let i = 0; i < shape.length; i++) {
@@ -90,7 +101,7 @@ function containsShape(array2D, shape, x, y) {
90
101
  if (shape[i][j] === undefined) {
91
102
  continue
92
103
  }
93
- if (array2D[i + y][j + x] !== shape[i][j]) {
104
+ if (array2D[x + i][y + j] !== shape[i][j]) {
94
105
  return false
95
106
  }
96
107
  }
@@ -1827,6 +1838,7 @@ exports.Arrays = {
1827
1838
  pickRandomIndices,
1828
1839
  initialize: initializeArray,
1829
1840
  initialize2D: initialize2DArray,
1841
+ rotate2D: rotate2DArray,
1830
1842
  containsShape,
1831
1843
  glue,
1832
1844
  pluck,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "4.17.0",
3
+ "version": "4.18.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {