-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
edb2073
commit c4c556e
Showing
6 changed files
with
223 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import { expect } from '@japa/expect' | ||
import { assert } from '@japa/assert' | ||
import { snapshot } from '@japa/snapshot' | ||
import { fileSystem } from '@japa/file-system' | ||
import { expectTypeOf } from '@japa/expect-type' | ||
import { configure, processCLIArgs, run } from '@japa/runner' | ||
|
||
processCLIArgs(process.argv.splice(2)) | ||
configure({ | ||
files: ['tests/**/*.spec.ts'], | ||
plugins: [expect(), assert(), fileSystem(), expectTypeOf()], | ||
plugins: [expect(), assert(), snapshot(), fileSystem(), expectTypeOf()], | ||
}) | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* @poppinss/string | ||
* | ||
* (c) Poppinss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Wraps a string value to be on multiple lines after | ||
* a certain characters limit has been hit. | ||
*/ | ||
export function wordWrap( | ||
value: string, | ||
options: { | ||
width: number | ||
indent?: string | ||
newLine?: string | ||
escape?: (value: string) => string | ||
} | ||
) { | ||
const width = options.width | ||
const indent = options.indent ?? '' | ||
const newLine = `${options.newLine ?? '\n'}${indent}` | ||
|
||
let regexString = '.{1,' + width + '}' | ||
regexString += '([\\s\u200B]+|$)|[^\\s\u200B]+?([\\s\u200B]+|$)' | ||
|
||
const re = new RegExp(regexString, 'g') | ||
const lines = value.match(re) || [] | ||
const result = lines | ||
.map(function (line) { | ||
if (line.slice(-1) === '\n') { | ||
line = line.slice(0, line.length - 1) | ||
} | ||
return options.escape ? options.escape(line) : line | ||
}) | ||
.join(newLine) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* @poppinss/string | ||
* | ||
* (c) Poppinss | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { test } from '@japa/runner' | ||
import { wordWrap } from '../src/word_wrap.js' | ||
|
||
const sentence = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. | ||
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. | ||
It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.` | ||
|
||
test.group('Word wrap', () => { | ||
test('wrap words after a certain width', ({ assert }) => { | ||
const lines = wordWrap(sentence, { width: 40 }).split(/\n/) | ||
assert.snapshot(lines).matchInline(` | ||
[ | ||
"Lorem Ipsum is simply dummy text of the ", | ||
"printing and typesetting industry. Lorem ", | ||
"Ipsum has been the industry's standard ", | ||
"dummy text ever since the 1500s, when an ", | ||
"unknown printer took a galley of type ", | ||
"and scrambled it to make a type specimen ", | ||
"book.", | ||
"", | ||
"It has survived not only five centuries, ", | ||
"but also the leap into electronic ", | ||
"typesetting, remaining essentially ", | ||
"unchanged.", | ||
"", | ||
"It was popularised in the 1960s with the ", | ||
"release of Letraset sheets containing ", | ||
"Lorem Ipsum passages, and more recently ", | ||
"with desktop publishing software like ", | ||
"Aldus PageMaker including versions of ", | ||
"Lorem Ipsum.", | ||
] | ||
`) | ||
}) | ||
|
||
test('add identation', ({ assert }) => { | ||
const lines = wordWrap(sentence, { width: 40, indent: ' ' }).split(/\n/) | ||
assert.snapshot(lines).matchInline(` | ||
[ | ||
"Lorem Ipsum is simply dummy text of the ", | ||
" printing and typesetting industry. Lorem ", | ||
" Ipsum has been the industry's standard ", | ||
" dummy text ever since the 1500s, when an ", | ||
" unknown printer took a galley of type ", | ||
" and scrambled it to make a type specimen ", | ||
" book.", | ||
"", | ||
" It has survived not only five centuries, ", | ||
" but also the leap into electronic ", | ||
" typesetting, remaining essentially ", | ||
" unchanged.", | ||
"", | ||
" It was popularised in the 1960s with the ", | ||
" release of Letraset sheets containing ", | ||
" Lorem Ipsum passages, and more recently ", | ||
" with desktop publishing software like ", | ||
" Aldus PageMaker including versions of ", | ||
" Lorem Ipsum.", | ||
] | ||
`) | ||
}) | ||
|
||
test('define custom new line', ({ assert }) => { | ||
const lines = wordWrap(sentence, { width: 40, newLine: '<br />' }).split('<br />') | ||
assert.snapshot(lines).matchInline(` | ||
[ | ||
"Lorem Ipsum is simply dummy text of the ", | ||
"printing and typesetting industry. Lorem ", | ||
"Ipsum has been the industry's standard ", | ||
"dummy text ever since the 1500s, when an ", | ||
"unknown printer took a galley of type ", | ||
"and scrambled it to make a type specimen ", | ||
"book. | ||
", | ||
"It has survived not only five centuries, ", | ||
"but also the leap into electronic ", | ||
"typesetting, remaining essentially ", | ||
"unchanged. | ||
", | ||
"It was popularised in the 1960s with the ", | ||
"release of Letraset sheets containing ", | ||
"Lorem Ipsum passages, and more recently ", | ||
"with desktop publishing software like ", | ||
"Aldus PageMaker including versions of ", | ||
"Lorem Ipsum.", | ||
] | ||
`) | ||
}) | ||
}) |