From adbba8810a291e9c20656a4cb5cce4534f599e49 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Thu, 14 Dec 2017 18:00:15 +0330 Subject: [PATCH 1/5] add json to file function --- snippets/write-json-to-file.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 snippets/write-json-to-file.md diff --git a/snippets/write-json-to-file.md b/snippets/write-json-to-file.md new file mode 100644 index 000000000..6a0ae21a0 --- /dev/null +++ b/snippets/write-json-to-file.md @@ -0,0 +1,9 @@ +### Write a JSON to a file + +Write a `json` object to a `.json` file. +```js +const fs = require('fs') + +const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 4)) +// jsonToFile({test: "is passed"}, 'testJsonFile') +``` From 99b900853cf82910be517647ecbafc2bc11b8d8b Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Thu, 14 Dec 2017 18:34:05 +0330 Subject: [PATCH 2/5] add a functions that deeply cleans json objects from unwanted keys --- snippets/json-cleaner.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/json-cleaner.md diff --git a/snippets/json-cleaner.md b/snippets/json-cleaner.md new file mode 100644 index 000000000..46ef745dc --- /dev/null +++ b/snippets/json-cleaner.md @@ -0,0 +1,20 @@ +### Clean Json objects + +Clean your json object from unwanted keys, deeply. +provide set of `keys` to keep and an indicator for `children` if there is any. + +```js +const cleanObj = (obj, keys = [], childIndicator) => { + Object.keys(obj).forEach(key => { + if (key === childIndicator) { + cleanObj(obj[key], keys, childIndicator) + } else if (!keys.includes(key)) { + delete obj[key] + } + }) +} +/* + dirtyObj = { a: 1, b: 2, children: {a: 1, b :2}} + let cleaned = cleanObj(dirtyObj, [a]) // { a: 1, children : { a: 1}} + */ +``` From bec3c5afa6d15160675d42145c904eb78132831c Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 12:56:13 +0330 Subject: [PATCH 3/5] removing unrelated file from "to merge" branch --- snippets/json-cleaner.md | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 snippets/json-cleaner.md diff --git a/snippets/json-cleaner.md b/snippets/json-cleaner.md deleted file mode 100644 index 46ef745dc..000000000 --- a/snippets/json-cleaner.md +++ /dev/null @@ -1,20 +0,0 @@ -### Clean Json objects - -Clean your json object from unwanted keys, deeply. -provide set of `keys` to keep and an indicator for `children` if there is any. - -```js -const cleanObj = (obj, keys = [], childIndicator) => { - Object.keys(obj).forEach(key => { - if (key === childIndicator) { - cleanObj(obj[key], keys, childIndicator) - } else if (!keys.includes(key)) { - delete obj[key] - } - }) -} -/* - dirtyObj = { a: 1, b: 2, children: {a: 1, b :2}} - let cleaned = cleanObj(dirtyObj, [a]) // { a: 1, children : { a: 1}} - */ -``` From cb488b39742979d664e883f84246b7c4d2208c5d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 15 Dec 2017 13:15:21 +0200 Subject: [PATCH 4/5] Update write-json-to-file.md --- snippets/write-json-to-file.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/write-json-to-file.md b/snippets/write-json-to-file.md index 6a0ae21a0..5cff72064 100644 --- a/snippets/write-json-to-file.md +++ b/snippets/write-json-to-file.md @@ -1,9 +1,9 @@ ### Write a JSON to a file -Write a `json` object to a `.json` file. -```js -const fs = require('fs') +Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` object to a `.json` file. -const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 4)) +```js +const fs = require('fs'); +const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2)) // jsonToFile({test: "is passed"}, 'testJsonFile') ``` From 564486472469851e0ff92fb4045afbe795b7c991 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 15 Dec 2017 13:15:51 +0200 Subject: [PATCH 5/5] Update write-json-to-file.md --- snippets/write-json-to-file.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/write-json-to-file.md b/snippets/write-json-to-file.md index 5cff72064..17de5a44e 100644 --- a/snippets/write-json-to-file.md +++ b/snippets/write-json-to-file.md @@ -5,5 +5,5 @@ Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` ```js const fs = require('fs'); const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2)) -// jsonToFile({test: "is passed"}, 'testJsonFile') +// jsonToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json' ```