Skip to content

Commit 82f97d4

Browse files
committed
enhanced sortedBy
1 parent 73d45bd commit 82f97d4

File tree

4 files changed

+91
-51
lines changed

4 files changed

+91
-51
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ $ book sm --help
8181

8282
**Notes**
8383
* The article title is taken from `title` property in the articles front-matter. If this property is not available, the articles filename will be used as title for the summary.
84-
* The option `-s` or `--sortedBy` can not be given `-` as argument, because commander.js will parse it an option. But you can set it in `book.json` as follows.
85-
84+
* The option `-s` or `--sortedBy` can not be given `-` as argument, because commander.js will parse it an option. But you can set it in `book.json` as follows.
85+
* set up the sortedBy and if there are any summaries missing the order, look at the example below and follow,
86+
for example, you have summaries like this `01-elementry-school, 02-middle-school, 03-university, ...`
87+
you realized high school was missing, then You can make correct order in the following way
88+
eg. `01-elementry-school, 02-middle-school, 02a-high-school, 03-university, ...`
89+
not `01-elementry-school, 02-middle-school, 03-high-school, 04-university, ...`
8690

8791
2> Create a `book.json` in the book`s root folder
8892

lib/files.js

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,43 @@
11
var _ = require('lodash');
22
var fs = require('fs');
33
var path = require('path');
4-
var async = require('async');
54
var color = require('bash-color');
65
var fm = require('front-matter');
76

7+
// separate for test
8+
var sort = function(current, next, sortedBy) {
9+
if (current.isDirectory && !next.isDirectory) return -1;
10+
if (!current.isDirectory && next.isDirectory) return 1;
11+
// Sorted if given current sorted hyphen, for example: `-` or `_`
12+
if (sortedBy) {
13+
var pattern = "(^\\d*)" + sortedBy;
14+
var reg = new RegExp(pattern);
15+
if(current.name.match(reg) && next.name.match(reg)){
16+
var currentNum = current.name.match(reg)[1];
17+
var nextNum = next.name.match(reg)[1];
18+
return currentNum - nextNum;
19+
}
20+
}
21+
return current.name.localeCompare(next.name);
22+
};
23+
824
// Use a loop to read all files
925
function ReadFile(filePath, filesJson, sortedBy) {
1026
var files;
1127

12-
try {
13-
// Synchronous readdir
14-
files = fs.readdirSync(filePath)
15-
// sort the files: directories first, afterwards files
16-
.map(function(v) {
17-
var stat = fs.statSync(path.resolve(filePath, v));
18-
return {
19-
name: v,
20-
isDirectory: stat.isDirectory()
21-
};
22-
})
23-
.sort(function(a, b) {
24-
if (a.isDirectory && !b.isDirectory) return -1;
25-
if (!a.isDirectory && b.isDirectory) return 1;
26-
// Sorted if given a sorted hyphen, for example: `-` or `_`
27-
if (sortedBy) {
28-
var pattern = "(^[\\da-zA-Z]*)" + sortedBy;
29-
var reg = RegExp(pattern);
30-
if(a.name.match(reg) && b.name.match(reg)){
31-
var aNum = a.name.match(reg)[1];
32-
var bNum = b.name.match(reg)[1];
33-
return aNum - bNum;
34-
}
35-
}
36-
return a.name.localeCompare(b.name);
37-
})
38-
.map(function(v) {
39-
return v.name;
40-
});
28+
function getFrontMatterTitle(newpath)
29+
{
30+
// default to use filename
31+
var title = path.parse(newpath).name;
32+
var content = fs.readFileSync(newpath,'utf8');
4133

42-
files.forEach(walk);
43-
} catch (error) {
44-
filesJson = null; //fixme
45-
console.log(color.red(error.message));
34+
if (!fm.test(content)) return title; // skip if no front matter
35+
36+
var frontMatter = fm(content);
37+
if (typeof frontMatter.attributes.title === "undefined") return title; // skip if no 'title' attributes
38+
// todo: set 'title' via config
39+
40+
return frontMatter.attributes.title;
4641
}
4742

4843
function walk(file) {
@@ -51,7 +46,7 @@ function ReadFile(filePath, filesJson, sortedBy) {
5146

5247
if (state.isDirectory()) {
5348
filesJson[file] = {};
54-
ReadFile(newpath, filesJson[file], sortedBy);
49+
new ReadFile(newpath, filesJson[file], sortedBy);
5550
// filter empty directories
5651
if (Object.keys(filesJson[file]).length < 1) {
5752
delete filesJson[file];
@@ -66,20 +61,32 @@ function ReadFile(filePath, filesJson, sortedBy) {
6661
}
6762
}
6863

69-
function getFrontMatterTitle(newpath)
70-
{
71-
// default to use filename
72-
var title = path.parse(newpath).name;
73-
var content = fs.readFileSync(newpath,'utf8');
74-
75-
if (!fm.test(content)) return title; // skip if no front matter
76-
77-
var frontMatter = fm(content);
78-
if (typeof frontMatter.attributes.title === "undefined") return title; // skip if no 'title' attributes
79-
// todo: set 'title' via config
64+
try {
65+
// Synchronous readdir
66+
files = fs.readdirSync(filePath)
67+
// sort the files: directories first, afterwards files
68+
.map(function(v) {
69+
var stat = fs.statSync(path.resolve(filePath, v));
70+
return {
71+
name: v,
72+
isDirectory: stat.isDirectory()
73+
};
74+
})
75+
.sort(function(current, next) {
76+
return sort(current, next, sortedBy);
77+
})
78+
.map(function(v) {
79+
return v.name;
80+
});
8081

81-
return frontMatter.attributes.title;
82+
files.forEach(walk);
83+
} catch (error) {
84+
filesJson = null; //fixme
85+
console.log(color.red(error.message));
8286
}
8387
}
8488

85-
module.exports = ReadFile;
89+
module.exports = {
90+
readFile: ReadFile,
91+
sort: sort
92+
};

lib/summary/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var async = require('async');
66

77
var bookJson = require('../bookJson');
88
var utils = require('../utils');
9-
var readFile = require('../files');
9+
var readFile = require('../files').readFile;
1010

1111
/**
1212
* Get a summary from a fold such as `/path/to/your/book` or `../book`

test/files.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var should = require('should');
2+
var sort = require('../lib/files').sort;
3+
4+
describe('files.js', function () {
5+
var DirClass = function(name) {
6+
this.name = name;
7+
this.isDirectory = true;
8+
};
9+
10+
it('test sort with alphabet', function () {
11+
var order = [new DirClass('01-a'), new DirClass('02-a'), new DirClass('03a-a'), new DirClass('03-a'), new DirClass('04-a')];
12+
var sorted = order.sort(function(current, next) {
13+
return sort(current, next, '-');
14+
});
15+
16+
should.deepEqual(sorted,
17+
[new DirClass('01-a'), new DirClass('02-a'), new DirClass('03-a'), new DirClass('03a-a'), new DirClass('04-a')]);
18+
});
19+
20+
it('test sort only digit', function () {
21+
var order = [new DirClass('01-a'), new DirClass('2-a'), new DirClass('7-a'), new DirClass('10-a'), new DirClass('04-a')];
22+
var sorted = order.sort(function(current, next) {
23+
return sort(current, next, '-');
24+
});
25+
26+
should.deepEqual(sorted,
27+
[new DirClass('01-a'), new DirClass('2-a'), new DirClass('04-a'), new DirClass('7-a'), new DirClass('10-a')]);
28+
});
29+
});

0 commit comments

Comments
 (0)