Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Export zxcvbn function conditional on the environment

Rather than directly attaching the zxcvbn function to the `window` object,
check first if the window exists (i.e., we are in a browser environment).

If not, then we may be in a CommonJS environment, and we can export the
function by attaching it to the `exports` object.
  • Loading branch information...
commit 6421ac52e28a0008824e71a272d2747d2cb99ae3 1 parent a12f1fd
@nmalkin nmalkin authored
Showing with 44 additions and 38 deletions.
  1. +8 −2 init.coffee
  2. +36 −36 zxcvbn.js
View
10 init.coffee
@@ -42,7 +42,7 @@ KEYPAD_STARTING_POSITIONS = (k for k,v of keypad).length
time = -> (new Date()).getTime()
# now that frequency lists are loaded, replace zxcvbn stub function.
-window.zxcvbn = (password, user_inputs) ->
+zxcvbn = (password, user_inputs) ->
start = time()
if user_inputs?
for i in [0...user_inputs.length]
@@ -54,4 +54,10 @@ window.zxcvbn = (password, user_inputs) ->
result.calc_time = time() - start
result
-window.zxcvbn_load_hook?() # run load hook from user, if defined
+# make zxcvbn function globally available
+# via window or exports object, depending on the environment
+if window?
+ window.zxcvbn = zxcvbn
+ window.zxcvbn_load_hook?() # run load hook from user, if defined
+else if exports?
+ exports.zxcvbn = zxcvbn
View
72 zxcvbn.js
36 additions, 36 deletions not shown
Please sign in to comment.
Something went wrong with that request. Please try again.