From 2120de36af601277daa232ec7f72a2c48d491437 Mon Sep 17 00:00:00 2001 From: David Majda Date: Tue, 28 Sep 2010 21:02:11 +0200 Subject: [PATCH] Behave like CommonJS module in CommonJS context --- src/peg.js | 10 +++++++--- src/utils.js | 6 ++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/peg.js b/src/peg.js index 7958148..83ca2cf 100644 --- a/src/peg.js +++ b/src/peg.js @@ -1,4 +1,4 @@ -(function(global) { +(function() { var undefined; @@ -32,6 +32,10 @@ PEG.GrammarError.prototype = Error.prototype; // @include "parser.js" // @include "compiler.js" -global.PEG = PEG; +if (typeof exports !== "undefined") { // Looks like we're in CommonJS environment. + extend(exports, PEG); +} else { // Okay, assume this is a browser. + window.PEG = PEG; +} -})(this); +})(); diff --git a/src/utils.js b/src/utils.js index 7afdf88..14f1018 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,3 +1,9 @@ +function extend(target, source) { + for (var key in source) { + target[key] = source[key]; + } +} + function contains(array, value) { /* * Stupid IE does not have Array.prototype.indexOf, otherwise this function