Skip to content

Commit eac5ba9

Browse files
authored
Implements own times func instead of ramda (#7)
1 parent e229ccf commit eac5ba9

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/core/helpers.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { times } from "ramda";
1+
import times from '../lib/times.mjs';
22

33
/*
44
mines opened
@@ -32,7 +32,6 @@ export const getCellNeighborMines = (cell = 0) => cell >> 3;
3232
// export const unflagCell = (cell = 0) => cell & (~CELL_FLAGGED);
3333
export const toggleCellFlag = (cell = 0) => cell ^ CELL_FLAGGED;
3434

35-
3635
export function getFreeRandomCell([width, height], flatBusyCells) {
3736
const sortedBusyCells = flatBusyCells
3837
.slice(0)
@@ -88,7 +87,7 @@ export const getNeighborCells = ([width, height], i) => {
8887
export const calcHeatMap = ([width, height], mines = []) => {
8988
const map = [];
9089

91-
mines.forEach(cell => {
90+
mines.forEach((cell) => {
9291
const col = cell % width;
9392
const row = Math.floor(cell / width);
9493

@@ -114,7 +113,7 @@ export function genCells(arena, minesCount, clicked) {
114113
const heatMap = calcHeatMap(arena, mines);
115114

116115
const cells = times(
117-
i => (heatMap[i] << 3) + (+mines.includes(i) << 1),
116+
(i) => (heatMap[i] << 3) + (+mines.includes(i) << 1),
118117
arena[0] * arena[1]
119118
);
120119

src/lib/times.mjs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default function times(fn, n) {
2+
const result = new Array(n);
3+
4+
for (let i = 0; i < n; i++) {
5+
result[i] = fn(i);
6+
}
7+
8+
return result;
9+
}

src/lib/times.test.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert';
3+
import times from './times.mjs';
4+
// import { times } from 'ramda';
5+
6+
describe('Runs function', () => {
7+
it('0 times', ({ mock }) => {
8+
const fn = mock.fn((i) => i * 2);
9+
const result = times(fn, 0);
10+
assert.strictEqual(fn.mock.callCount(), 0);
11+
assert.deepStrictEqual(result, []);
12+
});
13+
14+
it('1 time', ({ mock }) => {
15+
const fn = mock.fn((i) => i * 2);
16+
const result = times(fn, 1);
17+
assert.strictEqual(fn.mock.callCount(), 1);
18+
assert.deepStrictEqual(result, [0]);
19+
});
20+
21+
it('8 times', ({ mock }) => {
22+
const fn = mock.fn((i) => i * 2);
23+
const result = times(fn, 8);
24+
assert.strictEqual(fn.mock.callCount(), 8);
25+
assert.deepStrictEqual(result, [0, 2, 4, 6, 8, 10, 12, 14]);
26+
});
27+
});

0 commit comments

Comments
 (0)