From bf88422003f5b21c6d12cd2d5033fdc56a9a461a Mon Sep 17 00:00:00 2001 From: krutoo Date: Fri, 23 Jan 2026 21:06:33 +0500 Subject: [PATCH] feat(math)!: toShuffled removed toShuffled removed Also some docs improvemets Also duplicated code removed in randomFloat/Integer BREAKING CHANGE: toShuffled removed toShuffled removed because under the hood it just called shuffle with copying given array, you can make it by yourself --- docs/docs/math/shuffle.mdx | 12 ----------- docs/docs/rspack/node-externals.mdx | 33 +++++++++++++++++++++++++++++ docs/docs/rspack/plugin-exec.mdx | 2 +- src/math/__test__/shuffle.test.ts | 22 +------------------ src/math/random-float.ts | 4 +--- src/math/random-integer.ts | 4 +--- src/math/shuffle.ts | 12 +---------- 7 files changed, 38 insertions(+), 51 deletions(-) create mode 100644 docs/docs/rspack/node-externals.mdx diff --git a/docs/docs/math/shuffle.mdx b/docs/docs/math/shuffle.mdx index 3bb9303..ca860f6 100644 --- a/docs/docs/math/shuffle.mdx +++ b/docs/docs/math/shuffle.mdx @@ -23,15 +23,3 @@ shuffle(array, { random: myCustomRandomizer, }); ``` - -### `toShuffled` - -`toShuffled` works exactly like `shuffle` but don't mutates given array, instead it returns new randomly sorted array. - -```ts -import { toShuffled } from '@krutoo/utils'; - -const array = [1, 2, 3, 4, 5, 6, 7]; - -const newArray = toShuffled(array); // same items but randomly sorted -``` diff --git a/docs/docs/rspack/node-externals.mdx b/docs/docs/rspack/node-externals.mdx new file mode 100644 index 0000000..7b0fcbc --- /dev/null +++ b/docs/docs/rspack/node-externals.mdx @@ -0,0 +1,33 @@ +export const meta = { + category: 'Rspack', + title: 'nodeExternals', +}; + +# `nodeExternals` + +Simple modern alternative to `webpack-node-externals`. + +This utility function marks all dependencies from node_modules as `external`. +It means that such dependencies will not be included to bundle. + +Typically it can be useful when you bundle server application for running in Node.js. + +### Usage + +```js title="rspack.config.js" +import { nodeExternals } from '@kruoo/utils/rspack'; + +export default { + mode: 'development', + target: 'node', + output: { + filename: 'index.js' + } + externalsPresets: { + node: true, + }, + externals: [ + nodeExternals(), + ], +}; +``` diff --git a/docs/docs/rspack/plugin-exec.mdx b/docs/docs/rspack/plugin-exec.mdx index 5636e0d..3796f3a 100644 --- a/docs/docs/rspack/plugin-exec.mdx +++ b/docs/docs/rspack/plugin-exec.mdx @@ -23,7 +23,7 @@ export default { If you build your Node.js app and you want to run it during development in watch mode you can configure Rspack like this: ```js title="rspack.config.js" -import utils from '@kruoo/utils/rspack'; +import * as utils from '@kruoo/utils/rspack'; export default { mode: 'development', diff --git a/src/math/__test__/shuffle.test.ts b/src/math/__test__/shuffle.test.ts index ff8fc36..dbbdb30 100644 --- a/src/math/__test__/shuffle.test.ts +++ b/src/math/__test__/shuffle.test.ts @@ -1,6 +1,6 @@ import { describe, test } from 'node:test'; import { expect } from '@std/expect/expect'; -import { shuffle, toShuffled } from '../shuffle.ts'; +import { shuffle } from '../shuffle.ts'; describe('shuffle', () => { test('should randomly sort items', () => { @@ -20,23 +20,3 @@ describe('shuffle', () => { expect(matchCount < 7).toBe(true); }); }); - -describe('toShuffled', () => { - test('should return randomly sorted copy', () => { - const refer = [1, 2, 3, 4, 5, 6, 7]; - - const input = [...refer]; - const output = toShuffled(input); - - // check that input is not mutated - for (let i = 0; i < refer.length; i++) { - expect(input[i] === refer[i]).toBe(true); - } - - // check that output is new array but with same items - expect(output === input).toBe(false); - expect(output.length === input.length).toBe(true); - expect(output.every(item => input.includes(item))).toBe(true); - expect(output.some((item, index) => input[index] !== item)).toBe(true); - }); -}); diff --git a/src/math/random-float.ts b/src/math/random-float.ts index 5cbb1ad..4808b58 100644 --- a/src/math/random-float.ts +++ b/src/math/random-float.ts @@ -6,9 +6,7 @@ import type { RandomBetween } from './types.ts'; * @param max End of range. * @returns Random number. */ -export function randomFloat(min: number, max: number): number { - return min + Math.random() * (max - min); -} +export const randomFloat: RandomBetween = createRandomFloat(); /** * Returns random float number generator. diff --git a/src/math/random-integer.ts b/src/math/random-integer.ts index 394f8cd..7811de3 100644 --- a/src/math/random-integer.ts +++ b/src/math/random-integer.ts @@ -6,9 +6,7 @@ import type { RandomBetween } from './types.ts'; * @param max End of range. * @returns Random number. */ -export function randomInteger(min: number, max: number): number { - return Math.floor(min + Math.random() * (max + 1 - min)); -} +export const randomInteger: RandomBetween = createRandomInteger(); /** * Returns random integer number generator. diff --git a/src/math/shuffle.ts b/src/math/shuffle.ts index 422842a..1156c9d 100644 --- a/src/math/shuffle.ts +++ b/src/math/shuffle.ts @@ -4,6 +4,7 @@ export interface ShuffleOptions { /** * Randomly sorts items in given array. + * Mutates given array. For immutability use `shuffle([...list])`. * @param list Source array. * @param options Options. * @returns Exactly given array. @@ -21,14 +22,3 @@ export function shuffle( return list; } - -/** - * Returns new array with random sorted items. - * @param list Source array. - * @param options Options. - * @returns New randomly sorted array. - * @see https://stackoverflow.com/a/12646864 - */ -export function toShuffled(list: Array, options: ShuffleOptions = {}): Array { - return shuffle([...list], options); -}