1 const intersection = (a, b) => {
2 const bSet = new Set(b);
3 return a?.filter(value => bSet.has(value));
4 }
5
6 intersection([1, 2, 3], [1, 3, 5]) // [1, 3]
1 const isValidEmail = (email) => {
2 const emailRegex = new RegExp('[a-z0-9]+@[a-z0-9]+.[a-z]{2,4}$');
3
4 return emailRegex.test(email.toLowerCase());
5 }
1 // for PWA `standalone` property is `true`
2 const isPWA = window.navigator.standalone;
1 function slugify(value) {
2 return value
3 .toString()
4 .normalize('NFD') // split an accented letter in the base letter and the accent
5 .replace(/[\u0300-\u036f]/g, '') // remove all the accents
6 .toLowerCase()
7 .replace(/[^a-z0-9 -]/g, '') // replace all chars that are not letters, numbers and spaces
8 .trim()
9 .replace(/\s+/g, '-');}
10 slugify('Hello world'); // 'hello-world'
1 const numOfAppearances = (array, value) => {
2 return array.reduce((acc, currVal) => currVal === value ? ++acc : acc, 0);
3 };
4
5 const count = numOfAppearances(['one', 'yes', 3, 'yes', false], 'yes'); // 2
1 const arr = ['', 0, 1, 'true', false, Infinity, undefined, null, NaN, 12n, -0];
2 const truthy = arr.filter(Boolean); // [ 1, 'true', Infinity, 12n ]
1 function capitalise([first, ...rest]) {
2 return first.toUpperCase() + rest.join('');
3 };
4 capitalise('hello'); // 'Hello'
5
1 const arr = [1000, 1, 6, 2, 100, 1001, 201, 20, 305];
2 const sorted = arr.sort((a, b) => a - b);
3 console.log(sorted); // [ 1, 2, 6, 20, 100, 201, 305, 1000, 1001 ]