vanilla-lib

Vanilla JS library

目前為 2018-06-15 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/369430/605945/vanilla-lib.js

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

/* vanilla-lib */
// @require      https://greasyfork.org/scripts/369430/code/vanilla-lib.js?version=605814
// @require      https://greasyfork.org/scripts/369430/code/vanilla-lib.js?version=605944
function ImportVanillaLib( scope ) {
	if ( null === scope || 'undefined' === typeof scope ) {
		return  false;
	}

	scope.mapFlat = ( array,func ) => array.map( x => func(x) ).reduce( (a,b) => a.concat(b) );
	scope.parenth = ( elem,nth ) => traverse(elem, scope.ifndef(nth, 1), 0);
	scope.ifndef  = ( expr,value ) => ( scope.ndef(expr) ? value : expr );
	scope.ispojo  = expr => scope.isobj(expr, Object);
	scope.export  = ( source,target,members ) => ( Object.keys(source).filter( key => ! members || members.includes(key) )
	                                                                  .forEach( key => target[ key ] = source[ key ] ) );
	scope.ifnan   = ( expr,value ) => ( isNaN(expr) ? value : expr );
	scope.isobj   = ( expr,type ) => ( 'object' === typeof expr && ( ! type || scope.isfn(expr.constructor)
	                                       && type === ( scope.isfn(type) ? expr.constructor : expr.constructor.name ) ) );
	scope.isarr   = expr => scope.isobj(expr, Array);
	scope.isstr   = expr => ( 'string' === typeof expr );
	scope.isfn    = expr => ( 'function' === typeof expr );
	scope.ndef    = expr => ( 'undefined' === typeof expr );
	scope.test    = ( expr,func,other ) => ( !! expr ? func(expr) : scope.isfn(other) ? other(expr) : other );
	scope.fire    = ( elem,event,args ) => elem.dispatchEvent( scope.isobj(event) ? even
	                                     : new Event( event, scope.ifndef(args, { 'bubbles':true, 'cancelable':true }) ) );
	scope.warn    = console.warn;
	scope.log     = console.debug;
	scope.on      = ( elem,event,func ) => elem.addEventListener(event, func);
	scope.$$      = ( sel,elem ) => Array.slice((elem || document).querySelectorAll(sel));
	scope.$       = ( sel,elem ) => (elem || document).querySelector(sel);
	
	scope.aggRate = ( amount,rate,periods ) => ( ! periods ? amount : scope.aggRate(amount * rate, rate, periods - 1) ),
	scope.toDec   = expr => ( Math.round(parseFloat((expr +'').replace(/\$|,/g, '')) * 100) / 100 );
	
	//Array.prototype.mapFlat = function( func ) { return  mapFlat(this, func); };
	
	
	scope.appendTo = function( element, parent, reference ) {
		if ( !! reference ) {
			parent    = reference.parentNode;
			reference = reference.nextSibling;
		}
	
		if ( !! reference ) {
			return  scope.prependTo(element, parent, reference);
		} else  if ( !! parent ) {
			parent.append(element);
		} else {
			scope.warn('*** appendTo() could not add element: No parent or reference element provided');
		}
	
		return  element;
	};
	
	scope.attr = function( elem, name, value ) {
		if ( scope.isarr(elem) ) {
			return  elem.map( el => scope.attr(el, name, value) );
		}
	
		scope.keysAndValues(name, value, ( n,v ) => ( null === v ? elem.removeAttribute(n) : elem.setAttribute(n, v) ) );
		return  elem;
	};
	
	scope.create = function( html, containerType ) {
		let  container = null,
		     result    = null,
		     attrs, style;
	
		if ( scope.isobj(containerType) ) {
			attrs         = containerType.attrs;
			style         = containerType.style;
			containerType = containerType.container;
		}
	
		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 ( !! attrs ) {
			scope.attr(result, attrs);
		}
		if ( !! style ) {
			scope.css(result, style);
		}
	
		if ( 1 == result.length ) {
			result = result[ 0 ];
		}
		return  result;
	};
	
	scope.css = function( element, key, value ) {
		if ( isarr(element) ) {
			return  element.map( el => css(el, key, value) );
		}
	
		keysAndValues(key, value, ( k,v ) => element.style[ k ] = v );
		return  element;
	};
	
	scope.keysAndValues = function( key, value, action ) {
		// Case 1: key is an object (and there is no value)
		if ( scope.isobj(key) && ! value ) {
			return  Object.keys(key)
			        	.map( k => action(k, key[ k ]) );
		// Case 2: key is an array
		} else if ( scope.isarr(key) ) {
			// Case 1.a: value is an array of the same length
			if ( scope.isarr(value) && key.length === value.length ) {
				return  key.map( ( k,i ) => action(k, value[ i ]) );
			// Case 1.b: value is considered a simple, plain value
			} else {
				return  key.map( k => action(k, value) );
			}
		// Default Case: key and value considered as simple, plain values
		} else {
			return  action(key, value);
		}
	};
	
	scope.prependTo = function( element, parent, reference ) {
		if ( ! reference && !! parent ) {
			reference = parent.childNodes[ 0 ];
		}
	
		if ( !! reference ) {
			reference.parentNode.insertBefore(element, reference);
		} else if ( !! parent ) {
			parent.append(element);
		} else {
			scope.warn('*** prependTo() could not add element: No parent or reference element provided');
		}
	
		return  element;
	};
	
	scope.toDec2 = function( amount ) {
		amount = scope.toDec(amount);
		if ( isNaN(amount) ) {
			return  null;
		}
		let  segs = (amount +'').split('.');
		return  segs[ 0 ] +'.'+ ((segs[ 1 ] || 0) +'0').slice(0, 2);
	};
	
	scope.toMoney = function( amount ) {
		let  dec2 = scope.toDec2(amount);
		return  ( isNaN(dec2) ? null : dec2 < 0 ? '-$ '+ (-dec2) : '$ '+ dec2 );
	};
	
	scope.traverse = function( elem, up, sideways, elementsOnly, lastIfNull ) {
		let  last = elem;
		while ( !! elem && up -- > 0 )  elem = (last = elem, elem.parentNode);
	
		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 );
	};
}