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

Demo ignoring the array problem and silently convert to/from objects #1

Open
wants to merge 1 commit into
base: array-entry-setting
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
ValidatorAction,
ValidatorMap
} from './types';
import { isArrayObject } from './utils/array-object';
import { isArrayObject, arrayToObject } from './utils/array-object';

export {
CHANGESET,
Expand Down Expand Up @@ -1062,7 +1062,9 @@ export function Changeset(
validationMap?: ValidatorMap | null | undefined,
options?: Config
): BufferedChangeset {
const c: BufferedChangeset = changeset(obj, validateFn, validationMap, options);
const convertedObj = unArray(obj);

const c: BufferedChangeset = changeset(convertedObj, validateFn, validationMap, options);

return new Proxy(c, {
get(targetBuffer, key /*, receiver*/) {
Expand All @@ -1076,3 +1078,17 @@ export function Changeset(
}
});
}

function unArray(obj: any) {
if (!isObject(obj)) {
return obj;
}

for (let key in obj) {
if (Array.isArray(obj[key] as unknown)) {
obj[key] = arrayToObject(obj[key] as unknown[]);
}
}

return obj;
}
101 changes: 59 additions & 42 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Changeset } from '../src';
import get from '../src/utils/get-deep';
import set from '../src/utils/set-deep';
import lookupValidator from '../src/utils/validator-lookup';
import { arrayToObject } from '../src/utils/array-object';

let dummyModel: any;
const exampleArray: Array<any> = [];
Expand Down Expand Up @@ -654,7 +655,8 @@ describe('Unit | Utility | changeset', () => {
}
});

it('#get proxies to underlying array properties', () => {
// no longer an array
xit('#get proxies to underlying array properties', () => {
dummyModel.users = ['user1', 'user2'];
const dummyChangeset = Changeset(dummyModel);

Expand Down Expand Up @@ -937,7 +939,9 @@ describe('Unit | Utility | changeset', () => {
changeset.set('emails.0.primary', 'fun@email.com');

expect(changeset.get('emails.0.primary')).toEqual('fun@email.com');
expect(changeset.get('emails').unwrap()).toEqual([{ primary: 'fun@email.com' }]);
expect(changeset.get('emails').unwrap()).toEqual(
arrayToObject([{ primary: 'fun@email.com' }])
);
expect(changeset.changes).toEqual([{ key: 'emails.0.primary', value: 'fun@email.com' }]);
});

Expand All @@ -948,9 +952,10 @@ describe('Unit | Utility | changeset', () => {

expect(changeset.get('emails.0.funEmail')).toEqual('fun@email.com');
expect(changeset.changes).toEqual([{ key: 'emails.0.funEmail', value: 'fun@email.com' }]);
expect(changeset.get('emails').unwrap()).toEqual([
{ primary: 'bob@email.com', funEmail: 'fun@email.com' }
]);
expect(changeset.get('emails').unwrap()).toEqual(
// Error: primary is missing, incorrect merge?
arrayToObject([{ primary: 'bob@email.com', funEmail: 'fun@email.com' }])
);
});

it('can add new properties to new entries', () => {
Expand All @@ -961,10 +966,12 @@ describe('Unit | Utility | changeset', () => {

expect(changeset.get('emails.1.funEmail')).toEqual('fun@email.com');
expect(changeset.get('emails.1.primary')).toEqual('primary@email.com');
expect(changeset.get('emails').unwrap()).toEqual([
{ primary: 'bob@email.com' },
{ primary: 'primary@email.com', funEmail: 'fun@email.com' }
]);
expect(changeset.get('emails').unwrap()).toEqual(
arrayToObject([
{ primary: 'bob@email.com' },
{ primary: 'primary@email.com', funEmail: 'fun@email.com' }
])
);
expect(changeset.changes).toEqual([
{ key: 'emails.1.funEmail', value: 'fun@email.com' },
{ key: 'emails.1.primary', value: 'primary@email.com' }
Expand All @@ -981,10 +988,12 @@ describe('Unit | Utility | changeset', () => {

expect(changeset.get('emails.1.funEmail')).toEqual('fun@email.com');
expect(changeset.get('emails.1.primary')).toEqual('primary@email.com');
expect(changeset.get('emails').unwrap()).toEqual([
{ primary: 'bob@email.com' },
{ primary: 'primary@email.com', funEmail: 'fun@email.com' }
]);
expect(changeset.get('emails').unwrap()).toEqual(
arrayToObject([
{ primary: 'bob@email.com' },
{ primary: 'primary@email.com', funEmail: 'fun@email.com' }
])
);
expect(changeset.changes).toEqual([
{
key: 'emails.1',
Expand All @@ -1004,10 +1013,12 @@ describe('Unit | Utility | changeset', () => {
}
}
]);
expect(changeset.get('emails').unwrap()).toEqual([
{ primary: 'bob@email.com' },
{ primary: 'primary2@email.com', funEmail: 'fun@email.com' }
]);
expect(changeset.get('emails').unwrap()).toEqual(
arrayToObject([
{ primary: 'bob@email.com' },
{ primary: 'primary2@email.com', funEmail: 'fun@email.com' }
])
);
});

it('can edit a new object that was added after deleting an array entry', () => {
Expand All @@ -1026,13 +1037,15 @@ describe('Unit | Utility | changeset', () => {

changeset.set('emails.1', null);

expect(changeset.get('emails').unwrap()).toEqual([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com'
},
null
]);
expect(changeset.get('emails').unwrap()).toEqual(
arrayToObject([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com'
},
null
])
);
expect(changeset.changes).toEqual([
{
key: 'emails.1',
Expand All @@ -1045,16 +1058,18 @@ describe('Unit | Utility | changeset', () => {
primary: 'brandNewPrimary@email.com'
});

expect(changeset.get('emails').unwrap()).toEqual([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com'
},
{
fun: 'brandNew@email.com',
primary: 'brandNewPrimary@email.com'
}
]);
expect(changeset.get('emails').unwrap()).toEqual(
arrayToObject([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com'
},
{
fun: 'brandNew@email.com',
primary: 'brandNewPrimary@email.com'
}
])
);
expect(changeset.changes).toEqual([
{
key: 'emails.1',
Expand Down Expand Up @@ -1084,14 +1099,16 @@ describe('Unit | Utility | changeset', () => {

changeset.set('emails.1', null);

expect(changeset.get('emails').unwrap()).toEqual([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com',
value: 'the value'
},
null
]);
expect(changeset.get('emails').unwrap()).toEqual(
arrayToObject([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com',
value: 'the value'
},
null
])
);
expect(changeset.changes).toEqual([
{
key: 'emails.1',
Expand Down