Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ export class Bundle {
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
*/
trimStart(charType?: string): this {
const rx = new RegExp(`^${charType || '\\s'}+`)
const rx = new RegExp(`^(?:${charType || '\\s'})+`)
this.intro = this.intro.replace(rx, '')

if (!this.intro) {
Expand Down Expand Up @@ -509,7 +509,7 @@ export class Bundle {
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
*/
trimEnd(charType?: string): this {
const rx = new RegExp(`${charType || '\\s'}+$`)
const rx = new RegExp(`(?:${charType || '\\s'})+$`)

for (let i = this.sources.length - 1; i >= 0; i -= 1) {
const source = this.sources[i]
Expand Down
4 changes: 2 additions & 2 deletions src/MagicString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ export class MagicString {

/** @internal */
trimEndAborted(charType?: string): boolean {
const rx = new RegExp(`${charType || '\\s'}+$`)
const rx = new RegExp(`(?:${charType || '\\s'})+$`)

this.outro = this.outro.replace(rx, '')
if (this.outro.length)
Expand Down Expand Up @@ -1306,7 +1306,7 @@ export class MagicString {

/** @internal */
trimStartAborted(charType?: string): boolean {
const rx = new RegExp(`^${charType || '\\s'}+`)
const rx = new RegExp(`^(?:${charType || '\\s'})+`)

this.intro = this.intro.replace(rx, '')
if (this.intro.length)
Expand Down
11 changes: 11 additions & 0 deletions tests/Bundle/trim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,16 @@ describe('bundle', () => {
assert.equal(b1.toString(), 'a')
assert.equal(b2.toString(), 'a \n')
})

it('should apply the quantifier to the whole charType', () => {
const b = new Bundle()

b.prepend('xy')
b.addSource({ content: new MagicString('a') })
b.addSource({ content: new MagicString(''), separator: 'yx' })

b.trim('x|y')
assert.equal(b.toString(), 'a')
})
})
})
5 changes: 5 additions & 0 deletions tests/MagicString/trim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ describe('magicString', () => {
s.appendRight(4, 'test')
assert.strictEqual(s.trim().toString(), 'test')
})

it('should apply the quantifier to the whole charType', () => {
assert.equal(new MagicString('xyaxy').trimStart('x|y').toString(), 'axy')
assert.equal(new MagicString('axx').trimEnd('x|y').toString(), 'a')
})
})

describe('trimLines', () => {
Expand Down