diff --git a/dist/string.js b/dist/string.js index 85de7ae..2287e09 100644 --- a/dist/string.js +++ b/dist/string.js @@ -276,6 +276,17 @@ string.js - Copyright (C) 2012-2014, JP Richardson 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()); }, diff --git a/lib/string.js b/lib/string.js index 985944e..7992afb 100644 --- a/lib/string.js +++ b/lib/string.js @@ -199,6 +199,17 @@ string.js - Copyright (C) 2012-2014, JP Richardson 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()); }, diff --git a/test/string.test.js b/test/string.test.js index 254afb1..d1ac2f1 100644 --- a/test/string.test.js +++ b/test/string.test.js @@ -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());