Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Collection and Single should not mutate the provided data #90

Merged
merged 1 commit into from
Jan 10, 2025
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: 12 additions & 0 deletions src/Collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,18 @@ describe('Collection', () => {
expect(collection.getOne(0)).toEqual({ id: 0, name: 'bar' });
expect(collection.getOne(1)).toEqual({ id: 1, name: 'baz' });
});

it('should update the original item', () => {
const items = [{ name: 'foo' }, { name: 'baz' }];
const collection = new Collection<CollectionItem>({
items,
});
collection.updateOne(0, { id: 0, name: 'bar' });
expect(collection.getOne(0)).toEqual({ id: 0, name: 'bar' });
expect(collection.getOne(1)).toEqual({ id: 1, name: 'baz' });
expect(items[0]).toEqual({ name: 'foo' });
expect(items[1]).toEqual({ name: 'baz' });
});
});

describe('removeOne', () => {
Expand Down
10 changes: 6 additions & 4 deletions src/Collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import get from 'lodash/get.js';
import matches from 'lodash/matches.js';
import cloneDeep from 'lodash/cloneDeep.js';
import type { Database } from './Database.ts';
import type {
CollectionItem,
Expand Down Expand Up @@ -196,7 +197,8 @@ export class Collection<T extends CollectionItem = CollectionItem> {
}

addOne(item: T) {
const identifier = item[this.identifierName];
const clone = cloneDeep(item);
const identifier = clone[this.identifierName];
if (identifier != null) {
if (this.getIndex(identifier) !== -1) {
throw new Error(
Expand All @@ -208,10 +210,10 @@ export class Collection<T extends CollectionItem = CollectionItem> {
}
} else {
// @ts-expect-error - For some reason, TS does not accept writing a generic types with the index signature
item[this.identifierName] = this.getNewId();
clone[this.identifierName] = this.getNewId();
}
this.items.push(item);
return Object.assign({}, item); // clone item to avoid returning the original;
this.items.push(clone);
return clone; // clone item to avoid returning the original;
}

updateOne(identifier: number | string, item: T) {
Expand Down
8 changes: 8 additions & 0 deletions src/Single.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,13 @@ describe('Single', () => {
single.updateOnly({ name: 'bar' });
expect(single.getOnly()).toEqual({ name: 'bar' });
});

it('should not update the original item', () => {
const data = { name: 'foo' };
const single = new Single(data);
single.updateOnly({ name: 'bar' });
expect(single.getOnly()).toEqual({ name: 'bar' });
expect(data).toEqual({ name: 'foo' });
});
});
});
3 changes: 2 additions & 1 deletion src/Single.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import cloneDeep from 'lodash/cloneDeep.js';
import type { Database } from './Database.ts';
import type { CollectionItem, Embed, Query } from './types.ts';

Expand All @@ -12,7 +13,7 @@ export class Single<T extends CollectionItem = CollectionItem> {
"Can't initialize a Single with anything except an object",
);
}
this.obj = obj;
this.obj = cloneDeep(obj);
}

/**
Expand Down
Loading