PHP's wordwrap in TypeScript

✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { wordwrap } from 'locutus/php/strings/wordwrap'.

Or with CommonJS: const { wordwrap } = require('locutus/php/strings/wordwrap')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1wordwrap('Kevin van Zonneveld', 6, '|', true)'Kevin|van|Zonnev|eld'
2wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n')'The quick brown fox<br />\njumped over the lazy<br />\ndog.'
3wordwrap('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 TypeScript equivalent to PHP's wordwrap looks like.

export function wordwrap(...rawArgs: [str: string, intWidth?: number, strBreak?: string, cut?: boolean]): string {
// discuss at: https://locutus.io/php/wordwrap/
// parity verified: PHP 8.3
// original by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
// improved by: Nick Callen
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: Sakimori
// revised by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
// bugfixed by: Michael Grier
// bugfixed by: Feras ALHAEK
// improved by: Rafał Kukawski (https://kukawski.net)
// example 1: wordwrap('Kevin van Zonneveld', 6, '|', true)
// returns 1: 'Kevin|van|Zonnev|eld'
// example 2: wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n')
// returns 2: 'The quick brown fox<br />\njumped over the lazy<br />\ndog.'
// example 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.')
// returns 3: '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.'

let [str, intWidth, strBreak, cut] = rawArgs

intWidth = rawArgs.length >= 2 ? +(intWidth ?? 0) : 75
strBreak = rawArgs.length >= 3 ? '' + (strBreak ?? '') : '\n'
cut = rawArgs.length >= 4 ? !!cut : false

let i: number
let j: number
let line: string

str += ''

if (intWidth < 1) {
return str
}

const reLineBreaks = /\r\n|\n|\r/
const reBeginningUntilFirstWhitespace = /^\S*/
const reLastCharsWithOptionalTrailingWhitespace = /\S*(\s)?$/

const lines = str.split(reLineBreaks)
const l = lines.length
let match: RegExpMatchArray | null

// for each line of text
for (i = 0; i < l; lines[i++] += line) {
line = lines[i] ?? ''
lines[i] = ''

while (line.length > intWidth) {
// get slice of length one char above limit
const slice = line.slice(0, intWidth + 1)

// remove leading whitespace from rest of line to parse
let ltrim = 0
// remove trailing whitespace from new line content
let rtrim = 0

match = slice.match(reLastCharsWithOptionalTrailingWhitespace)

if (!match) {
break
}

// if the slice ends with whitespace
if (match[1]) {
// then perfect moment to cut the line
j = intWidth
ltrim = 1
} else {
// otherwise cut at previous whitespace
j = slice.length - match[0].length

if (j) {
rtrim = 1
}

// but if there is no previous whitespace
// and cut is forced
// cut just at the defined limit
if (!j && cut && intWidth) {
j = intWidth
}

// if cut wasn't forced
// cut at next possible whitespace after the limit
if (!j) {
const charsUntilNextWhitespace = (line.slice(intWidth).match(reBeginningUntilFirstWhitespace) || [''])[0]

j = slice.length + charsUntilNextWhitespace.length
}
}

lines[i] += line.slice(0, j - rtrim)
line = line.slice(j + ltrim)
lines[i] += line.length ? strBreak : ''
}
}

return lines.join('\n')
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


« More PHP strings functions


Star