Skip to content

Instantly share code, notes, and snippets.

@brunocarvalhodearaujo
Created January 9, 2017 12:38
Show Gist options
  • Save brunocarvalhodearaujo/aa522dd827ac05ba7c554d6202dfc454 to your computer and use it in GitHub Desktop.
Save brunocarvalhodearaujo/aa522dd827ac05ba7c554d6202dfc454 to your computer and use it in GitHub Desktop.
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