Skip to content

Commit

Permalink
Export zxcvbn function conditional on the environment
Browse files Browse the repository at this point in the history
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
nmalkin committed Jul 31, 2012
1 parent a12f1fd commit 6421ac5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
10 changes: 8 additions & 2 deletions init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
72 changes: 36 additions & 36 deletions zxcvbn.js

Large diffs are not rendered by default.

0 comments on commit 6421ac5

Please sign in to comment.