Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit 6d8bc21

Browse files
Merge pull request #29 from zhangchiqing/master
bluebird-promisell version
2 parents d15c4f8 + a02dfe8 commit 6d8bc21

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The solutions are best read in the following order:
2424
- [__righto.js__](./righto.js)
2525
- [__promises.js__](./promises.js)
2626
- [__promises-ramda.js__](./promises-ramda.js)
27+
- [__bluebird-promisell.js__](./bluebird-promisell.js)
2728
- [__most.js__](./most.js)
2829
- [__coroutines-co.js__](./coroutines-co.js)
2930
- [__coroutines-bluebird.js__](./coroutines-bluebird.js)

bluebird-promisell.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const Promise = require('bluebird');
7+
const P = require('bluebird-promisell');
8+
const R = require('ramda');
9+
const S = require('sanctuary');
10+
11+
// data Text = Buffer | String
12+
// readFile :: String -> String -> Promise Error Text
13+
const readFile = R.curry((encoding, filename) =>
14+
new Promise((resolve, reject) => {
15+
fs.readFile(filename, {encoding: encoding}, (err, data) => {
16+
if (err != null) {
17+
reject(err);
18+
} else {
19+
resolve(data);
20+
}
21+
});
22+
})
23+
);
24+
25+
// readFileWithDirAndName :: String -> String -> String -> Promise Error String
26+
const readFileWithDirAndName = R.curry((encoding, dir, name) => {
27+
const filePath = path.join(dir, name);
28+
return readFile(encoding)(filePath);
29+
});
30+
31+
// readFilesWithDirAndNames :: String -> [String] -> Promise Error [String]
32+
const readFilesWithDirAndNames = R.compose(P.traversep, readFileWithDirAndName);
33+
34+
// concatFiles :: String -> Promise Error String
35+
const concatFiles = dir => {
36+
const indexFileP = readFileWithDirAndName('utf8', dir, 'index.txt');
37+
const fileNamesP = P.liftp1(S.lines)(indexFileP);
38+
const readFilesWithNames = readFilesWithDirAndNames('utf8', dir);
39+
const filesP = P.liftp1(readFilesWithNames)(fileNamesP);
40+
return P.liftp1(R.join(''))(filesP);
41+
};
42+
43+
44+
const main = () => {
45+
concatFiles(process.argv[2])
46+
.then(data => {
47+
process.stdout.write(data);
48+
process.exit(0);
49+
},
50+
err => {
51+
process.stderr.write(String(err) + '\n');
52+
process.exit(1);
53+
});
54+
};
55+
56+
if (process.mainModule.filename === __filename) main();

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"dependencies": {
88
"async": "1.2.x",
99
"bluebird": "2.9.x",
10+
"bluebird-promisell": "0.4.x",
1011
"co": "4.5.x",
1112
"data.task": "3.0.x",
1213
"foreign": "1.0.x",

test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ test kgo.js
5757
test righto.js
5858
test promises.js
5959
test promises-ramda.js
60+
test bluebird-promisell.js
6061
test most.js
6162
test coroutines-co.js
6263
test coroutines-bluebird.js

0 commit comments

Comments
 (0)