Skip to content

Commit

Permalink
Allow override of prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
develohpanda committed May 12, 2019
1 parent 66717b4 commit b9e3b37
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ json-order allows for conversion between JS Objects and a JSON string while keep
### `parse`
To parse a JSON string and generate a map:
```js
const result = orderedJson.parse('{...}');
const result = orderedJson.parse('{...}', '$');
```

The second parameter controls what the map prefix should be.

`result.object` will contain a JS object while `result.map` will contain the generated property map.

### `stringify`
Expand Down
4 changes: 2 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const traverseObject = (obj: object, map: PropertyMap, parentKey: string) => {
});
};

const parse = (jsonString: string): OrderedParseResult => {
const parse = (jsonString: string, prefix: string = '$'): OrderedParseResult => {
const obj: object = JSON.parse(jsonString);

const map = {};
traverseObject(obj, map, '$');
traverseObject(obj, map, prefix);
return {
object: obj,
map
Expand Down
9 changes: 7 additions & 2 deletions src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ const copyProperty = (sourceObject: object, resultObject: object, propertyPath:
}
};

const stringify = (sourceObject: object, map: PropertyMap, space?: number): string => {
const stringify = (sourceObject: object, map?: PropertyMap, space?: number): string => {
if (!map) {
return JSON.stringify(sourceObject, null, space);
}

const mapKeys = Object.keys(map);
const prefixLength = mapKeys[0] && mapKeys[0].length || 0;

const resultObject = {};
mapKeys.forEach((mk) => {
const childKeys = map[mk];

// Remove starting $
const parentKey = mk.substr(1);
const parentKey = mk.substr(prefixLength);

const parent = getProperty(sourceObject, parentKey);

Expand Down
2 changes: 1 addition & 1 deletion test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PropertyMap } from '../src/models';
import parse from '../src/parse';

describe('parse ', () => {
const expectMap = (input: string, map: PropertyMap) => expect(parse(input).map).toEqual(map);
const expectMap = (input: string, map: PropertyMap) => expect(parse(input, '$').map).toEqual(map);

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

Expand Down
6 changes: 6 additions & 0 deletions test/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ describe('stringify ', () => {
{ $: ['a'] },
'{}'));

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

it('ignores properties not found in map',
() => expectString(
{ a: '1', b: '2' },
Expand Down

0 comments on commit b9e3b37

Please sign in to comment.