From 266ad3289b39d7451b93a7297bb18352dbfb967b Mon Sep 17 00:00:00 2001 From: Roman B Date: Fri, 24 May 2024 12:57:05 +0400 Subject: [PATCH] Implements own times func instead of ramda --- src/core/helpers.js | 7 +++---- src/lib/times.mjs | 9 +++++++++ src/lib/times.test.mjs | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/lib/times.mjs create mode 100644 src/lib/times.test.mjs diff --git a/src/core/helpers.js b/src/core/helpers.js index cdd7017..1e35305 100755 --- a/src/core/helpers.js +++ b/src/core/helpers.js @@ -1,4 +1,4 @@ -import { times } from "ramda"; +import times from '../lib/times.mjs'; /* mines opened @@ -32,7 +32,6 @@ export const getCellNeighborMines = (cell = 0) => cell >> 3; // export const unflagCell = (cell = 0) => cell & (~CELL_FLAGGED); export const toggleCellFlag = (cell = 0) => cell ^ CELL_FLAGGED; - export function getFreeRandomCell([width, height], flatBusyCells) { const sortedBusyCells = flatBusyCells .slice(0) @@ -88,7 +87,7 @@ export const getNeighborCells = ([width, height], i) => { export const calcHeatMap = ([width, height], mines = []) => { const map = []; - mines.forEach(cell => { + mines.forEach((cell) => { const col = cell % width; const row = Math.floor(cell / width); @@ -114,7 +113,7 @@ export function genCells(arena, minesCount, clicked) { const heatMap = calcHeatMap(arena, mines); const cells = times( - i => (heatMap[i] << 3) + (+mines.includes(i) << 1), + (i) => (heatMap[i] << 3) + (+mines.includes(i) << 1), arena[0] * arena[1] ); diff --git a/src/lib/times.mjs b/src/lib/times.mjs new file mode 100644 index 0000000..c943d58 --- /dev/null +++ b/src/lib/times.mjs @@ -0,0 +1,9 @@ +export default function times(fn, n) { + const result = new Array(n); + + for (let i = 0; i < n; i++) { + result[i] = fn(i); + } + + return result; +} diff --git a/src/lib/times.test.mjs b/src/lib/times.test.mjs new file mode 100644 index 0000000..d96f37e --- /dev/null +++ b/src/lib/times.test.mjs @@ -0,0 +1,27 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert'; +import times from './times.mjs'; +// import { times } from 'ramda'; + +describe('Runs function', () => { + it('0 times', ({ mock }) => { + const fn = mock.fn((i) => i * 2); + const result = times(fn, 0); + assert.strictEqual(fn.mock.callCount(), 0); + assert.deepStrictEqual(result, []); + }); + + it('1 time', ({ mock }) => { + const fn = mock.fn((i) => i * 2); + const result = times(fn, 1); + assert.strictEqual(fn.mock.callCount(), 1); + assert.deepStrictEqual(result, [0]); + }); + + it('8 times', ({ mock }) => { + const fn = mock.fn((i) => i * 2); + const result = times(fn, 8); + assert.strictEqual(fn.mock.callCount(), 8); + assert.deepStrictEqual(result, [0, 2, 4, 6, 8, 10, 12, 14]); + }); +});