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

Commit

Permalink
feat: text justification (#198)
Browse files Browse the repository at this point in the history
* max-depth and complexity errors

* feat(textJustification): Added text justification function

Closes #195
  • Loading branch information
Jonathan-Montanez authored and Kent C. Dodds committed Oct 15, 2018
1 parent 0f94be7 commit d39b0aa
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import occurrences from './occurrences'
import getMiddle from './getMiddle'
import debounce from './debounce'
import curry from './curry'
import textJustification from './textJustification'
import removeProperty from './removeProperty'

export {
Expand Down Expand Up @@ -147,5 +148,6 @@ export {
getMiddle,
debounce,
curry,
textJustification,
removeProperty,
}
74 changes: 74 additions & 0 deletions src/textJustification.js
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" */
10 changes: 10 additions & 0 deletions test/textJustification.test.js
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)
})

0 comments on commit d39b0aa

Please sign in to comment.