an experiment in implementing microdata via JSON objects
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
README.md
objectid.js
objectid_test.js
package.json
schemathing.js
schemathing_test.js

README.md

schemathing-js

0.0.0f-experiment

Overview

schemathing-js is an experiment in implementing a set of javascript objects that map to/from the microdata format as defined at schema.org.

A example Thing

A +Thing+ object might have the following methods --

factory operations

create()

params: an object (optional) E.g. A mongo object id. { _id: "MONGO_ID_STRING" }

Creates a new thing object. If an _id is with an object it will use that id (e.g. so you could instantiate a copy of a MongoDB object). If not it will create an _id property using a NewObjectId() function if defined.

clone()

params: object_id (optional) E.g. A mongo object id. { _id: "MONGO_ID_STRING" }

Creates a copy of itself as a new thing but with a new object_id.

comparison operations

isSimilar()

params: an object (required)

Checks if an object has same properties (other object could have additional properties)

strictIsSimilar()

params: an object (required)

Both objects have every property in common

equal()

params: an object (required)

Check to see if properties in common are equal (==).

strictEqual()

params: an object (required)

Check to see if common properties are strictly equal (===).

notEqual()

params: an object (required)

Check to see if properties in common are not equal (!=); returns true of false.

notStrictEqual()

params: an object (required)

Check to see if properties in common are strictly not equal (!==); returns true or false.

transformation functions

update()

params: an object (required)

Replace common properties from another object (except _id)

absorb()

params: an object (required)

Update and add additional properties from another object (except _id, merge _isA list)

morph()

params: an object (required)

Overwrite and add additional properties passed into morph (except _id, merge _isA list)

rendering functions

schemething relies on mote-js for implementing handlebars templates and rendering html.

toHTML()

params: options (optional) an array of attribute strings (e.g. ['id="myid"', 'class="myclass"'])

Render the default template with mote into HTML adding necessary attributes to outer div.

toJSON()

params: none

Render as a JSON object.

A theoretical example using the module in NodeJS

var assert = require('assert'),
	schema = require('schemathing');

// Dummy Object id objects and functions
var ObjectIds = { _id: 0 },
	NewObjectId = function () {
		ObjectIds._id += 1;
		return ObjectIds._id;
	},
	LastObjectId = function () {
		return ObjectIds._id;
	};

// Example playing with things
var aThing, aClonedThing,
	aEvent, 
	aThingLikeObject = {
		description: "A plain old JavaScript object.",
		image: "http://example.com/a-plain-js-object.jpg",
		name: 'aThingLikeObject',
		url: "http://example.com/a-plain-js-object.json"
	};

// Creating an empty thing
aThing = scheme.Thing.create({_id:1});
assert.strictEqual(aThing._id, 1, "aThing._id === 1");
// Should return false since aThing has nothing in common with aThingLikeObject
assert.ok(aThing.equal(aThinkLikeObject),"aThing == aThingLikeObject" );
// Merging aThingLikeObject
aThing.strictMerge(aThingLikeObject)
assert.ok(aThing.equal(aThingLikeObject),"aThing == aThingLikeObject");
assert.ok(! aThing.strictEqual(aThingLikeObject),"aThing === aThingLikeObject");
// Cloning, aClonedThing should have _id: 2
aClonedThing = aThing.clone();
assert.strictEqual(aClonedThing._id, 2, "aClonedThing should have an id: 2");
// Should be true because everything in common is equal
assert.ok(aThing.equal(aThingLikeObject),"aClonedThing == aThingLikeObject");
// Should be false because aThingLikeObject._id is missing
assert.ok(aThing.strictEqual(aThingLikeObject),"aClonedThing === aThingLikeObject");
// Should be true because all the fields, except _id are in common
assert.ok(aClonedThing.equal(aThing),"aClonedThing == aThing");
// Should be false since the _id is different
assert.ok(aClonedThing.strictEqual(aThing),"aClonedThing === aThing");

aEvent = schema.Event.create();
aEvent.merge(AThingLikeObject);
aEvent.name = "A new Event.";
aEvent.duration = '2h';
aEvent.startDate = new Date();
aEvent.endDate = new Date();
console.log("aEvent.toJSON(): " + aEvent.toJSON());
console.log("aEvent.toHtml(): " + aEvent.toHtml());

Background notes

Random ideas

  • schemathing.js should work in NodeJS, MongoDB's shell and HTML5 friendly browsers
  • dirty.js would be an interesting environment to play with shemathing for in-memory only applications
  • the property types need to be validated against schema.org's description (e.g. Boolean, Date, Number, Text, URL)

Notes on coding style

  • exported functions begin with a capital letter and are camel case (e.g. Assemble())
  • exported objects begin with a capitil letter and are camel case (e.g. Thing)
  • functions starting with lowercase are intended to be object methods applied by a factory (e.g. var myt = Thing.create(); console.log(myt.toJSON());)