-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from razroo/ZETA-7642-update-codemorph-add-jso…
…n-logic ZETA-7642: Add logic for adding json object even as a string for adding json logic
- Loading branch information
Showing
9 changed files
with
102 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { EditJson } from './../interfaces/json-morph.interface'; | ||
import { addJsonKeyValue } from './add-json'; | ||
|
||
describe('addJson', () => { | ||
it('should add json key value if nested json being added', () => { | ||
const mockJson = `{ | ||
"test": "123", | ||
"targets": {} | ||
}`; | ||
|
||
const codeBlock = `{ | ||
"server": { | ||
"executor": "@angular-devkit/build-angular:server" | ||
} | ||
}`; | ||
|
||
const mockEditJson: EditJson = { | ||
nodeType: 'addJsonKeyValue', | ||
valueToModify: 'targets', | ||
codeBlock: codeBlock | ||
} | ||
|
||
const modifiedJson = addJsonKeyValue(mockEditJson, mockJson); | ||
const expected = { | ||
"test": "123", | ||
"targets": { | ||
"server": { | ||
"executor": "@angular-devkit/build-angular:server" | ||
} | ||
} | ||
}; | ||
|
||
expect(modifiedJson).toEqual(expected); | ||
}); | ||
|
||
it('should add a key value', () => { | ||
const mockJson = `{ | ||
"scripts": { | ||
"test": "npm run test" | ||
} | ||
}`; | ||
|
||
const mockEditJson: EditJson = { | ||
nodeType: 'editJson', | ||
valueToModify: 'scripts', | ||
codeBlock: '{"test:ci": "npm run nx -- run-many --target=test --all --parallel --coverage --coverageReporters=lcov && node ./tools/coverageMerger.js"}' | ||
} | ||
|
||
const modifiedJson = addJsonKeyValue(mockEditJson, mockJson); | ||
const expected = { | ||
scripts: { | ||
"test": "npm run test", | ||
"test:ci": "npm run nx -- run-many --target=test --all --parallel --coverage --coverageReporters=lcov && node ./tools/coverageMerger.js" | ||
} | ||
}; | ||
|
||
expect(modifiedJson).toEqual(expected); | ||
}); | ||
|
||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { JSONPath } from 'jsonpath-plus'; | ||
import { EditJson } from "../interfaces/json-morph.interface"; | ||
import * as pointer from 'json-pointer'; | ||
|
||
export function addJsonKeyValue(editJson: EditJson, json: string): any { | ||
// double json parse hack to make sure /n are removed from string | ||
const codeBlock = typeof editJson.codeBlock === 'string' ? JSON.parse(JSON.parse(JSON.stringify(editJson.codeBlock))) : editJson.codeBlock; | ||
json = JSON.parse(json); | ||
//Get Pointer | ||
const JsonPointer = JSONPath({path: `$..${editJson.valueToModify}`, json, resultType: 'pointer'}); | ||
const firstJsonMatchedPointer = JsonPointer[0]; | ||
|
||
//Get value of json | ||
const jsonToEdit = pointer.get(json as any, firstJsonMatchedPointer); | ||
|
||
//Set value | ||
pointer.set(json as any, firstJsonMatchedPointer, {...jsonToEdit, ...codeBlock}); | ||
|
||
//Return modified value | ||
return json; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
src/rz/json/snapshots/nested-package-json/nested-package-output.json.snap
This file was deleted.
Oops, something went wrong.
59 changes: 0 additions & 59 deletions
59
src/rz/json/snapshots/nested-package-json/nested-package.json.snap
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
src/rz/json/snapshots/package-json/package-output.json.snap
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.