Sep 9, 2021 JavaScript
How to trim some character in JavaScript

In this Article we will go through how to trim some character only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const trim = (str, char) => str.split(char).filter(Boolean).join();

Sep 9, 2021 JavaScript
How to uppercase the first character of each word in a string in JavaScript

In this Article we will go through how to uppercase the first character of each word in a string only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const uppercaseWords = str => str.split(' ').map(w => `${w.charAt(0).toUpperCase()}${w.slice(1)}`).join(' ');

Sep 9, 2021 JavaScript
How to capitalize a string in JavaScript

In this Article we will go through how to capitalize a string only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;

Sep 9, 2021 JavaScript
How to check if a path is relative in JavaScript

In this Article we will go through how to check if a path is relative only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const isRelative = path => !/^([a-z]+:)?[\\/]/i.test(path);

Sep 9, 2021 JavaScript
How to check if a string consists of a repeated character sequence in JavaScript

In this Article we will go through how to check if a string consists of a repeated character sequence only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const consistsRepeatedSubstring = str => `${str}${str}`.indexOf(str, 1) !== str.length;

Sep 9, 2021 JavaScript
How to convert a string to camelcase in JavaScript

In this Article we will go through how to convert a string to camelcase only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const toCamelCase = str => str.trim().replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');

Sep 9, 2021 JavaScript
How to decapitalize a string in JavaScript

In this Article we will go through how to decapitalize a string only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const decapitalize = str => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;

Sep 9, 2021 JavaScript
How to generate a hash of a string in JavaScript

In this Article we will go through how to generate a hash of a string only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const hash = str => str.split('').reduce((prev, curr) => Math.imul(31, prev) + curr.charCodeAt(0) | 0, 0);

Sep 9, 2021 JavaScript
How to remove duplicate lines of a text document in JavaScript

In this Article we will go through how to remove duplicate lines of a text document only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const removeDuplicateLines = str => Array.from(new Set(str.split(/\r?\n/))).join('\n');

Sep 9, 2021 JavaScript
How to remove empty lines of a text document in JavaScript

In this Article we will go through how to remove empty lines of a text document only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const removeEmptyLines = str => str.split(/\r?\n/).filter(line => line.trim() !== '').join('\n');