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

Commit d39b0aa

Browse files
Jonathan-MontanezKent C. Dodds
authored and
Kent C. Dodds
committed
feat: text justification (#198)
* max-depth and complexity errors * feat(textJustification): Added text justification function Closes #195
1 parent 0f94be7 commit d39b0aa

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import occurrences from './occurrences'
7171
import getMiddle from './getMiddle'
7272
import debounce from './debounce'
7373
import curry from './curry'
74+
import textJustification from './textJustification'
7475
import removeProperty from './removeProperty'
7576

7677
export {
@@ -147,5 +148,6 @@ export {
147148
getMiddle,
148149
debounce,
149150
curry,
151+
textJustification,
150152
removeProperty,
151153
}

src/textJustification.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
export default textJustification
2+
3+
/**
4+
*
5+
* Original Source: https://www.malikbrowne.com/blog/text-justification-coding-question
6+
*
7+
* Description: Given an array of words(input) and a length L, format the text such that each
8+
* line has exactly L characters and is fully (left and right) justified.
9+
*
10+
* @param {Array} input - String array to be justified
11+
* @param {Integer} L - Length of characters per line
12+
* @return {Array} output - The string fully justified
13+
*/
14+
15+
function textJustification(input, L) {
16+
let index = 0
17+
const output = []
18+
19+
while (index < input.length) {
20+
21+
let count = input[index].length
22+
let last = index + 1
23+
24+
while (last < input.length) {
25+
26+
if (input[last].length + count + 1 > L) {
27+
break
28+
}
29+
count += input[last].length + 1
30+
last++
31+
}
32+
33+
let line = ''
34+
const diff = last - index - 1
35+
36+
if (last === input.length || diff === 0) {
37+
38+
for (let i = index; i < last; i++) {
39+
40+
line += `${input[i]} `
41+
}
42+
43+
line = line.substr(0, line.length - 1)
44+
45+
for (let i = line.length; i < L; i++) {
46+
line += ' '
47+
}
48+
49+
} else {
50+
51+
const space = (L - count) / diff
52+
const remainder = (L - count) % diff
53+
54+
for (let i = index; i < last; i++) {
55+
line += input[i]
56+
57+
if (i < last - 1) {
58+
59+
const max = space + ((i - index) < remainder ? 1 : 0)
60+
61+
for (let j = 0; j <= max; j++) {
62+
line += ' '
63+
}
64+
}
65+
}
66+
}
67+
68+
output.push(line)
69+
index = last
70+
}
71+
return output
72+
}
73+
74+
/* eslint complexity: "off", max-depth: "off" */

test/textJustification.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava'
2+
import {textJustification} from '../src'
3+
4+
test('Text justification function', t => {
5+
const object = ['This','is','an','example','of','text','justification.']
6+
const len = 16
7+
const expected = ['This is an','example of text','justification. ']
8+
const actual = textJustification(object, len)
9+
t.deepEqual(actual, expected)
10+
})

0 commit comments

Comments
 (0)