Permalink
Browse files

Add solution branch.

With separated concerns per #7
Refresh for cohort 016.
  • Loading branch information...
racheltstevens
racheltstevens committed Feb 24, 2017
1 parent 13200f7 commit 6afd874fea61230d095746ec44bbebb46cc5ac37
Showing with 116 additions and 4 deletions.
  1. +25 −0 bin/copy-json.js
  2. +8 −0 bin/hey-yall.js
  3. +9 −0 bin/randomizer.js
  4. +23 −2 lib/copy-json.js
  5. +21 −1 lib/hey-yall.js
  6. +30 −1 lib/randomizer.js
@@ -1,11 +1,36 @@
'use strict';

// import the function we're going to use to copy JSON
const copyJson = require('../lib/copy-json.js');

// define what standard-in and standard-out are
// for this script's purposes stdin will be what we type IN to the terminal
const stdin = '/dev/stdin';

// and stdout will be what the terminal spits out
const stdout = '/dev/stdout';

// COMMAND LINE: node copy-json.js data/names.json ...
// process.argv === ['node', 'copy-json.js', 'data/names.json', ...]

// define inFile as the 3rd argument from the command line
let inFile = process.argv[2] === '-' ? stdin : process.argv[2];

// if(process.argv[2] === '-')
// inFile = stdin;
// else {
// inFile = process.argv[2];
// }

// outFile is going to be the 4th argument passed on the command line
// OR
// spit out in our terminal (stdout)
let outFile = process.argv[3] ? process.argv[3] : stdout;

// if outfile is stdout we need to set the flag to append (so we dont overwrite
// our terminal)
// otherwise, we want to overwrite the file and replace it with new contents
let outFileFlag = outFile === stdout ? 'a' : 'w';

// call our copying function with the files and the flag we're using
copyJson(inFile, outFile, outFileFlag);
@@ -1,3 +1,11 @@
'use strict';

const heyYall = require('../lib/hey-yall.js');

const inFile = process.argv[2];

if (!inFile) {
throw('Script requires one arguments, an input file');
}

heyYall(inFile);
@@ -1,3 +1,12 @@
'use strict';

const randomizer = require('../lib/randomizer.js');

let inFile = process.argv[2];
let outFile = process.argv[3];

if (!outFile) {
throw('Script requires two arguments, an input and an output file');
}

randomizer(inFile, outFile);
@@ -1,31 +1,52 @@
'use strict';

// grab fs module and import it
const fs = require('fs');

const copyJson = function (inFile, outFile, outFileFlag) {
// use fs to run readfile, pass it the inFile, encode in utf8, and declare a
// callback.
// the callback takes (as arguments) 'error' and 'data'
fs.readFile(inFile, { encoding: 'utf8' }, (error, data) => {
let json, pojo;
// a string!
let json;

// plain old javascript object
let pojo;
if (error) {
console.error(error.stack);
return;
}

// try/catch block.
// try the thing. if don't work, catch the error
try {
// if (data) is json, and JSON.parse is able to do its job we're good
pojo = JSON.parse(data);
} catch (error) {
// otherwise console.error the error
console.error(error.stack);
return;
}

// do something with the pojo ? - We're not doing anything here

// turn the pojo back into json
json = JSON.stringify(pojo, null, 2);

// use writeFile to "write a file"
// takes the outFile (file we're writing to)
// json (the pojo stringified from line 56)
// the flag (set on line 30) 'a' or 'w' depending on where we want our output
// and a callback which only has one argument (error)
fs.writeFile(outFile, json, { flag: outFileFlag }, error => {
if (error) {
console.error(error.stack);
return;
}

console.error('\ncopied');
// let's us know that the function ran successfully
console.log('\ncopied');
});
});
};
@@ -2,8 +2,28 @@

const fs = require('fs');

const heyYall = function () {
const heyYall = function (inFile) {
// fs.readFile(file, options, callback)
fs.readFile(inFile, 'utf-8', (error, data) => {
if (error) {
console.error(error.stack);
return;
}

let dataArray = data.split('\n');
dataArray.pop();

dataArray.forEach((name) => {
console.log('HEYYYYY ', name);
});

// const showNames = function(name){
// console.log("HEYYYY ", name);
// };
//
// dataArray.forEach(showNames);

});
};

module.exports = heyYall;
@@ -2,8 +2,37 @@

const fs = require('fs');

const randomizer = function () {
const randomizer = function (inFile, outFile) {
fs.readFile(inFile, 'utf-8', (error, data) => {
if (error) {
console.error(error.stack);
return;
}

let dataArray = data.split('\n');

// remove line at the end of the file
dataArray = dataArray.slice(0, dataArray.length - 1);

// randomize the array
let randomLines = dataArray.sort(function () {
const n = Math.random();
const randomSorter = n < 0 ? Math.floor(n) : Math.ceil(n);
return randomSorter;
});

// add empty line back
randomLines.push('');

fs.writeFile(outFile, randomLines.join('\n'), { flag: 'w' }, error => {
if (error) {
console.error(error.stack);
return;
}

console.log('\nrandomized!');
});
});
};

module.exports = randomizer;

0 comments on commit 6afd874

Please sign in to comment.