Last active
April 28, 2020 14:49
-
-
Save matthewstokeley/2ae888320a389b65d4cbaaea10b3f665 to your computer and use it in GitHub Desktop.
Key Lookup Strategies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 0.0.2 | |
type Key: Symbol | String | |
type KeyClosure: Function( str: String ): Key | |
// Symbol Strategy | |
export var symbolKeyHandler = function symbolKeyHandler( | |
_cache: Function | |
): KeyClosure { | |
return ( key ) => { | |
return ( typeof key === 'Symbol' ) | |
? _cache ( key ) | |
: _cache ( new Symbol ( key ) ) | |
} | |
} | |
// Resolver Strategy | |
export var keyResolverHandler = function keyResolverHandler( | |
_cache: Function, | |
resolver: Resolver | |
): KeyClosure { | |
return ( str ) => { | |
return _cache ( resolver.call( this, str ) ) | |
} | |
} | |
type Resolver: Function | |
function resolverFn( str: String ) { | |
return md5( str ) | |
} | |
// String Strategy | |
export var stringKeyHandler = function stringKeyHandler( | |
_cache: Function | |
): KeyClosure { | |
const prefix = '' | |
return ( str ) => { | |
return _cache ( prefix + str ) | |
} | |
} | |
// Keystore cache strategy | |
export var keystore = ( function( map ) { | |
let _cache = ( function( ) { | |
return ( map ) => { | |
return function _cache( | |
key: Key | |
): Key { | |
return map.contains( key ) | |
? map.get( key ) | |
: map.set( key ) | |
} | |
} | |
} )( ) | |
return _cache( map ) | |
} ) () | |
const _table = {} | |
// @todo mutation observer | |
window.addEventListener( 'DOMContentLoaded', ( event ) => { | |
interface LookupInterface { | |
setStrategy: Function(), | |
getStrategy: Function() | |
} | |
function keyStore( | |
_keystore?: KeyStore | |
) { | |
const map = new WeakMAp | |
let _cache | |
if ( _keystore ) | |
_cache = _keystore( map ) | |
return { | |
map | |
_keystore, | |
_cache | |
} | |
} | |
} | |
class KeyLookup implements LookupInterface { | |
construct( _keystoreProto ) { | |
for ( var prop of _keystoreProto ) { | |
this.prototype[ prop ] = _keystoreProto | |
} | |
} | |
lookup( key: Key, _table ) { | |
return this.cache ( _table[ key ] ) | |
} | |
} | |
class KeyStrategy { | |
public strategy | |
construct() { | |
} | |
set strategy() { | |
this.strategy = strategy | |
} | |
get strategy() { | |
return this.strategy | |
} | |
} | |
( function() { | |
const ATTR = "attribute" | |
let _el = document.querySelectorAll() | |
let _containsAttr | |
if ( nodeType in _el ) { | |
if ( _el.nodeType === 1 ) { | |
_containsAttr = ( _el.dataset[ ATTR ] ) | |
? true | |
: false | |
} | |
} | |
let _adjacent = [] | |
if ( _containsAttr | |
&& _sibling.children ) { | |
let _sibling = _el.nextSibling() | |
for ( let i = 0; i < _sibling.children.length; i++ ) { | |
_adjacent[ i ] = _sibling.children[ i ] | |
} | |
} | |
let event = new Event( 'nodeListSelected' ) | |
} )() | |
} ) |
an example of an iffe without piping in a global variable to demonstrate knowledge of lexical records (block scopes) in javascript function() {}.bind == () => {}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've separated each of these key lookup strategies into isolated development environments, ala storybook with notebooks