Skip to content
Closed
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"dependencies": {
"@joshdb/auto-ensure": "2.0.0-next.4c9b27e.0",
"@joshdb/map": "2.0.0-next.782d01a.0",
"@joshdb/provider": "2.0.0-next.a699598.0",
"@joshdb/provider": "2.0.0-next.1c9d8d9.0",
"@sapphire/utilities": "^3.12.0"
},
"devDependencies": {
Expand Down
39 changes: 14 additions & 25 deletions src/lib/structures/Josh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,9 +1082,9 @@ export class Josh<StoredValue = unknown> {
for (const middleware of this.middlewares.getPostMiddlewares(Method.Map)) payload = await middleware[Method.Map](payload);

if (payload.errors.length) {
const { behaviorOnPayloadError } = this.options;
const { behaviorOnPayloadError = Josh.ErrorBehavior.Throw } = this.options;

if (behaviorOnPayloadError !== undefined && behaviorOnPayloadError >= Josh.ErrorBehavior.Log) {
if (behaviorOnPayloadError >= Josh.ErrorBehavior.Log) {
if (payload.errors.length === 1 && behaviorOnPayloadError === Josh.ErrorBehavior.Throw) throw payload.errors[0];
else {
for (const error of payload.errors) console.error(error);
Expand Down Expand Up @@ -1309,12 +1309,7 @@ export class Josh<StoredValue = unknown> {
/**
* Gets random value(s).
* @param options The options for getting random values.
* @returns The random value(s) or null.
*
* @example
* ```javascript
* await josh.random(); // null
* ```
* @returns The random value(s).
*
* @example
* ```javascript
Expand All @@ -1323,9 +1318,9 @@ export class Josh<StoredValue = unknown> {
* await josh.random(); // ['value']
* ```
*/
public async random(options?: Josh.RandomOptions): Promise<StoredValue[] | null> {
const { count = 1, duplicates = true } = options ?? {};
let payload: Payload.Random<StoredValue> = { method: Method.Random, errors: [], trigger: Trigger.PreProvider, count, duplicates };
public async random(options?: Josh.RandomOptions): Promise<StoredValue[]> {
const { count = 1, unique = false } = options ?? {};
let payload: Payload.Random<StoredValue> = { method: Method.Random, errors: [], trigger: Trigger.PreProvider, count, unique };

for (const middleware of Array.from(this.middlewares.values())) await middleware.run(payload);
for (const middleware of this.middlewares.getPreMiddlewares(Method.Random)) payload = await middleware[Method.Random](payload);
Expand All @@ -1339,22 +1334,16 @@ export class Josh<StoredValue = unknown> {

this.runBehaviorOnPayloadError(payload);

if (isPayloadWithData<StoredValue[]>(payload)) return payload.data.length ? payload.data : null;
if (isPayloadWithData<StoredValue[]>(payload)) return payload.data;

throw this.providerDataFailedError;
}

/**
* Get a random key.
*
* NOTE: `options.duplicates` only makes checks on primitive value types.
* @since 2.0.0
* @returns The random key or `null`.
*
* @example
* ```javascript
* await josh.randomKey(); // null
* ```
* @returns The random key(s).
*
* @example
* ```javascript
Expand All @@ -1363,9 +1352,9 @@ export class Josh<StoredValue = unknown> {
* await josh.randomKey(); // ['key']
* ```
*/
public async randomKey(options?: Josh.RandomOptions): Promise<string[] | null> {
const { count = 1, duplicates = true } = options ?? {};
let payload: Payload.RandomKey = { method: Method.RandomKey, errors: [], trigger: Trigger.PreProvider, count, duplicates };
public async randomKey(options?: Josh.RandomOptions): Promise<string[]> {
const { count = 1, unique = false } = options ?? {};
let payload: Payload.RandomKey = { method: Method.RandomKey, errors: [], trigger: Trigger.PreProvider, count, unique };

for (const middleware of Array.from(this.middlewares.values())) await middleware.run(payload);
for (const middleware of this.middlewares.getPreMiddlewares(Method.RandomKey)) payload = await middleware[Method.RandomKey](payload);
Expand All @@ -1379,7 +1368,7 @@ export class Josh<StoredValue = unknown> {

this.runBehaviorOnPayloadError(payload);

if (isPayloadWithData<string[]>(payload)) return payload.data.length ? payload.data : null;
if (isPayloadWithData<string[]>(payload)) return payload.data;

throw this.providerDataFailedError;
}
Expand Down Expand Up @@ -2078,10 +2067,10 @@ export namespace Josh {
count?: number;

/**
* Whether to allow duplicates.
* Whether the values must be unique.
* @since 2.0.0
*/
duplicates?: boolean;
unique?: boolean;
}

export enum ErrorBehavior {
Expand Down
12 changes: 6 additions & 6 deletions tests/lib/structures/Josh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ describe('Josh', () => {
test('GIVEN josh w/ data THEN returns data from random', async () => {
await josh.set('key', 'value');

const random = await josh.random({ count: 1, duplicates: false });
const random = await josh.random({ count: 1, unique: true });

expect(random).toEqual(['value']);
});
Expand All @@ -1276,7 +1276,7 @@ describe('Josh', () => {
await josh.set('key', 'value');
await josh.set('key2', 'value');

const random = await josh.random({ count: 2, duplicates: false });
const random = await josh.random({ count: 2, unique: true });

expect(random).toEqual(['value', 'value']);
});
Expand All @@ -1285,7 +1285,7 @@ describe('Josh', () => {
await josh.set('key', 'value');
await josh.set('key2', 'value');

const random = await josh.random({ count: 2, duplicates: true });
const random = await josh.random({ count: 2, unique: false });

expect(random).toEqual(['value', 'value']);
});
Expand Down Expand Up @@ -1321,7 +1321,7 @@ describe('Josh', () => {
test('GIVEN josh w/ data THEN returns data from random', async () => {
await josh.set('key', 'value');

const random = await josh.randomKey({ count: 1, duplicates: false });
const random = await josh.randomKey({ count: 1, unique: true });

expect(random).toEqual(['key']);
});
Expand All @@ -1330,7 +1330,7 @@ describe('Josh', () => {
await josh.set('key', 'value');
await josh.set('key2', 'value');

const random = await josh.randomKey({ count: 2, duplicates: false });
const random = await josh.randomKey({ count: 2, unique: true });

expect(random?.length).toEqual(2);
});
Expand All @@ -1339,7 +1339,7 @@ describe('Josh', () => {
await josh.set('key', 'value');
await josh.set('key2', 'value');

const random = await josh.randomKey({ count: 2, duplicates: true });
const random = await josh.randomKey({ count: 2, unique: false });

expect(random?.length).toEqual(2);
});
Expand Down
12 changes: 11 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ __metadata:
"@joshdb/auto-ensure": 2.0.0-next.4c9b27e.0
"@joshdb/eslint-config": ^1.1.0-next.a699598.0
"@joshdb/map": 2.0.0-next.782d01a.0
"@joshdb/provider": 2.0.0-next.a699598.0
"@joshdb/provider": 2.0.0-next.1c9d8d9.0
"@joshdb/ts-config": 1.1.0-next.a699598.0
"@sapphire/utilities": ^3.12.0
"@types/node": ^18.16.18
Expand Down Expand Up @@ -690,6 +690,16 @@ __metadata:
languageName: node
linkType: hard

"@joshdb/provider@npm:2.0.0-next.1c9d8d9.0":
version: 2.0.0-next.1c9d8d9.0
resolution: "@joshdb/provider@npm:2.0.0-next.1c9d8d9.0"
dependencies:
"@sapphire/utilities": ^3.12.0
reflect-metadata: ^0.1.13
checksum: 122a365773c15dcb61c620e4f20099ca6753477cde5d88181f34d4a8ab4a487ab2edfa50cc58af38b04456e4b29319671eb60da1719893804ba4fa172c204e94
languageName: node
linkType: hard

"@joshdb/provider@npm:2.0.0-next.a699598.0":
version: 2.0.0-next.a699598.0
resolution: "@joshdb/provider@npm:2.0.0-next.a699598.0"
Expand Down