Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The solutions are best read in the following order:
- [__righto.js__](./righto.js)
- [__promises.js__](./promises.js)
- [__promises-ramda.js__](./promises-ramda.js)
- [__bluebird-promisell.js__](./bluebird-promisell.js)
- [__most.js__](./most.js)
- [__coroutines-co.js__](./coroutines-co.js)
- [__coroutines-bluebird.js__](./coroutines-bluebird.js)
Expand Down
56 changes: 56 additions & 0 deletions bluebird-promisell.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const fs = require('fs');
const path = require('path');

const Promise = require('bluebird');
const P = require('bluebird-promisell');
const R = require('ramda');
const S = require('sanctuary');

// data Text = Buffer | String
// readFile :: String -> String -> Promise Error Text
const readFile = R.curry((encoding, filename) =>
new Promise((resolve, reject) => {
fs.readFile(filename, {encoding: encoding}, (err, data) => {
if (err != null) {
reject(err);
} else {
resolve(data);
}
});
})
);

// readFileWithDirAndName :: String -> String -> String -> Promise Error String
const readFileWithDirAndName = R.curry((encoding, dir, name) => {
const filePath = path.join(dir, name);
return readFile(encoding)(filePath);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's inconsistent for readFileWithDirAndName to specify the encoding while readFile requires the caller to provide it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I get it correctly. Are you suggestion making the encoding as an option, like this?

//    makeEncoding :: String -> Encoding
const makeEncoding = R.objOf('encoding');

//    Encoding
const UTF8 = makeEncoding('utf8');

//    readFileWithDirAndName :: Encoding -> String -> String -> Promise Error String
const readFileWithDirAndName = R.curryN(3, (encoding, dir, name) => {
  const filePath = path.join(dir, name);
  return readFile(encoding)(filePath);
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that if readFile takes encoding as an argument, so should readFileWithDirAndName. Conversely, if readFileWithDirAndName does not take encoding as an argument, neither should readFile. It seems to me these two functions should be consistent in this respect unless there's a reason for them not to be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Yeah, it makes sense. 👍

Updated!


// readFilesWithDirAndNames :: String -> [String] -> Promise Error [String]
const readFilesWithDirAndNames = R.compose(P.traversep, readFileWithDirAndName);

// concatFiles :: String -> Promise Error String
const concatFiles = dir => {
const indexFileP = readFileWithDirAndName('utf8', dir, 'index.txt');
const fileNamesP = P.liftp1(S.lines)(indexFileP);
const readFilesWithNames = readFilesWithDirAndNames('utf8', dir);
const filesP = P.liftp1(readFilesWithNames)(fileNamesP);
return P.liftp1(R.join(''))(filesP);
};


const main = () => {
concatFiles(process.argv[2])
.then(data => {
process.stdout.write(data);
process.exit(0);
},
err => {
process.stderr.write(String(err) + '\n');
process.exit(1);
});
};

if (process.mainModule.filename === __filename) main();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"async": "1.2.x",
"bluebird": "2.9.x",
"bluebird-promisell": "0.4.x",
"co": "4.5.x",
"data.task": "3.0.x",
"foreign": "1.0.x",
Expand Down
1 change: 1 addition & 0 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ test kgo.js
test righto.js
test promises.js
test promises-ramda.js
test bluebird-promisell.js
test most.js
test coroutines-co.js
test coroutines-bluebird.js
Expand Down