Created
May 10, 2015 10:13
-
-
Save z-------------/b68679eb2739bfd3e580 to your computer and use it in GitHub Desktop.
Python-like string and array chopping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var chop = function(list, arr) { | |
| var start = arr[0] || 0; | |
| var end = arr[1] || list.length; | |
| if (Array.isArray(list)) { | |
| list = list.splice(start, end - start); | |
| return list; | |
| } else if (typeof list === "string") { | |
| return list.substring(start, end); | |
| } | |
| return null; | |
| }; | |
| /* | |
| chop("spam", [1,]) => "pam" | |
| chop("spam", [1,2]) => "p" | |
| chop("spam", [,2]) => "sp | |
| chop(["zero", "one", "two"], [,2]) => ["zero", "one"] | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment