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

Disallow non-string content. #9

Merged
merged 1 commit into from Mar 27, 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
12 changes: 12 additions & 0 deletions src/MagicString/index.js
Expand Up @@ -19,6 +19,10 @@ MagicString.prototype = {
},

append: function ( content ) {
if ( typeof content !== 'string' ) {
throw new TypeError( 'appended content must be a string' );
}

this.str += content;
return this;
},
Expand Down Expand Up @@ -165,6 +169,10 @@ MagicString.prototype = {
},

insert: function ( index, content ) {
if ( typeof content !== 'string' ) {
throw new TypeError( 'inserted content must be a string' );
}

if ( index === 0 ) {
this.prepend( content );
} else if ( index === this.original.length ) {
Expand Down Expand Up @@ -224,6 +232,10 @@ MagicString.prototype = {
},

replace: function ( start, end, content ) {
if ( typeof content !== 'string' ) {
throw new TypeError( 'replacement content must be a string' );
}

var firstChar, lastChar, d;

firstChar = this.locate( start );
Expand Down
24 changes: 24 additions & 0 deletions test/index.js
Expand Up @@ -30,6 +30,14 @@ describe( 'MagicString', function () {
var s = new MagicString( 'abcdefghijkl' );
assert.strictEqual( s.append( 'xyz' ), s );
});

it( 'should throw when given non-string content', function () {
var s = new MagicString( '' );
assert.throws(
function () { s.append( [] ); },
TypeError
);
});
});

describe( 'clone', function () {
Expand Down Expand Up @@ -263,6 +271,14 @@ describe( 'MagicString', function () {
assert.equal( s.insert(1, '1').toString(), 'a1b' );
assert.equal( s.insert(1, '2').toString(), 'a12b' );
});

it( 'should throw when given non-string content', function () {
var s = new MagicString( '' );
assert.throws(
function () { s.insert( 0, [] ); },
TypeError
);
});
});

describe( 'locate', function () {
Expand Down Expand Up @@ -466,6 +482,14 @@ describe( 'MagicString', function () {
var s = new MagicString( 'abcdefghijkl' );
assert.strictEqual( s.replace( 3, 4, 'D' ), s );
});

it( 'should throw when given non-string content', function () {
var s = new MagicString( '' );
assert.throws(
function () { s.replace( 0, 1, [] ); },
TypeError
);
});
});

describe( 'slice', function () {
Expand Down