Skip to content

Instantly share code, notes, and snippets.

@FagnerMartinsBrack
Last active February 11, 2021 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FagnerMartinsBrack/69da85976eea5e642164180e03f42489 to your computer and use it in GitHub Desktop.
Save FagnerMartinsBrack/69da85976eea5e642164180e03f42489 to your computer and use it in GitHub Desktop.
(Medium) Why Small Modules Matter - End Result
const readFileAsJSON = require("./read-file");
const writeFileToJSON = require("./write-file");
const flattenArray = require("flatten-array");
module.exports = function flattenArrayInsideFile(fileName) {
return readFileAsJSON(fileName)
.then(function(parsedContent) {
const flattenedArray = flattenArray(parsedContent);
return writeFileToJSON(fileName, flattenedArray);
})
.then(function() {
console.log("Done!");
});
};
// async/await
module.exports = async function flattenArrayInsideFile(fileName) {
const parsedContent = await readFileAsJSON(fileName);
const flattenedArray = flattenArray(parsedContent);
await writeFileToJSON(fileName, flattenedArray);
console.log("Done!");
};
@gtkatakura
Copy link

What do you think of this idea?

update-file

const readFileAsJSON = require("./read-file");
const writeFileToJSON = require("./write-file");

module.exports = function updateFileAsJSON(fileName, transformation) {
  return readFileAsJSON(fileName)
    .then(function(parsedContent) {
      return writeFileToJSON(fileName, transformation(parsedContent));
    });
};

flatten-array-inside-file

const updateFileAsJSON = require("./update-file");
const flattenArray = require("flatten-array");

module.exports = function flattenArrayInsideFile(fileName) {
  return updateFileAsJSON(fileName, flattenArray)
    .then(function() {
      console.log("Done!");
    });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment