From 8aa84ebf93dfd288112d0c674c47f5c3b4a9b63b Mon Sep 17 00:00:00 2001 From: David Murdoch Date: Tue, 16 Aug 2016 14:52:15 -0400 Subject: [PATCH 1/2] Fix export so it works w/ `module.exports` & `window` Fixes Issue #13. This also fixes a similar issue where this library is used via browserify/webpack in the browser (or other environments where `window` is defined). Previously, `var S2 = require("s1-geometry").S2;`, would result in `window.S2` being defined but `S2` remaining undefined. This changes the behavior to prefer exporting via `module.exports`, if available, and falling back to `window` (or whatever `this` is in the module's imported scope -- which is usually `window`). Additionally, this keeps the library from polluting the global scope unnecessarily. --- src/s2geometry.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/s2geometry.js b/src/s2geometry.js index b13b09d..dee2264 100644 --- a/src/s2geometry.js +++ b/src/s2geometry.js @@ -22,10 +22,14 @@ // - i,j: they always use 30 bits, adjusting as needed. we use 0 to (1< Date: Tue, 16 Aug 2016 14:56:14 -0400 Subject: [PATCH 2/2] Don't overwrite L if it exists in export scope This makes the module.exports fix more inline with previous behavior. --- src/s2geometry.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/s2geometry.js b/src/s2geometry.js index dee2264..9eaf546 100644 --- a/src/s2geometry.js +++ b/src/s2geometry.js @@ -525,11 +525,15 @@ var L = (function () { // export to module.exports or window if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { module.exports.S2 = S2; - module.exports.L = L; + if (!module.exports.L) { + module.exports.L = L; + } } else { global.S2 = S2; - global.L = L; + if (!global.L) { + global.L = L; + } } })(this);