Skip to content

Commit

Permalink
Merge pull request #3 from cto-af/space-bug
Browse files Browse the repository at this point in the history
Fix bug with 'even with trailing spaces' rules
  • Loading branch information
hildjj authored Jun 8, 2023
2 parents 7625978 + 1f31fc7 commit 759b74f
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 33 deletions.
2 changes: 1 addition & 1 deletion docs/assets/search.js

Large diffs are not rendered by default.

46 changes: 25 additions & 21 deletions docs/classes/index.LineWrap.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/classes/spacebreaker.SpaceBreaker.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h4>Hierarchy</h4>
<ul class="tsd-hierarchy">
<li><span class="target">SpaceBreaker</span></li></ul></li></ul></section><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cto-af/linewrap/blob/2cb000a/lib/spacebreaker.js#L136">lib/spacebreaker.js:136</a></li></ul></aside>
<li>Defined in <a href="https://github.com/cto-af/linewrap/blob/7625978/lib/spacebreaker.js#L138">lib/spacebreaker.js:138</a></li></ul></aside>
<section class="tsd-panel-group tsd-index-group">
<section class="tsd-panel tsd-index-panel">
<details class="tsd-index-content tsd-index-accordion" open><summary class="tsd-accordion-summary tsd-index-summary">
Expand Down Expand Up @@ -77,7 +77,7 @@ <h5><code class="tsd-tag ts-flagOptional">Optional</code> <span class="tsd-kind-
<h4 class="tsd-returns-title">Returns <a href="spacebreaker.SpaceBreaker.html" class="tsd-signature-type tsd-kind-class">SpaceBreaker</a></h4><aside class="tsd-sources">
<p>Overrides Rules.constructor</p>
<ul>
<li>Defined in <a href="https://github.com/cto-af/linewrap/blob/2cb000a/lib/spacebreaker.js#L141">lib/spacebreaker.js:141</a></li></ul></aside></li></ul></section></section>
<li>Defined in <a href="https://github.com/cto-af/linewrap/blob/7625978/lib/spacebreaker.js#L143">lib/spacebreaker.js:143</a></li></ul></aside></li></ul></section></section>
<section class="tsd-panel-group tsd-member-group">
<h2>Properties</h2>
<section class="tsd-panel tsd-member tsd-is-private tsd-is-inherited tsd-is-external"><a id="_private" class="tsd-anchor"></a>
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<li><a href="index.html">index</a></li></ul>
<h1>Module index</h1></div><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cto-af/linewrap/blob/2cb000a/lib/index.js#L1">lib/index.js:1</a></li></ul></aside>
<li>Defined in <a href="https://github.com/cto-af/linewrap/blob/7625978/lib/index.js#L1">lib/index.js:1</a></li></ul></aside>
<section class="tsd-panel-group tsd-index-group">
<section class="tsd-panel tsd-index-panel">
<h3 class="tsd-index-heading uppercase">Index</h3>
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/spacebreaker.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<li><a href="spacebreaker.html">spacebreaker</a></li></ul>
<h1>Module spacebreaker</h1></div><aside class="tsd-sources">
<ul>
<li>Defined in <a href="https://github.com/cto-af/linewrap/blob/2cb000a/lib/spacebreaker.js#L1">lib/spacebreaker.js:1</a></li></ul></aside>
<li>Defined in <a href="https://github.com/cto-af/linewrap/blob/7625978/lib/spacebreaker.js#L1">lib/spacebreaker.js:1</a></li></ul></aside>
<section class="tsd-panel-group tsd-index-group">
<section class="tsd-panel tsd-index-panel">
<h3 class="tsd-index-heading uppercase">Index</h3>
Expand Down
13 changes: 11 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export class LineWrap {
this.#opts = {
escape: noEscape,
ellipsis: '\u{2026}',
example7: false,
firstCol: NaN,
hyphen: '-',
indent: '',
Expand All @@ -144,10 +145,14 @@ export class LineWrap {
newline: '\n',
newlineReplacement: ' ',
overflow: LineWrap.OVERFLOW_VISIBLE,
verbose: false,
width: 80,
...opts,
}
this.#rules = new SpaceBreaker()
this.#rules = new SpaceBreaker({
example7: this.#opts.example7,
verbose: this.#opts.verbose,
})
this.#graphemes = new Intl.Segmenter(this.#opts.locale, {
granularity: 'grapheme',
})
Expand Down Expand Up @@ -273,7 +278,7 @@ export class LineWrap {
/**
* Split a string into chunks. Each existing newline in the input creates a
* chunk boundary. Each URL is a chunk. Each remaining linebreak segment
* is a chunk, which has had the escape function performed on it.
* is a chunk
*
* @param {string} text
*/
Expand Down Expand Up @@ -338,6 +343,10 @@ export class LineWrap {
let spLen = 0
for (const chunk of this.#chunks(text)) {
for (const frag of this.#fragments(chunk)) {
if (this.#opts.verbose) {
// eslint-disable-next-line no-console
console.log(frag)
}
const fs = /** @type {string} */ (frag.string)
// All fragments are either shorter than the working width, or we're
// configured to go over.
Expand Down
12 changes: 7 additions & 5 deletions lib/spacebreaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ function LBspacesStart(state) {

if (state.next.cls === SP) {
switch (state.cur.cls) {
case ZW: // See LB8
case OP: // See LB14
case QU: // See LB15
case B2: // See LB17
case CL: // See LB16
case CP: // See LB16
case B2: // See LB17
case OP: // See LB14
case QU: // See LB15
case ZW: // See LB8
return PASS
default:
state.extra.fancy = true
Expand All @@ -127,7 +127,9 @@ function LBspacesStart(state) {
function LBbreakAfterSpace(state) {
// SP ÷
if (state.cur.cls === SP) {
state.setProp('space', true)
// This is space at the end of an "even after spaces" run, so the chunk
// isn't actually empty. Do not set the space property.
// state.setProp('space', true)
return MAY_BREAK
}
return PASS
Expand Down
12 changes: 12 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ describe('line wrapping', () => {
new LineWrap({width: 4, indent: 4})
})
})

it('does verbose logging', () => {
/* eslint-disable no-console */
const old = console.log
const res = []
console.log = (...args) => res.push(args)
const lw = new LineWrap({width: 4, verbose: true})
assert.equal(lw.wrap('abcde'), 'abcde')
assert(res.length > 0)
console.log = old
/* eslint-enable no-console */
})
})
1 change: 1 addition & 0 deletions test/spacebreaker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ describe('special space breaking', () => {
assertBreaks('a ', [[1, false], [2, true]])
assertBreaks('a\u200Bb', [[2, false], [3, false]])
assertBreaks('\u2014 \u2014', [[4, false]])
assertBreaks('utf8 base64" Default:', [[4, false], [5, true], [13, false], [21, false]])
})
})

0 comments on commit 759b74f

Please sign in to comment.