erin-noe-payne / scheming Goto Github PK
View Code? Open in Web Editor NEWJavascript schema generator
Javascript schema generator
Can probably drop the dependency on lodash
Given a schema that defaults a boolean property the instantiated (with non-provided value) appears to be getting set to 'true'. This is using v0.1.3
Code:
var Scheming = require('scheming');
var Bool = Scheming.create("Bool", {
isRoot: {
type: Boolean,
"default": function() {
return false;
}
}
});
var b = new Bool();
console.log("isRoot s/b 'false', but it is: ", b.isRoot);
Result:
isRoot s/b 'false', but it is: true
The UUID's are not exactly correct.
From wikipedia:
http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B (e.g., f47ac10b-58cc-4372-a567-0e02b2c3d479).
However, when running the uuid method several times it will return more letters than 8, 9, A, or B for the "y" value.
The line here:
((if c is "x" then r else (r & 0x7 | 0x8))).toString 16
https://github.com/autoric/scheming/blob/master/src/Scheming.coffee#L20
should have the 0x7 replaced with 0x3
like so
((if c is "x" then r else (r & 0x3 | 0x8))).toString 16
I got that from what looks like very similar code from:
http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
I think this was a type-o from lifting the original?
I would fork and make a pull request but I'm lazy.
I never documented getProperty, getProperties, eachProperty in the readme docs. Do that.
#16
Chrome does not optimize code with try catch statements, as documented here.
I should isolate the try catch statement to minimize impact.
Here's the code that breaks:
var Scheming = require('scheming');
Stuff = Scheming.create("Stuff", {
createdOn: {
type: Date,
"default": function() {
return Date.now();
}
}
});
new Stuff()
Produces this error:
TypeError: Cannot call method 'valueOf' of undefined
at Object.TYPES.Date.equals (/Users/lucas.caballero/tmp/break-scheming/node_modules/scheming/dest/Scheming.js:84:18)
at set (/Users/lucas.caballero/tmp/break-scheming/node_modules/scheming/dest/Scheming.js:569:19)
at Schema.Object.defineProperty.set [as createdOn] (/Users/lucas.caballero/tmp/break-scheming/node_modules/scheming/dest/Scheming.js:718:20)
at /Users/lucas.caballero/tmp/break-scheming/node_modules/scheming/dest/Scheming.js:725:37
at instanceFactory (/Users/lucas.caballero/tmp/break-scheming/node_modules/scheming/dest/Scheming.js:731:7)
at new Schema (/Users/lucas.caballero/tmp/break-scheming/node_modules/scheming/dest/Scheming.js:454:9)
at Object.<anonymous> (/Users/lucas.caballero/tmp/break-scheming/break.js:14:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
I'm guessing it's trying to determine if the value has changed (per 'reactive' programming), but the value is never assigned and so it is 'undefined'. Then calls to .valueOf() method are made during the comparison test which throws this error.
Update the documentation (README) to state that Scheming.TYPES.Mixed '*'
may be used to model an object with arbitrary properties. For example, if you wanted to create a Foo
model that contains two properties:
name | String | The name of this foo instance, which may be null
bar | Object | An initially empty object that will later have additional properties arbitrarily assigned to it
you could model it as:
Foo = Scheming.create 'Foo',
name :
type : String
default : null
bar :
type : "*"
default : {}
IMPORTANT: If you .watch()
an instance of the model above, the watcher will fire if the reference to bar
changes (e.g. if you assigned a different object to the bar
property), however, changes to the properties of bar
itself (e.g if you arbitrarily assigned a new property to bar
), will NOT fire the watch.
Given a model with a default value, that default is a shared reference between all model instances.
Example:
Model = Scheming.create({
foo : {
type : '*',
default : {}
}
});
m1 = new Model();
m1.foo.a = 7;
m2 = new Model();
console.log(m2.foo.a); //7
Current workaround is to set default to a function that returns a default value.
Need to investigate if the shared reference behavior is useful or a bug. If a bug, need to defensively copy default values.
When you nest schemas, the parent sets watch(es) on the children. How are these watches cleaned up or dereferenced when you are done with the instance?
Should overwrite array manipulators and document
Is there a way to get the properties and values of an instance as an object without explicitly calling each property?
I can get them by doing something like this:
newUser = new User({email: '[email protected]'})
userObject = User.getProperties()
params = {}
_.each userObject, (value, key) ->
params[key] = newUser[key]
But I would love it if there was a method on the instance to do that. Does that exist and I just missed it?
Once a property has a value, change management is skipped when assigning undefined
What happens when you watch a type of *
Deprecate _flush
If a watch callback throws an error, other watches stop working. Errors need to be caught and logged without being thrown.
The project has a bower dependency on lodash, but also includes lodash with the output. Using something like bower-main-files means lodash will get included more than once.
This takes the minified output from 18K (without lodash) to 70K. That is a significant amount extra to send to a browser (especially mobile). The build should not include lodash in the output and have a bower dependency as it does now.
write a gulp deploy task or otherwise tie into npm version command so that version strings are kept in sync across npm, bower, and package.json / bower.json
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.