I ran across sim.js, a library for discrete event simulation, and I thought it would be fun to use while learning node.js. It's a single javascript file, with no dependencies on other libraries or the browser.
What's the best practice for including this in my code base? Save the .js to disk, and convert it to a standard node module?
I.e.:
// ---sim.js
var Sim = function () {
this.simTime = 0;
this.entities = [];
}
Sim.Event = function (name) {
this.name = name;
};
// ... functions continue
module.exports = Sim;
// --- main.js
var Sim = require('./sim');
var sim = new Sim();
var trafficLights = [new Sim.Event("North-South Light"),
new Sim.Event("East-West Light")];
Or is there an easier way that would not require modifying/modularizing the original js?
I ran across sim.js, a library for discrete event simulation, and I thought it would be fun to use while learning node.js. It's a single javascript file, with no dependencies on other libraries or the browser.
What's the best practice for including this in my code base? Save the .js to disk, and convert it to a standard node module?
I.e.:
// ---sim.js
var Sim = function () {
this.simTime = 0;
this.entities = [];
}
Sim.Event = function (name) {
this.name = name;
};
// ... functions continue
module.exports = Sim;
// --- main.js
var Sim = require('./sim');
var sim = new Sim();
var trafficLights = [new Sim.Event("North-South Light"),
new Sim.Event("East-West Light")];
Or is there an easier way that would not require modifying/modularizing the original js?
Share asked Jan 16, 2014 at 19:32 Allyl IsocyanateAllyl Isocyanate 13.6k17 gold badges85 silver badges132 bronze badges 2- nope, according to your link: "only Mozilla Firefox with JavaScript version 1.7 supports process-based programming model via the yield keyword" – dandavis Commented Jan 16, 2014 at 19:50
- 1 "The SIM.JS library provides event-based programming model, for the following reasons:...only Mozilla...supports process-based programming". So in my reading, they are not using yield and doing idiomatic javascript. Reviewing the source there is no use of yield I can see simjs./_downloads/sim-0.26-debug.js – Allyl Isocyanate Commented Jan 16, 2014 at 20:10
1 Answer
Reset to default 4So yes, if you just want to make this work, you can save it to disk. It looks like Sim is designed for browser-global approach, which means it doesn't know anything about the CommonJS patterns that a node module must support. So you could add those like your like above module.exports = Sim;
snippet above, and that would basically get you up and running.
If you wanted to get this project to support node/CommonJS you could send them a patch. Many libraries that began life in the browser have had to add support for package managers such as requirejs, monjs, amd, bower, ponent, etc. For example, the moment library supports a lot of different types of uses, and you can see lots of bits of config and glue code necessary to make it work so broadly.
I wouldn't worry too much about "best practices" as this whole space is a freaking mess right now, but I'd point to moment.js as a good example of a project that works well in many different environments and packaging systems.