This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const UUIDGenerator = () => | |
| ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => | |
| (c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16) | |
| ); | |
| UUIDGenerator(); // 82f13078-8479-4060-87ea-1e919e71edfe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const detectDeviceType = () => | |
| /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) | |
| ? 'mobile' | |
| : 'desktop'; | |
| detectDeviceType(); // "mobile" or "desktop" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const capitalize = ([first, ...rest], lowerRest = false) => | |
| first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join('')); | |
| capitalize('JavaScript'); // 'JavaScript ' | |
| capitalize('JavaScript', true); // 'Javascript's |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const digitize = n => [...`${n}`].map(i => parseInt(i)); | |
| digitize(691710147); // [6,9,1,7,1,0,1,4,7] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const escapeHTML = str => | |
| str.replace( | |
| /[&<>'"]/g, | |
| tag => | |
| ({ | |
| '&': '&', | |
| '<': '<', | |
| '>': '>', | |
| "'": ''', | |
| '"': '"' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const unescapeHTML = str => | |
| str.replace( | |
| /&|<|>|'|"/g, | |
| tag => | |
| ({ | |
| '&': '&', | |
| '<': '<', | |
| '>': '>', | |
| ''': "'", | |
| '"': '"' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const stripHTMLTags = str => str.replace(/<[^>]*>/g, ''); | |
| stripHTMLTags('<a href="#">Me & you</a>'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const removeNonASCII = str => str.replace(/[^\x20-\x7E]/g, ''); | |
| removeNonASCII('äÄçÇéÉêlorem-ipsumöÖÐþúÚ'); // lorem-ipsum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const nativeType = v => | |
| v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase(); | |
| nativeType(new Set([1, 2, 3])); // set |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const toCurrency = (n, curr, LanguageFormat = undefined) => | |
| Intl.NumberFormat(LanguageFormat, { style: 'currency', currency: curr }).format(n); | |
| toCurrency(123456.789, 'EUR'); // €123,456.79 | currency: Euro | currencyLangFormat: Local | |
| toCurrency(123456.789, 'USD', 'en-us'); // $123,456.79 | currency: US Dollar | currencyLangFormat: English (United States) | |
| toCurrency(322342436423.2435, 'JPY'); // ¥322,342,436,423 | currency: Japanese Yen | currencyLangFormat: Local | |
| toCurrency(322342436423.2435, 'JPY', 'fi'); // 322 342 436 423 ¥ | currency: Japanese Yen | currencyLangFormat: Finnish |