Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions docs/docs/math/shuffle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
33 changes: 33 additions & 0 deletions docs/docs/rspack/node-externals.mdx
Original file line number Diff line number Diff line change
@@ -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(),
],
};
```
2 changes: 1 addition & 1 deletion docs/docs/rspack/plugin-exec.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
22 changes: 1 addition & 21 deletions src/math/__test__/shuffle.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
4 changes: 1 addition & 3 deletions src/math/random-float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions src/math/random-integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 1 addition & 11 deletions src/math/shuffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,14 +22,3 @@ export function shuffle<T extends any[]>(

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<T>(list: Array<T>, options: ShuffleOptions = {}): Array<T> {
return shuffle([...list], options);
}