Skip to content

Instantly share code, notes, and snippets.

@jcmoore
Last active May 25, 2017 06:03
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 jcmoore/601e42bb6bc397cbe979b2e3fab7154f to your computer and use it in GitHub Desktop.
Save jcmoore/601e42bb6bc397cbe979b2e3fab7154f to your computer and use it in GitHub Desktop.
symbolic.js syntax style-guide
// symbolic.js syntax style-guide
(function () {
function _getPropDefs (value) {
return Object.getOwnPropertyNames(value).concat(
Object.getOwnPropertySymbols(value)
).reduce(function (target, field) {
target[field] = Object.getOwnPropertyDescriptor(value, field);
return target;
}, {});
}
function symbolic (err) {
var id = Symbol(err.message);
err.toString = function () {
return id;
};
return err || id;
}
var ns = {};
var SomeKind = {
a: symbolic(Error()),
b: symbolic(Error()),
c: symbolic(Error()),
};
SomeKind[SomeKind.a] = function (value) { return 0|((0|value) === value); };
SomeKind[SomeKind.b] = function (value) { return 0|(typeof value === "function"); };
SomeKind[SomeKind.c] = function (value) { return 0|(typeof value === "number"); };
ns.SomeKind = {};
ns.SomeKind._ = function () {
this[SomeKind.a] = 2;
};
ns.SomeKind._.prototype = {
[SomeKind.b]: function () {
return this[SomeKind.a] * this[SomeKind.a];
},
};
ns.SomeKind.__ = _getPropDefs(ns.SomeKind._.prototype);
ns.SomeKind.$ = {
ok: function (value) {
return value instanceof ns.SomeKind._;
},
get: function () {
return new ns.SomeKind._();
},
mixin: function () {
ns.SomeKind._.apply(this, arguments);
Object.defineProperties(this, ns.SomeKind.__);
return this;
},
};
var OtherKind = {
x: symbolic(Error()),
y: symbolic(Error()),
z: symbolic(Error()),
};
OtherKind[OtherKind.x] = function (value) { return 0|(typeof value === "number"); };
OtherKind[OtherKind.y] = function (value) { return 0|(typeof value === "number"); };
OtherKind[OtherKind.z] = function (value) { return 0|(typeof value === "function"); };
ns.OtherKind = {};
ns.OtherKind._ = function () {
this[OtherKind.x] = 3;
};
ns.OtherKind._.prototype = {
get [SomeKind.a] () { return this[OtherKind.x]; },
set [SomeKind.a] (value) { this[OtherKind.x] = value; },
get [SomeKind.c] () { return this[OtherKind.y]; },
set [SomeKind.c] (value) { this[OtherKind.y] = value; },
[OtherKind.z]: function () {
return this[OtherKind.x] * this[OtherKind.x] * this[OtherKind.x];
},
};
ns.OtherKind.__ = _getPropDefs(ns.OtherKind._.prototype);
ns.OtherKind.$ = {
ok: function (value) {
return value instanceof ns.OtherKind._;
},
get: function (arg, param) {
return new ns.OtherKind._(arg, param);
},
mixin: function () {
ns.OtherKind._.apply(this, arguments);
Object.defineProperties(this, ns.OtherKind.__);
return this;
},
};
return {
SomeKind: Object.assign(function () {
return ns.SomeKind.$;
}, SomeKind),
OtherKind: Object.assign(function () {
return ns.OtherKind.$;
}, OtherKind),
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment