-
Notifications
You must be signed in to change notification settings - Fork 512
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space after There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't |
||
} | ||
|
||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if |
||
|
||
}); |
There was a problem hiding this comment.
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'
There was a problem hiding this comment.
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.