Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure inserting at position 0 is consistent with non-zero positions. #10

Merged
merged 1 commit into from Apr 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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