Skip to content

Commit

Permalink
NEW: option: separator (#4)
Browse files Browse the repository at this point in the history
closes #4
  • Loading branch information
dwhieb authored Jul 6, 2020
1 parent 15b2d34 commit 820ea51
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ The value of the `aligned` variable will be:

## Options

Option | Default | Description
-----------------|---------|-------------------------------------------------------------------------------------------------------
`alignmentError` | `false` | Specifies whether the library should throw an error if all lines do not have the same number of words.
Option | Default | Description
-----------------|----------|-------------------------------------------------------------------------------------------------------
`alignmentError` | `false` | Specifies whether the library should throw an error if all lines do not have the same number of words.
`separator` | `spaces` | Specifies whether words should be aligned using spaces or tabs. Allowed values: `spaces`, `tabs`.

[actions]: https://github.com/digitallinguistics/word-aligner/actions?query=workflow%3Atest
[GitHub]: https://github.com/digitallinguistics/word-aligner
Expand Down
35 changes: 23 additions & 12 deletions alignWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

function alignWords(lines, options = {}) {

const { alignmentError = false } = options;
const {
alignmentError = false,
separator = `spaces`,
} = options;

const whiteSpaceRegExp = /\s+/gu;

Expand All @@ -28,22 +31,30 @@ function alignWords(lines, options = {}) {

const longestLine = lines[indexOfLongestLine];

longestLine.forEach((word, i) => {
if (separator === `tabs`) {

const longestWordLength = lines.reduce((len, line) => {
if (!line[i]) return len;
if (line[i].length > len) return line[i].length;
return len;
}, 0);
lines = lines.map(line => line.join(`\t`).trim());

} else {

longestLine.forEach((word, i) => {

const longestWordLength = lines.reduce((len, line) => {
if (!line[i]) return len;
if (line[i].length > len) return line[i].length;
return len;
}, 0);

lines.forEach(words => {
if (!words[i]) return;
words[i] = words[i].padEnd(longestWordLength);
});

lines.forEach(words => {
if (!words[i]) return;
words[i] = words[i].padEnd(longestWordLength);
});

});
lines = lines.map(line => line.join(` `).trim());

lines = lines.map(line => line.join(` `).trim());
}

return lines;

Expand Down
30 changes: 27 additions & 3 deletions alignWords.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ describe(`alignWords`, function() {
];

const expectedResult = [
'waxdungu qasi',
'waxt-qungu qasi',
'day-one man',
`waxdungu qasi`,
`waxt-qungu qasi`,
`day-one man`,
];

const aligned = alignWords(lines);
Expand All @@ -47,4 +47,28 @@ describe(`alignWords`, function() {

});

it(`option: separator`, function() {

const lines = [
`waxdungu qasi`,
`waxt-qungu qasi`,
`day-one man`,
];

const expectedResult = [
`waxdungu\tqasi`,
`waxt-qungu\tqasi`,
`day-one\tman`,
];

const aligned = alignWords(lines, { separator: `tabs` });

aligned.forEach((line, i) => {

expect(line).to.be(expectedResult[i]);

});

});

});

0 comments on commit 820ea51

Please sign in to comment.