Created
January 9, 2017 12:38
-
-
Save brunocarvalhodearaujo/aa522dd827ac05ba7c554d6202dfc454 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export class Collection { | |
constructor(items = { }) { | |
this.data = { } | |
this.replace(items) | |
} | |
set(key, value) { | |
this.data[ key ] = value | |
} | |
replace(items) { | |
for (let key in items) { | |
this.set(key, items[key]) | |
} | |
} | |
has(key) { | |
return this.data.hasOwnProperty(key) | |
} | |
get(key, or = null) { | |
return this.has(key) ? this.data[ key ] : or | |
} | |
all() { | |
return this.data | |
} | |
keys() { | |
return Object.keys(this.all()) | |
} | |
remove(key) { | |
delete this.data[ key ] | |
} | |
clear() { | |
this.data = { } | |
} | |
count() { | |
return this.keys().length | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment