Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimise 0 and 1 argument cases for union & difference.

  • Loading branch information...
commit 0a2128ae000c865937f8518e7e88dc613bfc8d79 1 parent 1408d22
@Gozala authored
Showing with 6 additions and 3 deletions.
  1. +3 −2 difference.js
  2. +3 −1 union.js
View
5 difference.js
@@ -4,8 +4,9 @@ var slicer = Array.prototype.slice
var concater = Array.prototype.concat
var excludes = function excludes(item) { return this.indexOf(item) < 0 }
-module.exports = function difference(source, exclude) {
- if (!source) throw TypeError("difference requires at least two arguments")
+module.exports = function difference(source, exclude, rest) {
+ if (!source) throw TypeError("difference requires at least one arguments")
+ if (!exclude) return source
exclude = concater.apply([], slicer.call(arguments, 1))
return source.filter(excludes, exclude)
}
View
4 union.js
@@ -10,6 +10,8 @@ function include(union, set) {
return set.reduce(add, union)
}
-module.exports = function union() {
+module.exports = function union(first, second, rest) {
+ if (!first) return []
+ if (!second) return first
return slicer.call(arguments).reduce(include, [])
}
Please sign in to comment.
Something went wrong with that request. Please try again.