import domdiff from 'udomdiff' ; /**
 * This "hack" tells the library if the browser is IE11 or old Edge
 */ const IE = document . importNode . length != 1 ; /**
 * Node filter for createWalker
 */ const filter = NodeFilter . SHOW_ELEMENT | NodeFilter . SHOW_COMMENT ; /**
 * Node type of a persistent document fragment
 */ const PERSIST_NODE_TYPE = 123 ; private export const createWalker = IE ? node => document . createTreeWalker ( node , filter , null , false ) : node => document . createTreeWalker ( node , filter ) ; param node returns private function remove ( node ) { const { firstChild } = node ; const parent = firstChild . parentNode ; while ( parent . firstChild != parent . lastChild ) { parent . lastChild . remove ( ) ; } return firstChild ; } param node private export function clearNode ( node ) { if ( ! node . lastChild ) return ; node . textContent = '' ; } param node param operation returns private function diffable ( node , operation ) { if ( node . nodeType === PERSIST_NODE_TYPE ) { if ( 1 / operation < 0 ) { return operation ? remove ( node ) : node . lastChild ; } return operation ? node . valueOf ( ) as Node : node . firstChild ; } return node ; } param frag returns private export function persistent ( frag ) { const children = frag . childNodes ; if ( children . length < 2 ) return children [ 0 ] ; const nodes = Array . from ( children ) ; const nodeType = PERSIST_NODE_TYPE ; function valueOf ( ) { if ( children . length !== nodes . length ) { nodes . forEach ( n => frag . appendChild ( n ) ) ; } return frag ; } return { nodeType , valueOf , firstChild : nodes [ 0 ] } ; } param ref param oldNodes param newNodes returns private export function diff ( ref , oldNodes , newNodes ) { return domdiff ( ref . parentNode , oldNodes , newNodes , diffable , ref ) ; } param svg param markup returns private export function createTemplate ( svg , markup ) { const template = document . createElement ( 'template' ) ; if ( svg ) { template . innerHTML = `<svg>${ markup }</svg>` ; const { content } = template ; const svg = content . firstChild ; content . removeChild ( svg ) ; while ( svg . firstChild ) { content . appendChild ( svg . firstChild ) ; } } else { template . innerHTML = markup ; } return template ; } /**
 * Least Recently Used (LRU) Weak Cache based on the builtin weakmap object
 * Records can be GC'd it will leave the keys.
 * This is fine however as they will be removed when space is required.
 */ export class LRUWeakMap < K extends object , V > extends WeakMap implements WeakMap < K , V > { /**
     * Keys where the first entry is least used.
     */ private list : K [ ] = [ ] ; /**
     * Maximum amount of entries to allow.
     */ readonly max : number ; param size constructor ( size ) { super ( ) ; if ( size <= 0 ) { throw new TypeError ( `LRU Cache max size has to be positive integer larger than 0, got '${ size }'.` ) ; } this . max = size ; } param key returns delete ( key ) { const idx = this . list . indexOf ( key ) ; if ( idx != - 1 ) { this . list . splice ( idx , 1 ) ; } return super . delete ( key ) ; } param key returns get ( key ) { const idx = this . list . lastIndexOf ( key ) ; if ( idx < this . list . length - 1 ) { if ( idx != - 1 ) { this . list . splice ( idx , 1 ) ; } this . list . push ( key ) ; } return super . get ( key ) ; } param key param value returns set ( key , value ) { const idx = this . list . indexOf ( key ) ; if ( idx == - 1 ) { if ( this . max <= this . list . length ) { const [ tail ] = this . list . splice ( 0 , 1 ) ; super . delete ( tail ) ; } } else if ( idx < this . list . length - 1 ) { this . list . splice ( idx , 1 ) ; } this . list . push ( key ) ; return super . set ( key , value ) ; } }