Permalink
Browse files

Separate concerns

Per ga-wdi-boston/node-api#7
Fixes randomizer script ga-wdi-boston/node-api#18
Moves process.argv issues to `bin/<file-name>`
Keeps other logic in `lib/<file-name>`
Uses string interpolation in `lib/hey-yall.js`
  • Loading branch information...
racheltstevens
racheltstevens committed Feb 27, 2017
1 parent ffe762c commit 09e022f6a50b0dff0bfa76b3041e1fcb927c7120
Showing with 97 additions and 60 deletions.
  1. +13 −0 bin/copy-json.js
  2. +11 −0 bin/hey-yall.js
  3. +11 −0 bin/randomizer.js
  4. +28 −32 lib/copy-json.js
  5. +15 −13 lib/hey-yall.js
  6. +19 −15 lib/randomizer.js
@@ -0,0 +1,13 @@
'use strict';

const copyJson = require('../lib/copy-json.js');

const stdin = '/dev/stdin';
const stdout = '/dev/stdout';

let inFile = process.argv[2] === '-' ? stdin : process.argv[2];
let outFile = process.argv[3] ? process.argv[3] : stdout;

let outFileFlag = outFile === stdout ? 'a' : 'w';

copyJson(inFile, outFile, outFileFlag);
@@ -0,0 +1,11 @@
'use strict';

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

const inFile = process.argv[2];

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

heyYall(inFile);
@@ -0,0 +1,11 @@
'use strict';

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

let inFile = process.argv[2];

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

randomizer(inFile);
@@ -2,41 +2,37 @@

const fs = require('fs');

const stdin = '/dev/stdin';
const stdout = '/dev/stdout';

//
let inFile = process.argv[2] === '-' ? stdin : process.argv[2];
let outFile = process.argv[3] ? process.argv[3] : stdout;
let outFileFlag = outFile === stdout ? 'a' : 'w';

fs.readFile(inFile, { encoding: 'utf8' }, (error, data) => {
let json, pojo;
if (error) {
console.error(error.stack);
return;
}

// parse the data into JSON
try {
pojo = JSON.parse(data);
} catch (error) {
console.error(error.stack);
return;
}

// do something with the pojo

// make a string out of the pojo
json = JSON.stringify(pojo, null, 2);

// save it
fs.writeFile(outFile, json, { flag: outFileFlag }, error => {
const copyJson = function (inFile, outFile, outFileFlag) {
fs.readFile(inFile, { encoding: 'utf8' }, (error, data) => {
let json, pojo;
if (error) {
console.error(error.stack);
return;
}

console.error('\ncopied');
// parse the data into JSON
try {
pojo = JSON.parse(data);
} catch (error) {
console.error(error.stack);
return;
}

// do something with the pojo

// make a string out of the pojo
json = JSON.stringify(pojo, null, 2);

// save it
fs.writeFile(outFile, json, { flag: outFileFlag }, error => {
if (error) {
console.error(error.stack);
return;
}

console.error('\ncopied');
});
});
});
};

module.exports = copyJson;
@@ -2,21 +2,23 @@

const fs = require('fs');

let inFile = process.argv[2];
const heyYall = function (inFile) {
fs.readFile(inFile, { encoding: 'utf8' }, (error, content) => {
if (error) {
console.error(error);
}

fs.readFile(inFile, { encoding: 'utf8' }, (error, content) => {
if (error) {
console.error(error);
}
// 'Billy\nJames\nNick\n' --> ['Billy', 'James', 'Nick']

// 'Billy\nJames\nNick\n' --> ['Billy', 'James', 'Nick']
let lines = content.split('\n');

let lines = content.split('\n');
// clean up the array by removing the empty line
lines.pop();

// clean up the array by removing the empty line
lines.pop();

lines.forEach((line) => {
console.log('Hello, ' + line + '!');
lines.forEach((line) => {
console.log(`Hello, ${line}!`);
});
});
});
};

module.exports = heyYall;
@@ -1,24 +1,28 @@
'use strict';
const fs = require('fs');

let inFile = process.argv[2];
const randomizer = function (inFile) {
fs.readFile(inFile, { encoding: 'utf8' }, (error, content) => {
//first thing is check error
if (error) {
console.error(error);
}

fs.readFile(inFile, { encoding: 'utf8' }, (error, content) => {
//first thing is check error
if (error) {
console.error(error);
}
let lines = content.split('\n');

let lines = content.split('\n');
//clean up the array
lines.pop();

//clean up the array
lines.pop();
let randomLines = lines.sort(function () {
const n = 0.5 - Math.random();
const randomSorter = n < 0 ? Math.floor(n) : Math.ceil(n);
return randomSorter;
});

let randomLines = lines.sort(function () {
return 0.5 - Math.random();
randomLines.forEach((line) => {
console.log(line);
});
});
};

randomLines.forEach((line) => {
console.log(line);
});
});
module.exports = randomizer;

0 comments on commit 09e022f

Please sign in to comment.