Skip to content

Commit

Permalink
Merge pull request #10 from eventualbuddha/ensure-insert-at-start-is-…
Browse files Browse the repository at this point in the history
…consistent

Ensure inserting at position 0 is consistent with non-zero positions.
  • Loading branch information
Rich-Harris committed Apr 3, 2015
2 parents 2271c63 + dcb7945 commit 91d074c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/MagicString/index.js
Expand Up @@ -173,9 +173,7 @@ MagicString.prototype = {
throw new TypeError( 'inserted content must be a string' );
}

if ( index === 0 ) {
this.prepend( content );
} else if ( index === this.original.length ) {
if ( index === this.original.length ) {
this.append( content );
} else {
var mapped = this.locate(index);
Expand Down
10 changes: 8 additions & 2 deletions test/index.js
Expand Up @@ -272,6 +272,12 @@ describe( 'MagicString', function () {
assert.equal( s.insert(1, '2').toString(), 'a12b' );
});

it( 'should insert repeatedly at the beginning correctly', function () {
var s = new MagicString( 'ab' );
assert.equal( s.insert(0, '1').toString(), '1ab' );
assert.equal( s.insert(0, '2').toString(), '12ab' );
});

it( 'should throw when given non-string content', function () {
var s = new MagicString( '' );
assert.throws(
Expand Down Expand Up @@ -423,8 +429,8 @@ describe( 'MagicString', function () {
s.prepend( 'xyz' );
assert.equal( s.toString(), 'xyzabcdefghijkl' );

s.prepend( 'xyz' );
assert.equal( s.toString(), 'xyzxyzabcdefghijkl' );
s.prepend( '123' );
assert.equal( s.toString(), '123xyzabcdefghijkl' );
});

it( 'should return this', function () {
Expand Down

0 comments on commit 91d074c

Please sign in to comment.