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 'splice' function to the string object #2642

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
37 changes: 37 additions & 0 deletions Docs/Types/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,42 @@ Strips the String of its *<script>* tags and anything in between them.



String method: splice {#String:splice}
--------------------------------------

Similar to the [array.splice][], inserts a string before the specified position and removes unnecessary elements.

### Syntax:

myString.splice(position, remove[, element1[, ...[, elementN]]][, changes]);

### Arguments:

1. position - (*integer*) The position before which the operation is performed.
Use negative numbers to count from the end: -1 to append, -2 to insert before last character etc.
1. remove - (*integer*) Length of the text to be removed.
1. elements - (*string*, optional) Strings to insert. Or anything which can be converted to a string.
If you don't specify any, splice simply removes characters.
1. changes - (*boolean*, optional) If you wish to know what was removed, set this flag true.
Any boolean value at the end of the args array will be recognized as changes flag.

### Returns:

* (*string*) Modified string.
* (*array*) If the changes flag is set, returns an array of two strings: modified string and removed string.

### Examples:

'Hello, big world!'.splice(6, 4); // returns 'Hello, world!'
'Add here'.splice(3, 0, ' two', ' substrings'); // returns 'Add two substrings here'
'Replace this word'.splice(8, 4, 'that'); // returns 'Replace that word'
'You can count from an end'.splice(-7, 2, 'the'); // returns 'You can count from the end'
'Such a beautiful'.splice(-1, 0, ' array: ', [1, 1, 2, 3, 5]) // returns 'Such a beautiful array: 1,1,2,3,5'
'You can get removed string'.splice(12, 14, 'into ', 'the car', true) // returns ['You can get into the car', 'removed string']
'You can get removed string'.splice(12, 14, 'into ', 'the car', false) // returns 'You can get into the car'



[MDN String]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String
[MDN String:contains]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/contains
[MDN String:indexOf]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf
Expand All @@ -411,3 +447,4 @@ Strips the String of its *<script>* tags and anything in between them.
[String:trim]: #String:trim
[Array:rgbToHex]: /core/Types/Array/#Array:rgbToHex
[String:trim]: #String:trim
[array.splice]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
9 changes: 9 additions & 0 deletions Source/Types/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ String.implement({
if (match.charAt(0) == '\\') return match.slice(1);
return (object[name] != null) ? object[name] : '';
});
},

splice: function() {
arguments[0] += (arguments[0] < 0 ? this.length + 1 : 0);
var return_both = (typeof arguments[arguments.length - 1] === 'boolean' ? arguments[--arguments.length] : false),
t = this.split(""),
change = t.splice.apply(t, arguments).join(""),
out = t.join("");
return (return_both ? [out, change] : out);
}

});
Expand Down
36 changes: 36 additions & 0 deletions Specs/Types/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,40 @@ describe("String Methods", function(){
expect('this {should {break} mo} betta'.substitute({ 'break':'work' })).toEqual('this {should work mo} betta');
});

// String.splice

it('should remove characters', function () {
expect('Hello, big world!'.splice(6, 4)).toEqual('Hello, world!');
expect('Hello, big world!'.splice(6, -4)).toEqual('Hello, big world!');
expect('Hello, big world!'.splice(6, -4)).not.toEqual('Hello, world!');
expect('Hello, big world!'.splice(100, 5)).toEqual('Hello, big world!');
expect('Hello, big world!'.splice(100, 5)).not.toEqual('Hello, world!');
expect('Hello, big world!'.splice(-11, 4)).toEqual('Hello, world!');
expect('Hello, big world!'.splice(5, 100)).toEqual('Hello');
});

it('should add strings and objects', function () {
expect('Add here'.splice(3, 0, ' two', ' substrings')).toEqual('Add two substrings here');
expect('Such a beautiful'.splice(-1, 0, ' array: ', [1, 1, 2, 3, 5])).toEqual('Such a beautiful array: 1,1,2,3,5');
expect('Add here'.splice(-6, 0, ' substring')).toEqual('Add substring here');
expect('Add here'.splice(100, 0, ' a substring')).toEqual('Add here a substring');
expect('added here'.splice(-100, 0, 'Substring is ')).toEqual('Substring is added here');
});

it('should combine both add strings and remove characters next to added', function () {
expect('Replace this word'.splice(8, 4, 'that')).toEqual('Replace that word');
expect('You can count from an end'.splice(-7, 2, 'the', ' very')).toEqual('You can count from the very end');
});

it('should recognize flag', function () {
expect('Hello, big world!'.splice(6, 4, true)).toEqual(['Hello, world!', ' big']);
expect('Add here'.splice(100, 0, ' a substring', true)).toEqual(['Add here a substring', '']);
expect('Replace this word'.splice(8, 4, 'that', true)).toEqual(['Replace that word', 'this']);
expect('Hello, big world!'.splice(6, 4, false)).toEqual('Hello, world!');
expect('Add here'.splice(100, 0, ' a substring', false)).toEqual('Add here a substring');
expect('Replace this word'.splice(8, 4, 'that', false)).toEqual('Replace that word');
expect('You can get removed string'.splice(12, 14, 'into ', 'the car', true)).toEqual(['You can get into the car', 'removed string']);
expect('Such a beautiful'.splice(-1, 0, ' array: ', [1, 1, 2, 3, 5], true)).toEqual(['Such a beautiful array: 1,1,2,3,5', '']);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if remove was < 0?
What if remove and insert was missing?
What if position, remove, and insert were missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if remove was > this.length?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if position was > this.length? Pad with empty string?


});