vanilla-lib

Vanilla JS library

Stan na 12-06-2018. Zobacz najnowsza wersja.

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/369430/604970/vanilla-lib.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

/* 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 );
}