Skip to content

Commit

Permalink
fix: fixed output when passed into JSON.stringify (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Prilutskiy committed Apr 6, 2020
1 parent da5a070 commit 4682ad7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Filterion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,28 @@ describe('filterion.getValues', () => {
});
});

describe('filterion.toJSON', () => {
it('stringified representation matches the payload repesentation', () => {
const filterion = new Filterion<MyTestFilter>()
.add('name', 'Max');
const payload = filterion.getPayload();

const json = JSON.stringify(filterion);
const payloadJson = JSON.stringify(payload);

expect(json).toStrictEqual(payloadJson);
});
it('toJSON result matches the payload', () => {
const filterion = new Filterion<MyTestFilter>()
.add('name', 'Max');
const payload = filterion.getPayload();

const toJsonResult = filterion.toJSON();

expect(toJsonResult).toStrictEqual(payload);
})
});

type MyTestFilter = {
name: string;
age: number;
Expand Down
10 changes: 10 additions & 0 deletions src/Filterion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,22 @@ export class Filterion<S extends {} = {}> {
return new Filterion<S>(this.config).attach(payloadClone);
}

/*
* Attach existing payload
*/
public attach(payload: IFilterionPayload<S>): Filterion<S> {
const result = new Filterion<S>(this.config);
result.payload = payload;
return result;
}

/*
* Get JSON representation of the instance
*/
public toJSON(): IFilterionPayload<S> {
return this.payload;
}

/**
* Initialize payload"s values collection if it doesn"t exist yet
*/
Expand Down

0 comments on commit 4682ad7

Please sign in to comment.