#JavaScript (ES6), 147 bytes
JavaScript (ES6), 147 bytes
###Commented
Commented
#JavaScript (ES6), 147 bytes
###Commented
JavaScript (ES6), 147 bytes
Commented
#JavaScript (ES6), 148147 bytes
w=>F=(s,p=S=' ')=>(g=([c,...b],r=''o='',x=c=='h=c=='-')=>c?r[wo[w-1]?c==S&&r+`c==S&&o+`
`+F(b):r[w+~x]o[w+~h]?r+c+`o+c+`
`+F(b):c==Sc>S?g(b,r+ph?o:o+c)||g:g(b,r+p+co+p):g||g(b,x?r:r+co+p+c):ro)(s)||F(s,p+S)
###Commented
w => // w = requested width
F = ( // F is a recursive function taking:
s, // s = either the input string (first iteration) or an
// array of remaining characters (next iterations)
p = // p = current space padding
S = ' ' // S = space character
) => ( //
g = ( // g is a recursive function taking:
[c, // c = next character
...b], // b[] = array of remaining characters
o = '', // o = output for the current line
h = c == '-' // h = flag set if c is a hyphen
) => //
c ? // if c is defined:
o[w - 1] ? // if the line is full:
c == S && // fail if c is not a space
o + `\n` + F(b) // otherwise, append o + a linefeed and process the
// next line
: // else:
o[w + ~h] ? // if this is the last character and c is a hyphen:
o + c + `\n` + F(b) // append o + c + a linefeed and process the next
// line
: // else, we process the next character:
c > S ? // if c is not a space:
g(b, h ? o : o + c) // append c if it's not a hyphen
: // else:
g(b, o + p) || // append either the current space padding
g(b, o + p + c) // or the current padding and one extra space
: // else:
o // success: return o
)(s) // initial call to g() with s
|| F(s, p + S) // in case of failure, try again with a larger padding
#JavaScript (ES6), 148 bytes
w=>F=(s,p=S=' ')=>(g=([c,...b],r='',x=c=='-')=>c?r[w-1]?c==S&&r+`
`+F(b):r[w+~x]?r+c+`
`+F(b):c==S?g(b,r+p)||g(b,r+p+c):g(b,x?r:r+c):r)(s)||F(s,p+S)
#JavaScript (ES6), 147 bytes
w=>F=(s,p=S=' ')=>(g=([c,...b],o='',h=c=='-')=>c?o[w-1]?c==S&&o+`
`+F(b):o[w+~h]?o+c+`
`+F(b):c>S?g(b,h?o:o+c):g(b,o+p)||g(b,o+p+c):o)(s)||F(s,p+S)
###Commented
w => // w = requested width
F = ( // F is a recursive function taking:
s, // s = either the input string (first iteration) or an
// array of remaining characters (next iterations)
p = // p = current space padding
S = ' ' // S = space character
) => ( //
g = ( // g is a recursive function taking:
[c, // c = next character
...b], // b[] = array of remaining characters
o = '', // o = output for the current line
h = c == '-' // h = flag set if c is a hyphen
) => //
c ? // if c is defined:
o[w - 1] ? // if the line is full:
c == S && // fail if c is not a space
o + `\n` + F(b) // otherwise, append o + a linefeed and process the
// next line
: // else:
o[w + ~h] ? // if this is the last character and c is a hyphen:
o + c + `\n` + F(b) // append o + c + a linefeed and process the next
// line
: // else, we process the next character:
c > S ? // if c is not a space:
g(b, h ? o : o + c) // append c if it's not a hyphen
: // else:
g(b, o + p) || // append either the current space padding
g(b, o + p + c) // or the current padding and one extra space
: // else:
o // success: return o
)(s) // initial call to g() with s
|| F(s, p + S) // in case of failure, try again with a larger padding
#JavaScript (ES6), 148 bytes
Takes input as (width)(text).
w=>F=(s,p=S=' ')=>(g=([c,...b],r='',x=c=='-')=>c?r[w-1]?c==S&&r+`
`+F(b):r[w+~x]?r+c+`
`+F(b):c==S?g(b,r+p)||g(b,r+p+c):g(b,x?r:r+c):r)(s)||F(s,p+S)