Skip to content

Instantly share code, notes, and snippets.

@gitdagray
Last active February 8, 2024 14:15
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save gitdagray/df5a023fd41dd3d3d3b7a07a2779fec0 to your computer and use it in GitHub Desktop.
Save gitdagray/df5a023fd41dd3d3d3b7a07a2779fec0 to your computer and use it in GitHub Desktop.
Javascript Utility Functions
// Youtube tutorial here: https://youtu.be/LDgPTw6tePk
// These functions are designed to be exported, but you could create a class instead. See tutorial video.
// #1 proper case
export const properCase = (string) => {
return `${string[0].toUpperCase()}${string.slice(1).toLowerCase()}`;
};
// #2 console log
export const log = (content) => {
console.log(content);
}
// #3 query selector with optional scope
export const select = (selector, scope) => {
return (scope || document).querySelector(selector);
}
// #4 addEventListener wrapper
export const listen = (target, event, callback, capture = false) => {
target.addEventListener(event, callback, !!capture);
}
// #5 sanitize input / escape characters
export const sanitizeInput = (inputValue) => {
const div = document.createElement('div');
div.textContent = inputValue;
return div.innerHTML;
}
// #6 create an element with an optional CSS class
export const createElement = (tag, className) => {
const el = document.createElement(tag);
if (className) el.classList.add(className);
return el;
}
// #7 delete all contents
export const deleteChildElements = (parentElement) => {
let child = parentElement.lastElementChild;
while (child) {
parentElement.removeChild(child);
child = parentElement.lastElementChild;
}
}
// #8 add class with optional query scope
export const addClass = (selector, className, scope) => {
(scope || document).querySelector(selector).classList.add(className);
}
// #9 check for iOS
export const isIOS = () => {
return (
(/iPad|iPhone|iPod/.test(navigator.platform) ||
(navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1)) && !window.MSStream //MSStream is to avoid IE11
);
}
// #10 get parameters by name from url
export const getParameterValue = (paramName, url) => {
if (!url) url = window.location.href;
const regex = new RegExp(`[?&]${paramName}(=([^&#]*))`);
const results = regex.exec(url);
if (!results) return null;
if (!results[2]) return "";
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
@ahmad-511
Copy link

Nice utility functions

I have 2 variations of your select function but named them $ (uses querySelector) and $$ (uses querySelectorAll)

We can make use of the URL interface in getParametersValue,
check https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams

anyways, I tried to share my gist on your YT video but once again YT keeps deleting it so I gave up after 3rd attempt
I have a plural, resetForm and EventEmitter , see the z_example.js file
https://gist.github.com/ahmad-511/79f62262e96113225ddddd9f7b4c848f

Thanks Dave

@gitdagray
Copy link
Author

gitdagray commented May 27, 2021 via email

@ahmad-511
Copy link

No problems,
Thanks

@gitdagray
Copy link
Author

gitdagray commented May 27, 2021 via email

@ahmad-511
Copy link

I really appreciate it Dave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment