Skip to content

Instantly share code, notes, and snippets.

@ShapeGroup
Last active June 22, 2021 08:30
Show Gist options
  • Save ShapeGroup/c17d65799f8530f4adbc18512eeeb5ab to your computer and use it in GitHub Desktop.
Save ShapeGroup/c17d65799f8530f4adbc18512eeeb5ab to your computer and use it in GitHub Desktop.
collection of js code & techniques
,---._
.-- -.' \
| | :
: ; |
: | .--.--.
| : :/ / '
: | : /`./
| ; | : ;_
___ l \ \ `.
/ /\ J : `----. \
/ ../ `..- ,/ /`--' /
\ \ ;'--'. /
\ \ ,' `--'---'
"---....--'
A DEDICATED SPACE TO A SERIES OF JS TECHNIQUES
COLLECTED IN THEIR MOST ELEMENTARY FORM.
//// this is an exemple of autopopulated class structure
//// https://jsfiddle.net/nk2mfs0z/15/
class PATHS
{
constructor()
{
this.gethost();
}
sethost(hostname)
{
if(!hostname) paths.host = "http://localhost/";
else paths.host = hostname;
}
gethost()
{
return paths.host;
}
}
const paths = {}, pathdata = new PATHS();
pathdata.sethost("http://mysite.com/");
alert(paths.host);
let actualhost = pathdata.gethost();
alert(actualhost);
//// this is an exemple of singleton structure
//// https://jsfiddle.net/3rjn950m/8/
const person = {
name,
setName: newName => {
this.name = newName;
},
getName: () =>
{
return this.name;
}
}
person.setName("MARIO ROSSI");
alert(person.getName());
//// exemple of browser check
/// https://jsfiddle.net/2eg2yuzy/
function BrowserCheck()
{
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie|trident)/?\s*(.?\d+(.\d+)*)/i);
if(M && (tem= ua.match(/version/([.\d]+)/i))!= null) {M[2]=tem[1];}
M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
return M;
}
alert(BrowserCheck())
class MYCLASSNAME
{
constructor()
{
this.myfunctionone();
this.myfunctiontwo();
}
myfunctionone()
{
this.mydata = String(window.location);
}
myfunctiontwo()
{
this.mydata = String(window.location);
}
myfree()
{
return mydata = String(window.location);
}
}
const myclassname = new MYCLASSNAME();
var arrays = [ ['apple', ['taco', 'fish', 'apple', 'pizza'], 'banana', 'pear', 'fish', 'pancake', 'taco', 'pizza'], ['banana', 'pizza', 'fish', 'apple'] ];
var result = arrays.shift().filter(function(v) {
return arrays.every(function(a) {
return a.indexOf(v) !== -1;
});
});
//// this is an exemple of cross browser iframe content getter
//// https://stackoverflow.com/questions/7570496/getting-the-document-object-of-an-iframe
function getIframeContent(f) {
if (f.contentWindow) return f.contentWindow; else if (f.window) return f.window;
let c = (f.contentDocument) ? f.contentDocument : (f.document) ? f.document : false;
return (c && doc.defaultView) ? c.defaultView : (c && c.parentWindow) ? c.parentWindow : undefined;
}
//// this is an exemple of recoursive stepper loop
let elements = [...myarray],
step = 0;
( loopbystep = () => {
// make step
initStep( elements[step], step, otherparams,
() => {
//up step
step++;
//start new loop step whit new step
if( step < elements.length ) { loopbystep(elements,step); }
});
})(array,step,otherparams);
// operation for this step
function initStep(elements,step,otherparams,back)
{
//elements[step] do, if all done:
back(); //return to initStep
}
//// this is an exemple of recoursive trimmed string
function trim(value)
{
var temp = value;
var obj = /^(\s*)([\W\w])(\b\s$)/;
if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
var obj = / +/g;
temp = temp.replace(obj, " ");
if (temp == " ") { temp = ""; }
return temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment