vanilla-lib

Vanilla JS library

Verze ze dne 12. 06. 2018. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/369430/604970/vanilla-lib.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

/* vanilla-lib */
var  mapFlat = ( array,func ) => array.map( x => func(x) ).reduce( (a,b) => a.concat(b) ),
     parenth = ( elem,nth ) => traverse(elem, ifndef(nth, 1), 0), //( ! elem ? null : elem.parentNode ),
     ifndef  = ( expr,value ) => ( ndef(expr) ? value : expr ),
     ifnan   = ( expr,value ) => ( isNaN(expr) ? value : expr ),
     isarr   = expr => ( 'object' === typeof expr && Array === expr.constructor ),
     isfn    = expr => ( 'function' === typeof expr ),
     ndef    = expr => ( 'undefined' === typeof expr ),
     test    = ( expr,func,other ) => ( !! expr ? func(expr) : isfn(other) ? other(expr) : other ),
     log     = console.debug,
     on      = ( elem,event,func ) => elem.addEventListener(event, func),
     $$      = ( sel,elem ) => Array.slice((elem || document).querySelectorAll(sel)),
     $       = ( sel,elem ) => (elem || document).querySelector(sel);

var  aggrate = ( amount,rate,periods ) => ( ! periods ? amount : aggrate(amount * rate, rate, periods - 1) ),
     toDec   = expr => ( Math.round(parseFloat((expr +'').replace(/\$|,/g, '')) * 100) / 100 ),
     toMny   = amount => ( (segs => isNaN(segs[ 0 ]) ? null : '$ '+ segs[ 0 ] +'.'+ ((segs[ 1 ] || 0) +'00').slice(0, 2)) ((toDec(amount) +'').split('.')) );

//Array.prototype.mapFlat = function( func ) { return  mapFlat(this, func); };

function  adopt( elem, sibling, position ) {
	let  wrap = parenth(elem);
	// Look for the reference point (for 'insertBefore' function); null = use 'append' instead
	elem = ( position < 0 ? traverse(elem, 0, position, true) : traverse(elem, 0, (position || 0) + 1, false) );
	if ( ! elem ) {
		wrap.append(sibling);
	} else {
		wrap.insertBefore(sibling, elem);
	}
	// Return the element added
	return  sibling;
}

function  attr( elem, name, value ) {
	if ( isarr(elem) ) {
		return  elem.map( el => attr(el, name, value) );
	}

	if ( null === value ) {
		elem.removeAttribute(name);
	} else {
		elem.setAttribute(name, value);
	}
	return  elem;
}

function  create( html, containerType ) {
	let  container = null,
	     result    = null;
	containerType = containerType || 'div';
	create[ containerType ] = 
	container               = create[ containerType ] || document.createElement(containerType);
	container.innerHTML = html;
	result = Array.slice(container.childNodes)
	         	.map( elem => (elem.remove(), elem) );
	if ( 1 == result.length ) {
		result = result[ 0 ];
	}
	return  result;
}

function  traverse( elem, up, sideways, elementsOnly, lastIfNull ) {
	let  last = elem;
	while ( !! elem && up -- > 0 )  elem = (last = elem, parenth(elem));

	let  prop = ( elementsOnly ? 'Element' : '' ) +'Sibling';
	if ( sideways < 0 ) {
		while ( !! elem && sideways ++ < 0 )  elem = (last = elem, elem[ 'previous'+ prop ]);
	} else if ( sideways > 0 ) {
		while ( !! elem && sideways -- > 0 )  elem = (last = elem, elem[ 'next'+ prop ]);
	}

	return  ( ! lastIfNull ? elem : elem || last );
}