From 7f2dc850e0e4ec9d17ca5efa472eb37db0ab035f Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 16 Apr 2012 15:34:30 +0200 Subject: [PATCH] Change how the library is exported This commit makes the library define the |PEG| global variable (for browser export) and possibly assign it into |module.exports| (for Node.js export) later. The |module.exports| assignment is done *outside* the main library |function| statement. The big idea behind this is to make copy & paste inclusion of the library into another code easier -- one just needs to strip the last three lines. --- src/peg.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/peg.js b/src/peg.js index 220834b..98d6a12 100644 --- a/src/peg.js +++ b/src/peg.js @@ -6,7 +6,7 @@ * Copyright (c) 2010-2012 David Majda * Licensend under the MIT license. */ -(function(undefined) { +var PEG = (function(undefined) { var PEG = { /* PEG.js version. */ @@ -41,12 +41,10 @@ PEG.GrammarError.prototype = Error.prototype; // @include "parser.js" // @include "compiler.js" -if (typeof module === "object") { - module.exports = PEG; -} else if (typeof window === "object") { - window.PEG = PEG; -} else { - throw new Error("Can't export PEG library (no \"module\" nor \"window\" object detected)."); -} +return PEG; })(); + +if (typeof module !== "undefined") { + module.exports = PEG; +}