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

update stringify's param to match that of JSON.stringify #237

Open
wants to merge 2 commits into
base: master
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const jsonString = orderedJson.stringify(srcObj, map, 2);
- `srcObj`: an object with the properties in any order
- `map` [optional]: the property map generated by `parse` above. If the map is unset, the response is a standard `JSON.stringify`.
- `separator` [optional]: a non-empty `string` that controls what the key separator is in the generated map. Defaults to `~`.
- `space` [optional]: a `number` used to insert white space into the output JSON string for readability purposes, as per the `JSON.stringify` [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters).
- `space` [optional]: a `string` or `number` used to insert white space into the output JSON string for readability purposes, per the `JSON.stringify` [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters).

### `order`

Expand Down
34 changes: 25 additions & 9 deletions __tests__/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import stringify from '../src/stringify';
import {PropertyMap} from '../src/models';

describe('stringify ', () => {
const expectString = (obj: object, map: PropertyMap | null, str: string) =>
expect(stringify(obj, map, '.', 0)).toEqual(str);
const expectString = (obj: object, map: PropertyMap | null, space: string | number | undefined, str: string) =>
expect(stringify(obj, map, '.', space)).toEqual(str);

it('returns nothing for a blank JSON string', () =>
expectString({}, {}, '{}'));
expectString({}, {}, undefined, '{}'));

it('throws error if separator is an empty string', () => {
expect(() => stringify({}, {}, '')).toThrowError(
Expand All @@ -16,36 +16,38 @@ describe('stringify ', () => {
});

it('ignores properties not found in source', () =>
expectString({}, {$: ['a']}, '{}'));
expectString({}, {$: ['a']}, undefined, '{}'));

it('returns regular json string if map is undefined', () =>
expectString({a: '1', b: '2'}, null, '{"a":"1","b":"2"}'));
expectString({a: '1', b: '2'}, null, undefined, '{"a":"1","b":"2"}'));

it('ignores properties not found in map', () =>
expectString({a: '1', b: '2'}, {$: ['b']}, '{"b":"2"}'));
expectString({a: '1', b: '2'}, {$: ['b']}, undefined, '{"b":"2"}'));

it('returns first level object properties in order', () =>
expectString({a: 2, b: 1}, {$: ['b', 'a']}, '{"b":1,"a":2}'));
expectString({a: 2, b: 1}, {$: ['b', 'a']}, undefined, '{"b":1,"a":2}'));

it('returns first level array value in order', () =>
expectString({a: ['2', 1, true]}, {$: ['a']}, '{"a":["2",1,true]}'));
expectString({a: ['2', 1, true]}, {$: ['a']}, undefined, '{"a":["2",1,true]}'));

it('returns nested [array] > [object] properties in expected order', () =>
expectString(
{a: [1, {c: '3', d: '2'}]},
{'$': ['a'], '$.a.1': ['d', 'c']},
undefined,
'{"a":[1,{"d":"2","c":"3"}]}'
));

it('ignores nested [array] > [object] properties not found in map', () =>
expectString(
{a: [1, {b: 2, c: 3}, 4]},
{'$': ['a'], '$.a.1': ['c']},
undefined,
'{"a":[1,{"c":3},4]}'
));

it('ignores nested [array] > [object] properties not found in map', () =>
expectString({a: [1, {b: 2, c: 3}, 4]}, {$: ['a']}, '{"a":[1,{},4]}'));
expectString({a: [1, {b: 2, c: 3}, 4]}, {$: ['a']}, undefined, '{"a":[1,{},4]}'));

it('handles multi-character prefix', () => {
expect(
Expand Down Expand Up @@ -127,6 +129,7 @@ describe('stringify ', () => {
'$.a.e': ['g', 'f'],
'$.a.b': ['d', 'c'],
},
undefined,
'{"i":7,"a":{"e":{"g":5,"f":4},"h":6,"b":{"d":4,"c":3}}}'
));

Expand Down Expand Up @@ -161,6 +164,19 @@ describe('stringify ', () => {
'$.a.b.1.d.0': ['f', 'e'],
'$.a.b.1.d.0.f': ['h', 'g'],
},
undefined,
'{"i":7,"a":{"b":[8,{"d":[{"f":{"h":"h","g":true},"e":12},10],"c":9},11]}}'
));

it('supports numeric space parameter', () => {
expectString({ a: { b: { c: 3 } } }, null, 2, '{\n "a": {\n "b": {\n "c": 3\n }\n }\n}');
});

it('supports actual-space space parameter', () => {
expectString({ a: { b: { c: 3 } } }, null, " ", '{\n "a": {\n "b": {\n "c": 3\n }\n }\n}');
});

it('supports any character space parameter', () => {
expectString({ a: { b: { c: 3 } } }, null, ">", '{\n>"a": {\n>>"b": {\n>>>"c": 3\n>>}\n>}\n}');
});
});
4 changes: 2 additions & 2 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import order from './order';
* @param sourceObject an object with the properties in any order
* @param map the `PropertyMap` generated by the `parse` method.
* @param separator a non-empty `string` that controls what the key separator is in the generated map. Defaults to `~`.
* @param space a `number` used to insert white space into the output JSON string for readability purposes, as per the `JSON.stringify` [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters)
* @param space a `string` or `number` used to insert white space into the output JSON string for readability purposes, as per the `JSON.stringify` [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters)
* @returns the stringified source object ordered as per the map. If the map is unset, the response is a standard `JSON.stringify`.
*/
const stringify = (
sourceObject: object,
map: PropertyMap | null,
separator = '~',
space?: number
space?: string | number | undefined
): string => {
return JSON.stringify(order(sourceObject, map, separator), null, space);
};
Expand Down
Loading