cafe-utility 4.15.0 → 4.17.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 +4 -0
  2. package/index.js +30 -0
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -12,6 +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[][];
16
+ declare function containsShape<T>(array2D: T[][], shape: T[][], x: number, y: number): boolean;
15
17
  declare function takeRandomly<T>(array: T[], count: number): T[];
16
18
  declare function pickRandomIndices<T>(array: T[], count: number): number[];
17
19
  declare function pluck<T, K extends keyof T>(array: T[], key: K): T[K][];
@@ -309,6 +311,8 @@ export declare const Arrays: {
309
311
  takeRandomly: typeof takeRandomly;
310
312
  pickRandomIndices: typeof pickRandomIndices;
311
313
  initialize: typeof initializeArray;
314
+ initialize2D: typeof initialize2DArray;
315
+ containsShape: typeof containsShape;
312
316
  glue: typeof glue;
313
317
  pluck: typeof pluck;
314
318
  pick: typeof pick;
package/index.js CHANGED
@@ -70,6 +70,34 @@ function initializeArray(count, initializer) {
70
70
  return results
71
71
  }
72
72
 
73
+ function initialize2DArray(x, y, initialValue) {
74
+ const array = []
75
+ for (let i = 0; i < x; i++) {
76
+ array.push([])
77
+ for (let j = 0; j < y; j++) {
78
+ array[i].push(initialValue)
79
+ }
80
+ }
81
+ return array
82
+ }
83
+
84
+ 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) {
86
+ return false
87
+ }
88
+ for (let i = 0; i < shape.length; i++) {
89
+ for (let j = 0; j < shape[i].length; j++) {
90
+ if (shape[i][j] === undefined) {
91
+ continue
92
+ }
93
+ if (array2D[i + y][j + x] !== shape[i][j]) {
94
+ return false
95
+ }
96
+ }
97
+ }
98
+ return true
99
+ }
100
+
73
101
  function takeRandomly(array, count) {
74
102
  return shuffle(array).slice(0, count)
75
103
  }
@@ -1798,6 +1826,8 @@ exports.Arrays = {
1798
1826
  takeRandomly,
1799
1827
  pickRandomIndices,
1800
1828
  initialize: initializeArray,
1829
+ initialize2D: initialize2DArray,
1830
+ containsShape,
1801
1831
  glue,
1802
1832
  pluck,
1803
1833
  pick,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "4.15.0",
3
+ "version": "4.17.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {