Created
May 12, 2014 23:14
-
-
Save aaronpowell/265830200143026d229d to your computer and use it in GitHub Desktop.
Sweet.js classes combined with type-safe properties via ES6 Proxies
This file contains 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
function Person(name) { | |
this.name = name; | |
return new Proxy(this, { | |
set: function (target, property, val) { | |
var currentType = typeof target[property]; | |
var newType = typeof val; | |
if (property in target && currentType !== newType) { | |
throw new Error('Property ' + property + ' must be a ' + currentType); | |
} | |
target[property] = val; | |
} | |
}); | |
} | |
Person.prototype.say = function say(msg) { | |
console.log(this.name + ' says: ' + msg); | |
}; | |
var bob = new Person('Bob'); | |
bob.say('Macros are sweet!'); | |
bob.name = 42; |
This file contains 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
macro type { | |
rule { $this } => { | |
new Proxy($this, { | |
set: function (target, property, val) { | |
var currentType = typeof target[property]; | |
var newType = typeof val; | |
if (property in target && currentType !== newType) { | |
throw new Error('Property ' + property + ' must be a ' + currentType); | |
} | |
target[property] = val; | |
} | |
}); | |
} | |
} | |
macro class { | |
rule { | |
$className { | |
constructor ($cparams...) { $cbody ... } | |
$($mname $mparams $mbody) ... | |
} | |
} => { | |
function $className ($cparams ...) { | |
$cbody ... | |
return type this | |
} | |
$($className.prototype.$mname | |
= function $mname $mparams $mbody; ) ... | |
} | |
} |
This file contains 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
class Person { | |
constructor(name) { | |
this.name = name; | |
} | |
say(msg) { | |
console.log(this.name + " says: " + msg); | |
} | |
} | |
var bob = new Person("Bob"); | |
bob.say("Macros are sweet!"); | |
bob.name = 42; |
Nice. A non-proxy way could by using typeshave, a lightweight typesafe function wrapper for nodejs/browse PLUG :)
Its 3.9K gzipped, supports jsonschema, and works pretty much everywhere. So:
foo = function(bar){
}
could be rewritten like so:
foo = typesafe({
bar: { type: "object" }
}, function(bar){
}
Or just wrapped later on, including validating deepnested structures, and optional args:
foo = typesafe({
bar: {
type: "object",
required: true,
properties: {
foo: { type: "string", required:true },
items: [{
type: "integer"
}]
}
}
}, foo );
foo({ foo: "helloworld", items:[1,2,3] });
It looks less noisy in coffeescript imho
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by http://www.nczonline.net/blog/2014/04/29/creating-type-safe-properties-with-ecmascript-6-proxies/