Last active
August 29, 2015 14:17
-
-
Save DanElliottPalmer/6ed1b0534c8e15f1ea2e to your computer and use it in GitHub Desktop.
ValidatedMap - Example of what I wanted to do following this curiosity: https://gist.github.com/DanElliottPalmer/aa22a3c038d5f2ef94d2
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
class ValidatedMap { | |
clear(){ | |
this._.clear(); | |
} | |
constructor( _validationRules, _defaultValues ){ | |
let validationRules = _validationRules || null; | |
let defaultValues = _defaultValues || []; | |
this._ = new Map(); | |
this.rules = new Map( validationRules ); | |
defaultValues.forEach( value => { | |
this.set( value[0], value[1] ); | |
}); | |
} | |
delete( key ){ | |
return this._.delete( key ); | |
} | |
destroy(){ | |
this._.clear(); | |
this._ = null; | |
this.rules.clear(); | |
this.rules = null; | |
} | |
entries(){ | |
return this._entries(); | |
} | |
forEach( callback, ctx ){ | |
return this._.forEach( callback, ctx ); | |
} | |
get( key ){ | |
return this._.get( key ); | |
} | |
has( key ){ | |
return this._.has( key ); | |
} | |
keys(){ | |
return this._.keys(); | |
} | |
get length(){ | |
return 1; | |
} | |
set( key, value ){ | |
if( this.rules.has( key ) ){ | |
let assert = this.rules.get( key ); | |
if( !assert( value ) ) return false; | |
} | |
this._.set( key, value ); | |
return this; | |
} | |
get size(){ | |
return this._.size; | |
} | |
values(){ | |
return this._.values(); | |
} | |
[ Symbol.iterator ](){ | |
return this._[ Symbol.iterator ](); | |
} | |
} | |
var attrsMap = new ValidatedMap([["lineWidth", function( value ){ | |
return typeof value === "number"; | |
}]]); | |
attrsMap.set("fill", "#f00"); | |
attrsMap.set("lineWidth", 10); | |
attrsMap.set("lineWidth", "abc"); | |
attrsMap.set("lineWidth", 5); | |
for( let value of attrsMap ){ | |
console.log( value ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment