Greasy Fork

vanilla-lib

Vanilla JS library

目前为 2018-06-11 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/scripts/369430/604932/vanilla-lib.js

/* vanilla-lib */
var  parent = elem => ( ! elem ? null : elem.parentNode ),
     isarr  = expr => ( 'object' === typeof expr && Array === expr.constructor ),
     isfn   = expr => ( 'function' === typeof expr ),
     test   = ( expr,func,other ) => ( !! expr ? func(expr) : isfn(other) ? other(expr) : other ),
     log    = console.debug,
     $$     = ( sel,elem ) => Array.slice((elem | document).querySelectorAll(sel)),
     $      = ( sel,elem ) => (elem | document).querySelector(sel);

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, parent(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 );
}