Last active
March 9, 2017 16:02
-
-
Save ericelliott/2126b2d061691ff2aacc150af4520684 to your computer and use it in GitHub Desktop.
stamp-utils bar usage example
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
import { compose, init } from 'stamp-utils'; | |
// Some more privileged methods, with some private data. | |
const availability = init(function () { | |
let isOpen = false; // private | |
Object.assign(this, { | |
open () { | |
isOpen = true; | |
return this; | |
}, | |
close () { | |
isOpen = false; | |
return this; | |
}, | |
isOpen () { | |
return isOpen; | |
} | |
}); | |
}); | |
// Here's a stamp with public methods, and some state: | |
const membership = compose({ | |
methods: { | |
add (member) { | |
this.members[member.name] = member; | |
return this; | |
}, | |
getMember (name) { | |
return this.members[name]; | |
} | |
}, | |
properties: { | |
members: {} | |
} | |
}); | |
// Let's set some defaults: | |
const defaults = compose({ | |
properties: { | |
name: 'The Saloon', | |
specials: 'Whisky, Gin, Tequila' | |
} | |
}); | |
const overrides = init(function (overrides) { | |
Object.assign(this, overrides); | |
}); | |
// Classical inheritance has nothing on this. No parent/child coupling. | |
// No deep inheritance hierarchies. | |
// Just good, clean code reusability. | |
const bar = compose(availability, membership, defaults, overrides); | |
const myBar = bar({name: 'Moe\'s'}); | |
// Silly, but proves that everything is as it should be. | |
const result = myBar.add({name: 'Homer'}).open().getMember('Homer'); | |
console.log(result); // { name: 'Homer' } | |
console.log(` | |
name: ${ myBar.name } | |
isOpen: ${ myBar.isOpen() } | |
specials: ${ myBar.specials } | |
`); | |
/* | |
name: Moe's | |
isOpen: true | |
specials: Whisky, Gin, Tequila | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment