vanilla-lib

Vanilla JS library

Від 12.06.2018. Дивіться остання версія.

Цей скрипт не слід встановлювати безпосередньо. Це - бібліотека для інших скриптів для включення в мета директиву // @require https://update.greasyfork.org/scripts/369430/604970/vanilla-lib.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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.

(I already have a user style manager, let me install it!)

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