Skip to content

Instantly share code, notes, and snippets.

View bobdobbalina's full-sized avatar

Mike Grunwald bobdobbalina

View GitHub Profile
@bobdobbalina
bobdobbalina / uuid.js
Created January 20, 2021 19:18
Javascript: Generate a unique universal identifier
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
@bobdobbalina
bobdobbalina / detectDevice.js
Created January 20, 2021 19:19
Javascript: Detect device type
const detectDeviceType = () =>
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
? 'mobile'
: 'desktop';
detectDeviceType(); // "mobile" or "desktop"
@bobdobbalina
bobdobbalina / capitalize.js
Last active January 20, 2021 19:20
Javascript: Capitalize the first letter in a string
const capitalize = ([first, ...rest], lowerRest = false) =>
first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));
capitalize('JavaScript'); // 'JavaScript '
capitalize('JavaScript', true); // 'Javascript's
@bobdobbalina
bobdobbalina / digitize.js
Created January 20, 2021 19:21
Javascript: Convert a number into an array of digits
const digitize = n => [...`${n}`].map(i => parseInt(i));
digitize(691710147); // [6,9,1,7,1,0,1,4,7]
@bobdobbalina
bobdobbalina / escapeHTML.js
Created January 20, 2021 19:23
Javascript: Escape the HTML tags
const escapeHTML = str =>
str.replace(
/[&<>'"]/g,
tag =>
({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
"'": '&#39;',
'"': '&quot;'
@bobdobbalina
bobdobbalina / unescapeHTML.js
Created January 20, 2021 19:24
Javascript: Unescape HTML tags
const unescapeHTML = str =>
str.replace(
/&amp;|&lt;|&gt;|&#39;|&quot;/g,
tag =>
({
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
'&#39;': "'",
'&quot;': '"'
@bobdobbalina
bobdobbalina / stripHTML.js
Created January 20, 2021 19:24
Javascript: Strip HTML tags
const stripHTMLTags = str => str.replace(/<[^>]*>/g, '');
stripHTMLTags('<a href="#">Me & you</a>');
@bobdobbalina
bobdobbalina / nonASCII.js
Created January 20, 2021 19:25
Javascript: Remove non ASCII characters from strings
const removeNonASCII = str => str.replace(/[^\x20-\x7E]/g, '');
removeNonASCII('äÄçÇéÉêlorem-ipsumöÖÐþúÚ'); // lorem-ipsum
@bobdobbalina
bobdobbalina / nativeType.js
Created January 20, 2021 19:26
Javascript: Get the native type of any value
const nativeType = v =>
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
nativeType(new Set([1, 2, 3])); // set
@bobdobbalina
bobdobbalina / formatCurrancy.js
Created January 20, 2021 19:27
Javascript: Format currency
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