Skip to content

Commit

Permalink
fix: replace "in" with "hasOwnProperty" in reshape's for in (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
velialiev authored Oct 11, 2023
1 parent 9776334 commit 6bce049
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/reshape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ export function reshape<Type, Shape extends Record<string, unknown>>({
const result: Record<string, Store<any>> = {};

for (const key in shape) {
if (key in shape) {
const fn = shape[key];
result[key] = source.map((state) => {
const result = fn(state);
return result === undefined ? null : result;
});
if (!Object.prototype.hasOwnProperty.call(shape, key)) {
continue;
}

const fn = shape[key];
result[key] = source.map((state) => {
const result = fn(state);
return result === undefined ? null : result;
});
}

return result as any;
Expand Down
21 changes: 20 additions & 1 deletion src/reshape/reshape.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStore, createEvent } from 'effector';
import { createEvent, createStore } from 'effector';
import { reshape } from './index';

test('reshape from string to different types', () => {
Expand Down Expand Up @@ -74,3 +74,22 @@ test('reshape returns null in shape if fn returned undefined', () => {
expect(shape.first.getState()).toBe('');
expect(shape.second.getState()).toBe(null);
});

test("reshape ignores shape's prototype properties", () => {
type User = { first: string; second?: number };

const $user = createStore<User>({ first: '' });

const shape = {
first: (user: User) => user.first,
};

Object.setPrototypeOf(shape, { second: (user: User) => user.second });

const stores = reshape({
source: $user,
shape,
});

expect(stores).not.toHaveProperty('second');
});

0 comments on commit 6bce049

Please sign in to comment.