This commit is contained in:
Randy8080 committed 2022-10-18 11:37:51 +08:00
1 parent 7fc7945881
commit 098238ad13
286 files changed
+8596

No files matched your search

@@ -0,0 +1,34 @@
---
title: How to capitalize a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const capitalize = str => `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
```
### Or
```js {.wrap}
const capitalize = ([first, ...rest]) => `${first.toUpperCase()}${rest.join('')}`;
```
### Example
```js {.wrap}
capitalize('hello world'); // 'Hello world'
```
@@ -0,0 +1,31 @@
---
title: How to check if a path is relative in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isRelative = path => !/^([a-z]+:)?[\\/]/i.test(path);
```
### Example
```js {.wrap}
isRelative('/foo/bar/baz'); // false
isRelative('C:\\foo\\bar\\baz'); // false
isRelative('foo/bar/baz.txt'); // true
isRelative('foo.md'); // true
```
@@ -0,0 +1,31 @@
---
title: How to check if a string consists of a repeated character sequence in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const consistsRepeatedSubstring = str => `${str}${str}`.indexOf(str, 1) !== str.length;
```
### Example
```js {.wrap}
consistsRepeatedSubstring('aa'); // true
consistsRepeatedSubstring('aaa'); // true
consistsRepeatedSubstring('ababab'); // true
consistsRepeatedSubstring('abc'); // false
```
@@ -0,0 +1,29 @@
---
title: How to check if a string is a palindrome in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to check if a string is a palindrome 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isPalindrome = str => str === str.split('').reverse().join('');
```
### Example
```js {.wrap}
isPalindrome('abc'); // false
isPalindrom('abcba'); // true
```
@@ -0,0 +1,31 @@
---
title: How to check if a url is absolute in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to check if a url is absolute 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isAbsoluteUrl = url => /^[a-z][a-z0-9+.-]*:/.test(url);
```
### Example
```js {.wrap}
isAbsoluteUrl('https://1loc.dev'); // true
isAbsoluteUrl('https://1loc.dev/foo/bar'); // true
isAbsoluteUrl('1loc.dev'); // false
isAbsoluteUrl('//1loc.dev'); // false
```
@@ -0,0 +1,30 @@
---
title: How to check if two strings are anagram in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to check if two strings are anagram 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const areAnagram = (str1, str2) => str1.toLowerCase().split('').sort().join('') === str2.toLowerCase().split('').sort().join('');
```
### Example
```js {.wrap}
areAnagram('listen', 'silent'); // true
areAnagram('they see', 'the eyes'); // true
areAnagram('node', 'deno'); // true
```
@@ -0,0 +1,29 @@
---
title: How to convert a letter to associate emoji in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to convert a letter to associate emoji 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const letterToEmoji = c => String.fromCodePoint(c.toLowerCase().charCodeAt() + 127365);
```
### Example
```js {.wrap}
letterToEmoji('a'); // 🇦
letterToEmoji('b'); // 🇧
```
@@ -0,0 +1,31 @@
---
title: How to convert a string to camelcase in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const toCamelCase = str => str.trim().replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');
```
### Example
```js {.wrap}
toCamelCase('background-color'); // backgroundColor
toCamelCase('-webkit-scrollbar-thumb'); // WebkitScrollbarThumb
toCamelCase('_hello_world'); // HelloWorld
toCamelCase('hello_world'); // helloWorld
```
@@ -0,0 +1,30 @@
---
title: How to convert a string to pascalcase in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to convert a string to pascalcase 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const toPascalCase = str => (str.match(/[a-zA-Z0-9]+/g) || []).map(w => `${w.charAt(0).toUpperCase()}${w.slice(1)}`).join('');
```
### Example
```js {.wrap}
toPascalCase('hello world'); // 'HelloWorld'
toPascalCase('hello.world'); // 'HelloWorld'
toPascalCase('foo_bar-baz'); // FooBarBaz
```
@@ -0,0 +1,28 @@
---
title: How to convert a string to url slug in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to convert a string to url slug 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const slugify = string => string.toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]+/g, '');
```
### Example
```js {.wrap}
slugify('Chapter One: Once upon a time...'); // 'chapter-one-once-upon-a-time'
```
@@ -0,0 +1,29 @@
---
title: How to convert a windows file path to unix path in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to convert a windows file path to unix path 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const toUnixPath = path => path.replace(/[\\/]+/g, '/').replace(/^([a-zA-Z]+:|\.\/)/, '');
```
### Example
```js {.wrap}
toUnixPath('./foo/bar/baz'); // foo/bar/baz
toUnixPath('C:\\foo\\bar\\baz'); // /foo/bar/baz
```
@@ -0,0 +1,31 @@
---
title: How to convert camelcase to kebab-case and vice versa in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to convert camelcase to kebab-case and vice versa 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const kebabToCamel = str => str.replace(/-./g, m => m.toUpperCase()[1]);
const camelToKebab = str => str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
```
### Example
```js {.wrap}
kebabToCamel('background-color'); // 'backgroundColor'
camelToKebab('backgroundColor'); // 'background-color'
```
@@ -0,0 +1,28 @@
---
title: How to convert snake_case to camelcase in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to convert snake_case 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const snakeToCamel = str => str.toLowerCase().replace(/(_\w)/g, m => m.toUpperCase().substr(1));
```
### Example
```js {.wrap}
snakeToCamel('HELLO_world'); // 'helloWorld'
```
@@ -0,0 +1,39 @@
---
title: How to convert the name of an excel column to number in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to convert the name of an excel column to number 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const getIndex = col => col.split('').reduce((prev, next) => prev * 26 + parseInt(next, 36) - 9, 0);
```
### Example
```js {.wrap}
getIndex('A'); // 1
getIndex('B'); // 2
getIndex('C'); // 3
getIndex('Z'); // 26
getIndex('AA'); // 27
getIndex('AB'); // 28
getIndex('AC'); // 29
getIndex('AZ'); // 52
getIndex('AAA'); // 703
getIndex('AAB'); // 704
```
@@ -0,0 +1,30 @@
---
title: How to count the number of words in a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to count the number of words 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const countWords = str => str.trim().split(/\s+/).length;
```
### Example
```js {.wrap}
countWords('Hello World'); // 2
countWords('Hello World'); // 2
countWords(' Hello World '); // 2
```
@@ -0,0 +1,46 @@
---
title: How to count the occurrences of a character in a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to count the occurrences of a character 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const countOccurrences = (str, char) => [...str].reduce((a, v) => (v === char ? a + 1 : a), 0);
```
### Or
```js {.wrap}
const countOccurrences = (str, char) => str.split('').reduce((a, v) => (v === char ? a + 1 : a), 0);
```
### Or
```js {.wrap}
const countOccurrences = (str, char) => [...str].filter(item => item === char).length;
```
### Or
```js {.wrap}
const countOccurrences = (str, char) => str.split('').filter(item => item === char).length;
```
### Example
```js {.wrap}
countOccurrences('a.b.c.d.e', '.'); // 4
```
@@ -0,0 +1,34 @@
---
title: How to decapitalize a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const decapitalize = str => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
```
### Or
```js {.wrap}
const decapitalize = ([first, ...rest]) => `${first.toLowerCase()}${rest.join('')}`;
```
### Example
```js {.wrap}
decapitalize('Hello world'); // 'hello world'
```
@@ -0,0 +1,28 @@
---
title: How to escape html special characters in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to escape html special characters 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const escape = str => str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
```
### Or
```js {.wrap}
const escape = str => str.replace(/[&<>"']/g, m => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' })[m]);
```
@@ -0,0 +1,34 @@
---
title: How to generate a hash of a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const hash = str => str.split('').reduce((prev, curr) => Math.imul(31, prev) + curr.charCodeAt(0) | 0, 0);
```
### Or
```js {.wrap}
const hash = str => str.split('').reduce((prev, curr) => (((prev << 5) - prev) + curr.charCodeAt(0)) | 0, 0);
```
### Example
```js {.wrap}
hash('hello'); // 99162322
```
@@ -0,0 +1,35 @@
---
title: How to get the base url without any parameters in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to get the base url without any parameters 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const baseUrl = url => url.indexOf('?') === -1 ? url : url.slice(0, url.indexOf('?'));
```
### Or
```js {.wrap}
// Note that `includes` isn't supported in IE 11
const baseUrl = url => url.includes('?') ? url.slice(0, url.indexOf('?')) : url;
```
### Example
```js {.wrap}
baseUrl('https://domain.com/path/sub/path?foo=bar&hello=world'); // 'https://domain.com/path/sub/path'
```
@@ -0,0 +1,22 @@
---
title: How to get the file extension from a file name in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to get the file extension from a file name 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const ext = fileName => fileName.split('.').pop();
```
@@ -0,0 +1,28 @@
---
title: How to get the file name from a url in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to get the file name from a url 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const fileName = url => url.substring(url.lastIndexOf('/') + 1);
```
### Example
```js {.wrap}
fileName('http://domain.com/path/to/document.pdf'); // 'document.pdf'
```
@@ -0,0 +1,29 @@
---
title: How to get the length of a string in bytes in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to get the length of a string in bytes 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const bytes = str => new Blob([str]).size;
```
### Example
```js {.wrap}
bytes('hello world'); // 11
bytes('🎉'); // 4
```
@@ -0,0 +1,35 @@
---
title: How to get the number of a character in a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to get the number of a character 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const characterCount = (str, char) => str.split(char).length - 1;
```
### Or
```js {.wrap}
const characterCount = (str, char) => str.replace((new RegExp(String.raw`[^${char}]`, 'g')), '').length;
```
### Example
```js {.wrap}
characterCount('192.168.1.1', '.'); // 3
characterCount('star wars', 's'); // 2
```
@@ -0,0 +1,28 @@
---
title: How to make the first character of a string lowercase in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to make the first character of a string lowercase 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const lowercaseFirst = str => `${str.charAt(0).toLowerCase()}${str.slice(1)}`;
```
### Example
```js {.wrap}
lowercaseFirst('Hello World'); // 'hello World'
```
@@ -0,0 +1,29 @@
---
title: How to normalize file path slashes in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to normalize file path slashes 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const normalizePath = path => path.replace(/[\\/]+/g, '/');
```
### Example
```js {.wrap}
normalizePath('\\foo\\bar\\baz\\'); // /foo/bar/baz/
normalizePath('.//foo//bar///////baz/'); // ./foo/bar/baz/
```
@@ -0,0 +1,39 @@
---
title: How to prepend a line number to each line of a text document in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to prepend a line number to each line 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const prependNumbers = str => str.split(/\r?\n/).map((line, i) => `${(i + 1).toString().padStart(2, ' ')} ${line}`).join('\n');
```
### Example
```js {.wrap}
prependNumbers(`one
two
three
four`);
/* Output */
/*
1 one
2 two
3 three
4 four
*/
```
@@ -0,0 +1,41 @@
---
title: How to remove duplicate lines of a text document in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const removeDuplicateLines = str => Array.from(new Set(str.split(/\r?\n/))).join('\n');
```
### Example
```js {.wrap}
removeDuplicateLines(`one
three
two
three
one
four`);
/* Output */
/*
one
three
two
four
*/
```
@@ -0,0 +1,41 @@
---
title: How to remove empty lines of a text document in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const removeEmptyLines = str => str.split(/\r?\n/).filter(line => line.trim() !== '').join('\n');
```
### Example
```js {.wrap}
removeEmptyLines(`red
green
blue
yellow`);
/* Output */
/*
red
green
blue
yellow
*/
```
@@ -0,0 +1,28 @@
---
title: How to remove spaces from a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to remove spaces from 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const removeSpaces = str => str.replace(/\s/g, '');
```
### Example
```js {.wrap}
removeSpaces('hel lo wor ld'); // 'helloworld'
```
+28
View File
@@ -0,0 +1,28 @@
---
title: How to repeat a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to repeat 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const repeat = (str, numberOfTimes) => str.repeat(numberOfTimes);
```
### Or
```js {.wrap}
const repeat = (str, numberOfTimes) => Array(numberOfTimes + 1).join(str);
```
@@ -0,0 +1,25 @@
---
title: How to replace all line breaks with br elements in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to replace all line breaks with br elements 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const nl2br = str => str.replace(new RegExp('\r?\n', 'g'), '<br>');
// In React
str.split('\n').map((item, index) => <React.Fragment key={index}>{item}<br /></React.Fragment>)
```
@@ -0,0 +1,22 @@
---
title: How to replace all tab characters with spaces in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to replace all tab characters with spaces 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const replace = (str, numSpaces = 4) => str.replaceAll('\t', ' '.repeat(numSpaces));
```
@@ -0,0 +1,32 @@
---
title: How to replace multiple spaces with a single space in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to replace multiple spaces with a single space 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
// Replace spaces, tabs and new line characters
const replaceSpaces = str => str.replace(/\s\s+/g, ' ');
// Only replace spaces
const replaceOnlySpaces = str => str.replace(/ +/g, ' ');
```
### Example
```js {.wrap}
replaceSpaces('this\n is \ta \rmessage'); // 'this is a message'
```
@@ -0,0 +1,28 @@
---
title: How to replace the first given number of characters of a string with another character in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to replace the first given number of characters of a string with another 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const mask = (str, num, mask) => `${str}`.slice(num).padStart(`${str}`.length, mask);
```
### Example
```js {.wrap}
mask(1234567890, 3, '*'); // ***4567890
```
@@ -0,0 +1,46 @@
---
title: How to reverse a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to reverse 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const reverse = str => str.split('').reverse().join('');
```
### Or
```js {.wrap}
const reverse = str => [...str].reverse().join('');
```
### Or
```js {.wrap}
const reverse = str => str.split('').reduce((rev, char)=> `${char}${rev}`, '');
```
### Or
```js {.wrap}
const reverse = str => (str === '') ? '' : `${reverse(str.substr(1))}${str.charAt(0)}`;
```
### Example
```js {.wrap}
reverse('hello world'); // 'dlrow olleh'
```
@@ -0,0 +1,37 @@
---
title: How to reverse the order of lines of a text in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to reverse the order of lines of a text 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const reverseLines = str => str.split(/\r?\n/).reverse().join('\n');
```
### Example
```js {.wrap}
reverseLines(`one
two
three`);
/* Output */
/*
three
two
one
*/
```
@@ -0,0 +1,54 @@
---
title: How to sort lines of a text document in the alphabetical order in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to sort lines of a text document in the alphabetical order 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const sortLines = str => str.split(/\r?\n/).sort().join('\n');
// Reverse the order
const reverseSortedLines = str => str.split(/\r?\n/).sort().reverse().join('\n');
```
### Example
```js {.wrap}
sortLines(`Thaddeus Mullen
Kareem Marshall
Ferdinand Valentine
Hasad Lindsay
Mufutau Berg
Knox Tyson
Kasimir Fletcher
Colton Sharp
Adrian Rosales
Theodore Rogers`);
/* Output */
/*
Adrian Rosales
Colton Sharp
Ferdinand Valentine
Hasad Lindsay
Kareem Marshall
Kasimir Fletcher
Knox Tyson
Mufutau Berg
Thaddeus Mullen
Theodore Rogers
*/
```
@@ -0,0 +1,28 @@
---
title: How to sort the characters of a string in the alphabetical order in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to sort the characters of a string in the alphabetical order 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const sort = str => str.split('').sort((a, b) => a.localeCompare(b)).join('');
```
### Example
```js {.wrap}
sort('hello world'); // dehllloorw
```
@@ -0,0 +1,29 @@
---
title: How to strip ansi codes from a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to strip ansi codes from 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const stripAnsiCodes = str => str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
```
### Example
```js {.wrap}
stripAnsiCodes('\u001B[4mcake\u001B[0m'); // 'cake'
stripAnsiCodes('\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m'); // 'foofoo'
```
@@ -0,0 +1,28 @@
---
title: How to swap case of characters in a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to swap case of characters 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const swapCase = str => str.split('').map(c => (c === c.toLowerCase() ? c.toUpperCase() : c.toLowerCase())).join('');
```
### Example
```js {.wrap}
swapCase('Hello World'); // 'hELLO wORLD'
```
@@ -0,0 +1,34 @@
---
title: How to trim slashes at the beginning and the end of a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to trim slashes at the beginning and the end 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const trimSlashes = str => str.replace(/^\/+|\/+$/g, '');
```
### Or
```js {.wrap}
const trimSlashes = str => str.split('/').filter(Boolean).join('/');
```
### Example
```js {.wrap}
trimSlashes('//hello/world///'); // hello/world
```
@@ -0,0 +1,30 @@
---
title: How to trim some character in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const trim = (str, char) => str.split(char).filter(Boolean).join();
```
### Example
```js {.wrap}
trim('/hello world//', '/'); // hello world
trim('"hello world"', '"'); // hello world
trim(' hello world ', ' '); // hello world
```
@@ -0,0 +1,30 @@
---
title: How to trim the file extension from a file name in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to trim the file extension from a file name 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const trimExt = fileName => fileName.indexOf('.') === -1 ? fileName : fileName.split('.').slice(0, -1).join('.');
```
### Example
```js {.wrap}
trimExt('document'); // document
trimExt('document.pdf'); // document
trimExt('document.2020.pdf'); // document.2020
```
@@ -0,0 +1,28 @@
---
title: How to truncate a string at full words in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to truncate a string at full words 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const truncate = (str, max, suffix) => str.length < max ? str : `${str.substr(0, str.substr(0, max - suffix.length).lastIndexOf(' '))}${suffix}`;
```
### Example
```js {.wrap}
truncate('This is a long message', 20, '...'); // 'This is a long...'
```
@@ -0,0 +1,22 @@
---
title: How to unescape html special characters in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
In this Article we will go through how to unescape html special characters 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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const unescape = str => str.replace(/&amp;/g , '&').replace(/&lt;/g , '<').replace(/&gt;/g , '>').replace(/&#0*39;/g , "'").replace(/&quot;/g, '"');
```
@@ -0,0 +1,34 @@
---
title: How to uppercase the first character of each word in a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- String
layout: note
---
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`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const uppercaseWords = str => str.split(' ').map(w => `${w.charAt(0).toUpperCase()}${w.slice(1)}`).join(' ');
```
### Or
```js {.wrap}
const uppercaseWords = str => str.replace(/^(.)|\s+(.)/g, c => c.toUpperCase());
```
### Example
```js {.wrap}
uppercaseWords('hello world'); // 'Hello World'
```