This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* max-depth and complexity errors * feat(textJustification): Added text justification function Closes #195
- Loading branch information
1 parent
0f94be7
commit d39b0aa
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
export default textJustification | ||
|
||
/** | ||
* | ||
* Original Source: https://www.malikbrowne.com/blog/text-justification-coding-question | ||
* | ||
* Description: Given an array of words(input) and a length L, format the text such that each | ||
* line has exactly L characters and is fully (left and right) justified. | ||
* | ||
* @param {Array} input - String array to be justified | ||
* @param {Integer} L - Length of characters per line | ||
* @return {Array} output - The string fully justified | ||
*/ | ||
|
||
function textJustification(input, L) { | ||
let index = 0 | ||
const output = [] | ||
|
||
while (index < input.length) { | ||
|
||
let count = input[index].length | ||
let last = index + 1 | ||
|
||
while (last < input.length) { | ||
|
||
if (input[last].length + count + 1 > L) { | ||
break | ||
} | ||
count += input[last].length + 1 | ||
last++ | ||
} | ||
|
||
let line = '' | ||
const diff = last - index - 1 | ||
|
||
if (last === input.length || diff === 0) { | ||
|
||
for (let i = index; i < last; i++) { | ||
|
||
line += `${input[i]} ` | ||
} | ||
|
||
line = line.substr(0, line.length - 1) | ||
|
||
for (let i = line.length; i < L; i++) { | ||
line += ' ' | ||
} | ||
|
||
} else { | ||
|
||
const space = (L - count) / diff | ||
const remainder = (L - count) % diff | ||
|
||
for (let i = index; i < last; i++) { | ||
line += input[i] | ||
|
||
if (i < last - 1) { | ||
|
||
const max = space + ((i - index) < remainder ? 1 : 0) | ||
|
||
for (let j = 0; j <= max; j++) { | ||
line += ' ' | ||
} | ||
} | ||
} | ||
} | ||
|
||
output.push(line) | ||
index = last | ||
} | ||
return output | ||
} | ||
|
||
/* eslint complexity: "off", max-depth: "off" */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import test from 'ava' | ||
import {textJustification} from '../src' | ||
|
||
test('Text justification function', t => { | ||
const object = ['This','is','an','example','of','text','justification.'] | ||
const len = 16 | ||
const expected = ['This is an','example of text','justification. '] | ||
const actual = textJustification(object, len) | ||
t.deepEqual(actual, expected) | ||
}) |