Skip to content

Instantly share code, notes, and snippets.

@koresar
Created July 21, 2021 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koresar/21bd323148b2645d1d91601d8e2a8361 to your computer and use it in GitHub Desktop.
Save koresar/21bd323148b2645d1d91601d8e2a8361 to your computer and use it in GitHub Desktop.
getters-setters test
const test = require("tape");
const stampit = require("../src/stampit");
function hasStringAccessor(name) {
let storedValue;
return stampit({
props: {
get [name]() {
return storedValue;
},
set [name](value) {
if (!["undefined", "string"].includes(typeof value)) return;
storedValue = value;
}
},
init(opts) {
this[name] = opts[name];
}
});
}
test(".create()", (t) => {
const TestAccessors = stampit(
hasStringAccessor("one"),
{ name: "TestAccessors" }
);
const instance1 = TestAccessors({ one: "mango" });
t.equal(instance1.one, "mango");
const instance2 = TestAccessors({ one: "banana" });
t.equal(instance2.one, "banana");
// check instance1 again
t.equal(instance1.one, "mango");
t.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment