Created
September 9, 2011 06:43
-
-
Save m242/1205631 to your computer and use it in GitHub Desktop.
Scala Option in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MyOption, Option; | |
Array.prototype.filter = function(f) { | |
var x, _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = this.length; _i < _len; _i++) { | |
x = this[_i]; | |
if (f(x)) { | |
_results.push(x); | |
} | |
} | |
return _results; | |
}; | |
Number.prototype.filter = function(f) { | |
return [this.valueOf()].filter(f); | |
}; | |
String.prototype.filter = Number.prototype.filter; | |
Object.prototype.filter = function(f) { | |
if (f(this)) { | |
return this; | |
} else { | |
return null; | |
} | |
}; | |
Array.prototype.foreach = function(f) { | |
var x, _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = this.length; _i < _len; _i++) { | |
x = this[_i]; | |
_results.push(f(x)); | |
} | |
return _results; | |
}; | |
Number.prototype.foreach = function(f) { | |
return [this.valueOf()].foreach(f); | |
}; | |
String.prototype.foreach = Number.prototype.foreach; | |
Object.prototype.foreach = function(f) { | |
if (this) { | |
return f(this); | |
} else { | |
return null; | |
} | |
}; | |
Array.prototype.exists = function(f) { | |
var item, _i, _len; | |
for (_i = 0, _len = this.length; _i < _len; _i++) { | |
item = this[_i]; | |
if (f(item)) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
Number.prototype.exists = function(f) { | |
return [this.valueOf()].exists(f); | |
}; | |
String.prototype.exists = Number.prototype.exists; | |
Object.prototype.exists = function(f) { | |
return f(this); | |
}; | |
Array.prototype.map = function(f) { | |
var x, _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = this.length; _i < _len; _i++) { | |
x = this[_i]; | |
_results.push(f(x)); | |
} | |
return _results; | |
}; | |
Number.prototype.map = function(f) { | |
return [this.valueOf()].map(f); | |
}; | |
String.prototype.map = Number.prototype.map; | |
Object.prototype.map = function(f) { | |
if (this) { | |
return f(this); | |
} else { | |
return null; | |
} | |
}; | |
MyOption = (function() { | |
function MyOption(item) { | |
this.item = item; | |
} | |
MyOption.prototype.getOrElse = function(def) { | |
if (this.item != null) { | |
return this.item; | |
} else { | |
return def; | |
} | |
}; | |
MyOption.prototype.exists = function(f) { | |
var _ref; | |
return (_ref = this.item) != null ? _ref.exists(f) : void 0; | |
}; | |
MyOption.prototype.get = function() { | |
return this.item; | |
}; | |
MyOption.prototype.isDefined = function() { | |
return this.item != null; | |
}; | |
MyOption.prototype.isEmpty = function() { | |
return !(this.item != null); | |
}; | |
MyOption.prototype.filter = function(f) { | |
var _ref; | |
return (_ref = this.item) != null ? _ref.filter(f) : void 0; | |
}; | |
MyOption.prototype.foreach = function(f) { | |
var _ref; | |
return (_ref = this.item) != null ? _ref.foreach(f) : void 0; | |
}; | |
MyOption.prototype.map = function(f) { | |
var _ref; | |
return (_ref = this.item) != null ? _ref.map(f) : void 0; | |
}; | |
MyOption.prototype.orElse = function(def) { | |
if (this.item != null) { | |
return this; | |
} else { | |
return def; | |
} | |
}; | |
MyOption.prototype.orNull = function() { | |
if (this.item != null) { | |
return this; | |
} else { | |
return null; | |
} | |
}; | |
return MyOption; | |
})(); | |
Option = function(x) { | |
return new MyOption(x); | |
}; | |
$(function() { | |
var x, y; | |
x = "Yes"; | |
y = null; | |
alert(Option(x).getOrElse("No")); | |
return alert(Option(y).getOrElse("No")); | |
}); |
Suggestions:
- support for
Some
&None
; - method
equals
to compareOption
s; - treat
undefined
asnull
so it isNone
too;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Came looking for a Javascript Option, and was amused to find you here first Landon!