Skip to content

Commit

Permalink
Add method to shuffle data
Browse files Browse the repository at this point in the history
  • Loading branch information
tobeno committed Jul 16, 2023
1 parent 15a3dda commit 72930a1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/wrappers/data.wrapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { snakeCase } from 'change-case';
import { equalsObjectDeep } from '../utils/object.utils';
import { Data } from './data.wrapper';
import { Text } from './text.wrapper';

Expand Down Expand Up @@ -177,6 +178,36 @@ describe('Data', () => {
});
});

describe('shuffled', () => {
it('should return the array shuffled', async () => {
const data = Data.create([1, 2, 3, 4, 5]);

let shuffled = false;
for (let i = 0; i < 100; i++) {
if (!equalsObjectDeep(data.shuffled.value, data.value)) {
shuffled = true;
break;
}
}

expect(shuffled).toBe(true);
});

it('should return the object shuffled', async () => {
const data = Data.create({ a: 1, b: 2, c: 3, d: 4, e: 5 });

let shuffled = false;
for (let i = 0; i < 100; i++) {
if (!equalsObjectDeep(data.shuffled.keys.value, data.keys.value)) {
shuffled = true;
break;
}
}

expect(shuffled).toBe(true);
});
});

describe('sorted', () => {
it('should return the array sorted', async () => {
const data = Data.create([3, 1, 2]);
Expand Down Expand Up @@ -766,6 +797,36 @@ describe('Data', () => {
});
});

describe('shuffle', () => {
it('should return the array shuffled', async () => {
const data = Data.create([1, 2, 3, 4, 5]);

let shuffled = false;
for (let i = 0; i < 100; i++) {
if (!equalsObjectDeep(data.shuffle().value, data.value)) {
shuffled = true;
break;
}
}

expect(shuffled).toBe(true);
});

it('should return the object shuffled', async () => {
const data = Data.create({ a: 1, b: 2, c: 3, d: 4, e: 5 });

let shuffled = false;
for (let i = 0; i < 100; i++) {
if (!equalsObjectDeep(data.shuffle().keys.value, data.keys.value)) {
shuffled = true;
break;
}
}

expect(shuffled).toBe(true);
});
});

describe('sort', () => {
it('should sort an array', async () => {
const data = Data.create(['1', '5', '2', '3', '4', '10']);
Expand Down
19 changes: 19 additions & 0 deletions src/wrappers/data.wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ export class Data<
});
}

get shuffled(): Data<ValueType extends Array<any> ? ValueType : any> {
return this.shuffle();
}

/**
* Returns the data sorted by its values.
*/
Expand Down Expand Up @@ -537,6 +541,21 @@ export class Data<
return Data.create(Object.fromEntries(entries)) as any;
}

/**
* Returns the data randomly shuffled.
*/
shuffle(): Data<ValueType extends Array<any> ? ValueType : any> {
if (Array.isArray(this.value)) {
return Data.create(
[...this.value].sort(() => Math.random() - 0.5),
) as any;
}

const entries = this.entries.shuffle();

return Data.create(Object.fromEntries(entries.value)) as any;
}

/**
* Returns the data sorted keys.
*/
Expand Down

0 comments on commit 72930a1

Please sign in to comment.