vanilla-lib

Vanilla JS library

Version vom 12.06.2018. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyforks.org/scripts/369430/604970/vanilla-lib.js

/* 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 );
}
长期地址
遇到问题?请前往 GitHub 提 Issues。