How can I convert a string to a URL-friendly slug in JavaScript with support for non-Latin characters?
Antek N
antek n profile pic

Question 1: How do I check if a string ends with a specific substring in JavaScript? To check if a string ends with a specific substring in JavaScript, you can use theendsWith() method or check the substring manually using string manipulation techniques. Here's how you can accomplish it: 1. Using theendsWith() method: TheendsWith() method is a built-in method in JavaScript strings that returns a boolean value indicating whether the string ends with the specified substring.

1
2
3
4
5
6

const str = "Hello, world!";
const suffix = "world!";

const endsWithSuffix = str.endsWith(suffix);
console.log(endsWithSuffix); // Output: true

2. Manual check: If theendsWith() method is not available or you prefer an alternative approach, you can manually check if a string ends with a specific substring using string manipulation techniques.

1
2
3
4
5
6

const str = "Hello, world!";
const suffix = "world!";

const endsWithSuffix = str.slice(-suffix.length) === suffix;
console.log(endsWithSuffix); // Output: true

In this approach, we use theslice() method to extract the portion of the string that has the same length as the suffix. Then, we compare it with the suffix to check if they are equal. Both approaches will returntrue if the string ends with the specified substring andfalse otherwise. Question 2: What are the differences between JavaScript's Array.reduce() and Array.reduceRight() methods when reducing arrays? BothArray.reduce() andArray.reduceRight() are methods in JavaScript arrays used to reduce an array to a single value. The main difference between the two lies in the direction of iteration: 1.Array.reduce(): Thereduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. The function takes four arguments: the accumulator, the current element, the current index, and the array itself. It performs the reduction by applying the provided function to each element, updating the accumulator along the way.

1
2
3
4
5

const arr = [1, 2, 3, 4, 5];

const sum = arr.reduce((accumulator, current) => accumulator + current, 0);
console.log(sum); // Output: 15

In this example, thereduce() method iterates over the array from left to right, starting with an initial accumulator value of0. The function adds each element to the accumulator, resulting in the sum of all elements. 2.Array.reduceRight(): ThereduceRight() method works similarly toreduce(), but it iterates over the array in reverse order (from right to left). It also takes four arguments: the accumulator, the current element, the current index, and the array itself.

1
2
3
4
5

const arr = [1, 2, 3, 4, 5];

const sum = arr.reduceRight((accumulator, current) => accumulator + current, 0);
console.log(sum); // Output: 15

In this example,reduceRight() starts with the initial accumulator value of0 and adds each element in reverse order, resulting in the same sum asreduce(). The choice betweenreduce() andreduceRight() depends on the specific use case and the desired order of iteration. If the order of the elements matters, choose the appropriate method accordingly. Question 3: How can I convert a string to a URL-friendly slug in JavaScript with support for non-Latin characters? To convert a string to a URL-friendly slug in JavaScript while preserving non-Latin characters, you can use a combination of string manipulation techniques and regular expressions. Here's a step-by -step approach: 1. Remove diacritical marks: To handle non-Latin characters, it's recommended to remove diacritical marks (accents) from the string using Unicode normalization and regular expressions. You can use thenormalize() method with the "NFD" normalization form and a regular expression to remove the diacritical marks.

1
2
3
4

const str = "Café au Lait";
const normalizedStr = str.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
console.log(normalizedStr); // Output: Cafe au Lait

In this example, thenormalize() method converts the string to the "NFD" normalization form, which separates the base character and diacritical marks. Thereplace() method with the regular expression/[\u0300-\u036f]/g removes the diacritical marks from the string. 2. Convert to lowercase: Convert the string to lowercase using thetoLowerCase() method to ensure consistency in the slug.

1
2
3

const lowercaseStr = normalizedStr.toLowerCase();
console.log(lowercaseStr); // Output: cafe au lait

3. Replace non-alphanumeric characters: Replace any non-alphanumeric characters with a hyphen or remove them entirely using regular expressions. You can use thereplace() method with a regular expression to achieve this.

1
2
3

const slug = lowercaseStr.replace(/[^a-z0-9]+/g, "-");
console.log(slug); // Output: cafe-au-lait

In this example, the regular expression/[^a-z0-9]+/g matches any characters that are not lowercase letters or digits and replaces them with a hyphen. The finalslug variable contains the URL-friendly slug representation of the original string. It's important to note that the conversion may not handle all possible language-specific characters, as the handling of non-Latin characters can vary based on the requirements and specific use case.