From 10fe3c8b68cf7ea8cc1c107522b0a71e0fab0a96 Mon Sep 17 00:00:00 2001 From: Erik Rogers Date: Thu, 2 Jun 2016 13:35:34 -0400 Subject: [PATCH 1/3] Add insert method --- dist/string.js | 11 +++++++++++ lib/string.js | 11 +++++++++++ test/string.test.js | 8 ++++++++ 3 files changed, 30 insertions(+) diff --git a/dist/string.js b/dist/string.js index 85de7ae..18f4f1d 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.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..2c7825c 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.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()); From d986f4e86b74d7e128bf49f7d5125e7c429fe256 Mon Sep 17 00:00:00 2001 From: Erik Rogers Date: Thu, 2 Jun 2016 13:46:04 -0400 Subject: [PATCH 2/3] Make consistent with slice on string (instead of internal slice method) --- lib/string.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/string.js b/lib/string.js index 2c7825c..7992afb 100644 --- a/lib/string.js +++ b/lib/string.js @@ -206,7 +206,7 @@ string.js - Copyright (C) 2012-2014, JP Richardson ss = ss || '' index = parseInt(index, 10) || 0 - var s = this.s.slice(0, index) + ss + this.slice(index) + var s = this.s.slice(0, index) + ss + this.s.slice(index) return new this.constructor(s) }, From 0aa8073bd1e7d3144164d698eff4382f8a2e92a9 Mon Sep 17 00:00:00 2001 From: Erik Rogers Date: Thu, 2 Jun 2016 13:46:33 -0400 Subject: [PATCH 3/3] Updated dist --- dist/string.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/string.js b/dist/string.js index 18f4f1d..2287e09 100644 --- a/dist/string.js +++ b/dist/string.js @@ -283,7 +283,7 @@ string.js - Copyright (C) 2012-2014, JP Richardson ss = ss || '' index = parseInt(index, 10) || 0 - var s = this.s.slice(0, index) + ss + this.slice(index) + var s = this.s.slice(0, index) + ss + this.s.slice(index) return new this.constructor(s) },