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

Added insert method for inserting substrings at a specific index #190

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions dist/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ string.js - Copyright (C) 2012-2014, JP Richardson <jprichardson@gmail.com>
return new this.constructor(s)
},

insert: function(ss, index) {
if (this.s === null || this.s === undefined)
return new this.constructor('')

ss = ss || ''
index = parseInt(index, 10) || 0

var s = this.s.slice(0, index) + ss + this.s.slice(index)
return new this.constructor(s)
},

isAlpha: function() {
return !/[^a-z\xDF-\xFF]|^$/.test(this.s.toLowerCase());
},
Expand Down
11 changes: 11 additions & 0 deletions lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ string.js - Copyright (C) 2012-2014, JP Richardson <jprichardson@gmail.com>
return new this.constructor(s)
},

insert: function(ss, index) {
if (this.s === null || this.s === undefined)
return new this.constructor('')

ss = ss || ''
index = parseInt(index, 10) || 0

var s = this.s.slice(0, index) + ss + this.s.slice(index)
return new this.constructor(s)
},

isAlpha: function() {
return !/[^a-z\xDF-\xFF]|^$/.test(this.s.toLowerCase());
},
Expand Down
8 changes: 8 additions & 0 deletions test/string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@
})
})

describe('- insert(substring, index)', function() {
it('should insert `substring` at the specified index in the original string', function() {
EQ (S('the string').insert('prefix ').s, 'prefix the string')
EQ (S('we can do this').insert(' too', 14).s, 'we can do this too')
EQ (S('this is cool').insert('un', 8).s, 'this is uncool')
})
})

describe('- isAlpha()', function() {
it("should return true if the string contains only letters", function() {
T (S("afaf").isAlpha());
Expand Down