PHP's wordwrap in JavaScript
How to use
You you can install via yarn add locutus
and
require this function via const wordwrap = require('locutus/php/strings/wordwrap')
.
It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.
Examples
Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.
# | code | expected result |
---|---|---|
1 | wordwrap('Kevin van Zonneveld', 6, '|', true) | 'Kevin|van|Zonnev|eld' |
2 | wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n') | 'The quick brown fox<br />\njumped over the lazy<br />\ndog.' |
3 | wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.') | 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim\nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea\ncommodo consequat.' |
Here’s what our current JavaScript equivalent to PHP's wordwrap looks like.
module.exports = function wordwrap(str, intWidth, strBreak, cut) { |
A community effort
Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows
The McDonald’s Theory.
This means that we assimilate first iterations with imperfections,
hoping for others to take issue with-and improve them.
This unorthodox approach has worked very well to foster fun and fruitful collaboration,
but please be reminded to use our creations at your own risk.
THE SOFTWARE IS PROVIDED "AS IS"
has never been more true than for Locutus.
Now go and: [ View on GitHub | Edit on GitHub | View Raw ]
Star