Skip to content
Open
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
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ function xgettext(input, options, cb) {
if (options['files-from']) {
input = fs.readFileSync(options['files-from'], options['from-code'])
.split('\n')
Copy link
Collaborator

Choose a reason for hiding this comment

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

The combination of this line and the lines you added feels a bit like a mix of 2 solutions.

I'd choose either of these:

  • If you want to follow GNU gettext as close as possible, use readline to do the line splitting. After that you can use your code to do the trimming (\t \r \n and space). Ideally a future commit/PR can take care of ignoring lines that start with # (comments).
  • Or use .split(/\r\n|\r|\n/) to split immediately and forget about \t and space.

To get the core functionality you were after, I'm perfectly fine with the second option. Of course getting close to GNU gettext is the goal, but this would already be a step in that direction.

.map(function (line) {
return line.split('').reduceRight(function(acc, c) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

split('') has some issues. You can use Array.from(line) since we only support node 6 and up.

if (' \t\r'.indexOf(c) === -1 || acc.length > 0) {
return c + acc;
}
return acc;
}, '');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, having to loop through all characters just to remove the last ones feels quite heavy.
Can't this be done with a regex? Something like line.replace(/[\t\r ]+$/, '')?

})
.filter(function (line) {
return line.trim().length > 0;
});
Expand Down
20 changes: 20 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ describe('CLI', function () {
});
});

it('should read input from a CR LF delimited file', function (done) {
run(['--output=-', '--files-from=fixtures/list-crlf.txt'], function (err) {
throw new Error(err);
}, function (code, data) {
assert.equal(0, code);
assert(data.match('This is a fixed sentence'));
done();
});
});

it('should read input from a HT delimited file', function (done) {
run(['--output=-', '--files-from=fixtures/list-ht.txt'], function (err) {
throw new Error(err);
}, function (code, data) {
assert.equal(0, code);
assert(data.match('This is a fixed sentence'));
done();
});
});

it('should handle stdin input', function (done) {
var child = run(['--output=-', '--language=Handlebars', '--from-code=utf8', '-'], function (err) {
throw new Error(err);
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/list-crlf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixtures/template.hbs
1 change: 1 addition & 0 deletions test/fixtures/list-ht.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixtures/template.hbs