Skip to content

Instantly share code, notes, and snippets.

View bobdobbalina's full-sized avatar

Mike Grunwald bobdobbalina

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / formToJson.js
Last active January 20, 2021 19:17
Javascript: Convert form data to json
const formToObject = form =>
Array.from(new FormData(form)).reduce(
(acc, [key, value]) => ({
...acc,
[key]: value
}),
{}
);
formToObject(document.querySelector('form');
@bobdobbalina
bobdobbalina / serialize.js
Created January 20, 2021 19:15
Javascript: Serializing form data
const serializeForm = form =>
Array.from(new FormData(form), field => field.map(encodeURIComponent).join('=')).join('&');
serializeForm(document.querySelector('form')
// full-name=Adrian%20Legaspi&email=adrian.luball%40gmail.com&nickname=ImLuyou
@bobdobbalina
bobdobbalina / dynamicCall.js
Last active January 20, 2021 19:13
Javascript: Calling dynamically a function inside an array
let method = (/* True or false */ ? 'start' : 'stop');
array[method]();