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 1 commit
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
29 changes: 29 additions & 0 deletions Docs/Types/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,34 @@ 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, insert);

### Arguments:

1. position - (*integer*) The position before which the insertion is made.
1. remove - (*integer*) Length of the text to be removed.
1. insert - (*string*) String to insert.

### Returns:

* (*string*) Modified string.

### Examples:

'Hello, big world!'.splice(6, 4, ''); // returns 'Hello, world!'
'Add here'.splice(3, 0, ' substring'); // returns 'Add substring here'
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't seem intuitive. One could argue this would have been the result: 'Add substring'

Copy link
Member

Choose a reason for hiding this comment

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

I know it's suppose to mimic Array.splice ... just not exactly very readable.

'Replace this word'.splice(8, 4, 'that'); // returns 'Replace that word'
'You can count from an end'.splice(-6, 2, 'the'); // returns 'You can count from the end'



[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 +439,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
4 changes: 4 additions & 0 deletions Source/Types/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ String.implement({
if (match.charAt(0) == '\\') return match.slice(1);
return (object[name] != null) ? object[name] : '';
});
},

splice: function(pos, rem, ins) {
return (this.slice(0,pos) + ins + this.slice(pos + Math.abs(rem)));
Copy link
Member

Choose a reason for hiding this comment

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

Missing space after 0, pos

Copy link
Member

Choose a reason for hiding this comment

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

If it should work the same as Array#splice, you should be able to insert all rest arguments in between.

Copy link

Choose a reason for hiding this comment

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

Shouldn't ins be optional

}

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

// String.splice

it('should add substrings and remove characters next to added', function () {
expect('Hello, big world!'.splice(6, 4, '')).toEqual('Hello, world!');
expect('Add here'.splice(3, 0, ' substring')).toEqual('Add substring here');
expect('Replace this word'.splice(8, 4, 'that')).toEqual('Replace that word');
expect('You can count from an end'.splice(-6, 2, 'the')).toEqual('You can count from the end');
});
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?


});