Skip to content

Instantly share code, notes, and snippets.

@knalli
Created January 19, 2013 18:10
Show Gist options
  • Save knalli/4574041 to your computer and use it in GitHub Desktop.
Save knalli/4574041 to your computer and use it in GitHub Desktop.
NodeUnit equalArrayitems
equalArrayItems = (actual, expected) ->
test.ok actual instanceof Array
test.ok expected instanceof Array
test.equals actual.length, expected.length
# Loop 1: All values of "expected" MUST be in "actual".
for item in expected
test.ok actual.indexOf(item) > -1
# Loop 2: All values of "actual" MUST be in "expected".
for item in actual
test.ok expected.indexOf(item) > -1
equalArrayItems array1, array2
equalArrayItems = (actual, expected) ->
return false unless actual instanceof Array
return false unless expected instanceof Array
return false if actual.length isnt expected.length
# Loop 1: All values of "expected" MUST be in "actual".
return false if (item for item in expected when actual.indexOf(item) > -1).length isnt expected.length
# Loop 2: All values of "actual" MUST be in "expected".
return false if (item for item in actual when expected.indexOf(item) > -1).length isnt actual.length
true
test.ok equalArrayItems array1, array2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment