Last active
May 22, 2022 02:51
-
-
Save Crystal-RainSlide/6e4bfa89a97d9b8f5f3d53884cbd2f2c to your computer and use it in GitHub Desktop.
String.prototype: slice() , substring() and substr() testcase
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
const str = "part1part2part3"; | |
const argList = [ | |
[], [0], [5], [-5], | |
[0, 0], [5, 5], [-5, -5], | |
[0, 5], [5, 7], [0, -5], [5, -5], | |
[5, 0], [7, 5], [-5, 0], [-5, 5], | |
["Boom"] | |
]; | |
const results = argList.map( | |
args => ({ | |
args, | |
slice : str.slice( ...args ), | |
substring : str.substring( ...args ), | |
substr : str.substr( ...args ) | |
}) | |
); | |
console.log(results); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
slice()
substring()
substr()
"part1part2part3"
"part1part2part3"
"part1part2part3"
0
"part1part2part3"
"part1part2part3"
"part1part2part3"
5
"part2part3"
"part2part3"
"part2part3"
-5
"part3"
"part1part2part3"
"part3"
substring()
different0, 0
""
""
""
5, 5
""
""
"part2"
substr()
different-5, -5
""
""
""
0, 5
"part1"
"part1"
"part1"
5, 7
"pa"
"pa"
"part2pa"
substr()
different0, -5
"part1part2"
""
""
slice()
different5, -5
"part2"
"part1"
""
5, 0
""
"part1"
""
substring()
different7, 5
""
"pa"
"rt2pa"
-5, 0
""
""
""
-5, 5
""
"part1"
"part3"
"Boom"
"part1part2part3"
"part1part2part3"
"part1part2part3"