Skip to content

Commit 518fa10

Browse files
committed
Add the urlify algorithm
1 parent 05c7202 commit 518fa10

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ An algorithm is a finite sequence of well-defined, computer-implementable instru
2929
* [Document distance](src/algorithms/string/document-distance) - measure similarity between two strings;
3030
* [Is unique](src/algorithms/string/unique) - check a string for uniqueness;
3131
* [Check permutation](src/algorithms/string/permutation) - check if a string is a permutation of the other;
32+
* [URLify](src/algorithms/string/urlify) - replace all spaces in a string with '%20';
3233
* **Sort**
3334
* [Insertion sort](src/algorithms/sort/insertion/simple) - sort an array with a simple insertion algorithm;
3435
* [Merge sort](src/algorithms/sort/merge) - sort an array with a merge sort algorithm;
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# URLify
2+
3+
Write a method to replace all spaces in a string with '%20'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { urlify } from "../urlify";
2+
3+
test.each([
4+
["Mr John Smith ", 13, "Mr%20John%20Smith"],
5+
["Hey", 3, "Hey"],
6+
["L o L", 5, "L%20o%20L"]
7+
])(
8+
"if you urlify the %p string of %p len you should get '%p'",
9+
(input, len, value) => {
10+
expect(urlify(input, len)).toBe(value);
11+
},
12+
);
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Replace all spaces in a string with '%20'
3+
* @param {string} input
4+
* @param {number} len
5+
* @returns string
6+
*/
7+
export function urlify(input: string, len: number): string {
8+
return input
9+
.split("")
10+
.map((el) => el === " " ? "%20" : el)
11+
.filter((v, i) => i < len)
12+
.join('');
13+
}

0 commit comments

Comments
 (0)