some more documentation; reimplement Ox.range using Ox.loop

This commit is contained in:
rolux 2011-05-07 00:47:17 +02:00
parent ae80b6db54
commit 8c4bc3b415
5 changed files with 305 additions and 243 deletions

View file

@ -1,122 +1,18 @@
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html> <html>
<head> <head>
<style> <script type="text/javascript" src="../../build/Ox.js"></script>
table { <script>
//border: 1px solid black; var element = document.createElement('script');
} element.src = '../../build/Ox.UI/js/Map/Ox.Map.js';
td { element.type = 'text/javascript';
//border: 1px solid red; document.head.appendChild(element);
} </script>
</style> <script>
Ox.documentReady(function() {
Ox.print(Ox.Map)
})
</script>
</head> </head>
<body> <body></body>
<table>
<tr>
<td>Ox.foo</td>
<td>&lt;foo&gt;</td>
<td>foo</td>
</tr>
</table>
<table>
<tr>
<td width=16/>
<td><b>Usage</b></td>
</tr>
</table>
<table>
<tr>
<td width=32/>
<td>Ox.foo(foo)</td>
<td>returns</td>
<td>&lt;foo&gt;</td>
<td>foo</td>
</tr>
<tr>
<td width=32/>
<td>Ox.foo(foo, bar)</td>
<td>returns</td>
<td>&lt;foobar&gt;</td>
<td>foobar</td>
</tr>
</table>
<table>
<tr>
<td width=16/>
<td><b>Arguments</b></td>
</tr>
</table>
<table>
<tr>
<td width=32/>
<td>foo</td>
<td>&lt;foo&gt;</td>
<td>foo</td>
<td width=300></td>
</tr>
<tr>
<td colspan=5>
<table>
<tr>
<td width=48/>
<td><b>Properties</b></td>
</tr>
</table>
<table>
<tr>
<td width=64/>
<td>foo</td>
<td>&lt;foo&gt;</td>
<td>foo</td>
<td width=100></td>
</tr>
<tr>
<td colspan=5>
<table>
<tr>
<td width=80/>
<td><b>Properties</b></td>
</tr>
</table>
<table>
<tr>
<td width=96/>
<td>foo</td>
<td>&lt;foo&gt;</td>
<td>foo</td>
<td width=100></td>
</tr>
<tr>
<td width=96/>
<td>foobar</td>
<td>&lt;foobar&gt;</td>
<td>foobar</td>
<td width=100></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width=64/>
<td>foobar</td>
<td>&lt;foobar&gt;</td>
<td>foobar</td>
<td width=100></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width=32/>
<td>foobar</td>
<td>&lt;foobar&gt;</td>
<td>foobar</td>
<td width=300></td>
</tr>
</table>
</body>
<html> <html>

View file

@ -8,10 +8,12 @@ Ox.DocPage = function(options, self) {
.options(options || {}) .options(options || {})
.addClass('OxText') .addClass('OxText')
.css({ .css({
width: '640px' width: '640px',
height: '400px',
overflow: 'auto'
}); });
$('body').css('overflowY', 'auto') $('body').css('overflowY', 'scroll')
that.append($('<h1>').append('<code>' + self.options.doc.name + '</code>')); that.append($('<h1>').append('<code>' + self.options.doc.name + '</code>'));
@ -85,12 +87,12 @@ Ox.DocPage = function(options, self) {
.addClass(className) .addClass(className)
.css({marginLeft: ((level * 32 + 32) + 'px')}) .css({marginLeft: ((level * 32 + 32) + 'px')})
.html( .html(
'<code><b>&gt;</b>&nbsp;' + '<code><b>&gt;&nbsp;' +
Ox.encodeHTML(example.statement) Ox.encodeHTML(example.statement)
//example.statement //example.statement
.replace(/ /g, '&nbsp;') .replace(/ /g, '&nbsp;')
.replace(/\n/g, '<br/>\n&nbsp;&nbsp;') + .replace(/\n/g, '<br/>\n&nbsp;&nbsp;') +
'</code>' '</b></code>'
) )
); );
example.result && $elements.push($('<div>') example.result && $elements.push($('<div>')

View file

@ -35,6 +35,8 @@ Ox.SyntaxHighlighter = function(options, self) {
.replace(/\r\n/g, '\n') .replace(/\r\n/g, '\n')
.replace(/\r/g, '\n'); .replace(/\r/g, '\n');
//self.options.source = Ox.minify(self.options.source);
self.cursor = 0; self.cursor = 0;
self.source = ''; self.source = '';
self.tokens = Ox.tokenize(self.options.source); self.tokens = Ox.tokenize(self.options.source);

View file

@ -10,7 +10,7 @@
Ox <f> The <code>Ox</code> object Ox <f> The <code>Ox</code> object
See <code>Ox.wrap</code> for details. See <code>Ox.wrap</code> for details.
(value) -> <o> wrapped value (value) -> <o> wrapped value
value <*> any value value <*> Any value
@*/ @*/
Ox = function(val) { Ox = function(val) {
@ -28,6 +28,7 @@ Ox.AMPM = ['AM', 'PM'];
//@ Ox.DURATIONS <[str]> ['year', 'month', 'day', 'minute', 'second'] //@ Ox.DURATIONS <[str]> ['year', 'month', 'day', 'minute', 'second']
Ox.DURATIONS = ['year', 'month', 'day', 'minute', 'second']; Ox.DURATIONS = ['year', 'month', 'day', 'minute', 'second'];
//@ Ox.EARTH_RADIUS <number> Radius of the earth in meters //@ Ox.EARTH_RADIUS <number> Radius of the earth in meters
// see http://en.wikipedia.org/wiki/WGS-84
Ox.EARTH_RADIUS = 6378137; Ox.EARTH_RADIUS = 6378137;
//@ Ox.EARTH_CIRCUMFERENCE <num> Circumference of the earth in meters //@ Ox.EARTH_CIRCUMFERENCE <num> Circumference of the earth in meters
Ox.EARTH_CIRCUMFERENCE = Ox.EARTH_RADIUS * 2 * Math.PI; Ox.EARTH_CIRCUMFERENCE = Ox.EARTH_RADIUS * 2 * Math.PI;
@ -36,8 +37,8 @@ Ox.HTML_ENTITIES = {
'"': '&quot;', '&': '&amp;', "'": '&apos;', '<': '&lt;', '>': '&gt;' '"': '&quot;', '&': '&amp;', "'": '&apos;', '<': '&lt;', '>': '&gt;'
}; };
//@ Ox.KEYS <object> Names for key codes //@ Ox.KEYS <object> Names for key codes
// the dot notation ('0.numpad') allows for namespaced events ('key_0.numpad'), // The dot notation ('0.numpad') allows for namespaced events ('key_0.numpad'),
// so that binding to 'key_0' will catch 'key0.numpad' too // so that binding to 'key_0' will catch both 'key_0' and 'key_0.numpad'.
Ox.KEYS = { Ox.KEYS = {
0: 'section', 8: 'backspace', 9: 'tab', 12: 'clear', 13: 'enter', 0: 'section', 8: 'backspace', 9: 'tab', 12: 'clear', 13: 'enter',
16: 'shift', 17: 'control', 18: 'alt', 20: 'capslock', 27: 'escape', 16: 'shift', 17: 'control', 18: 'alt', 20: 'capslock', 27: 'escape',
@ -209,7 +210,9 @@ Ox.doc = (function() {
lastItem.properties = lastItem.properties || []; lastItem.properties = lastItem.properties || [];
lastItem.properties.push(item); lastItem.properties.push(item);
// include leading linebreaks and whitespace // include leading linebreaks and whitespace
lastItem.source = Ox.merge(lastItem.source, parseTokens(tokens[i], true)); lastItem.source = Ox.merge(
lastItem.source, parseTokens(tokens[i], true)
);
} }
}); });
function decodeLinebreaks(match, submatch) { function decodeLinebreaks(match, submatch) {
@ -361,9 +364,9 @@ Ox.doc = (function() {
return Ox.startsWith(type, unwrapped); return Ox.startsWith(type, unwrapped);
})).length })).length
) { ) {
ret.types.push(wrap(type[0])) ret.types.push(wrap(type[0]));
} else { } else {
ret.default = str; ret['default'] = str;
} }
}); });
function unwrap(str) { function unwrap(str) {
@ -371,7 +374,8 @@ Ox.doc = (function() {
str = str.substr(1, str.length - 2) : str; str = str.substr(1, str.length - 2) : str;
} }
function wrap(str) { function wrap(str) {
return isArray ? '[' + str + ']' : str; return isArray ?
'array[' + str + (str != 'any' ? 's' : '') + ']' : str;
} }
return ret; return ret;
} }
@ -506,9 +510,12 @@ Ox.loadFile = (function() {
element.src = file; element.src = file;
} else { } else {
if (!findFileInHead()) { if (!findFileInHead()) {
element = document.createElement(type == 'css' ? 'link' : 'script'); element = document.createElement(
type == 'css' ? 'link' : 'script'
);
element[type == 'css' ? 'href' : 'src'] = file; element[type == 'css' ? 'href' : 'src'] = file;
element.type = type == 'css' ? 'text/css' : 'text/javascript'; element.type = type == 'css' ?
'text/css' : 'text/javascript';
if (type == 'css') { if (type == 'css') {
element.rel = 'stylesheet'; element.rel = 'stylesheet';
waitForCSS(); waitForCSS();
@ -573,7 +580,8 @@ Ox.print = function() {
date = new Date(); date = new Date();
args.unshift( args.unshift(
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3), Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3),
arguments.callee.caller.name || '(anonymous)' (arguments.callee.caller && arguments.callee.caller.name) ||
'(anonymous)'
); );
window.console && window.console.log.apply(window.console, args); window.console && window.console.log.apply(window.console, args);
return args.join(' '); return args.join(' ');
@ -599,7 +607,7 @@ Ox.wrap <f> Wraps a value so that one can directly call any Ox function on it
(value) -> <o> wrapped value (value) -> <o> wrapped value
chain <f> wrap the return value to allow chaining chain <f> wrap the return value to allow chaining
value <f> unwrap the value wrapped by chain() value <f> unwrap the value wrapped by chain()
value <*> any value value <*> Any value
> Ox.wrap("foobar").repeat(2) > Ox.wrap("foobar").repeat(2)
"foobarfoobar" "foobarfoobar"
> Ox.wrap("foobar").chain().reverse().toTitleCase().value() > Ox.wrap("foobar").chain().reverse().toTitleCase().value()
@ -728,7 +736,7 @@ Ox.every = function(obj, fn) {
Ox.extend = function() { Ox.extend = function() {
/* /*
>>> Ox.extend({a: 1, b: 1, c: 1}, {b: 2, c: 2}, {c: 3}).c >>> Ox.extend({a: 1, b: 1, c: 1}, {b: 2, c: 2}, {c: 3}).c
3 {"a":1,"b":2,"c":3}
*/ */
var obj = arguments[0]; var obj = arguments[0];
Ox.forEach(Array.prototype.slice.call(arguments, 1), function(arg, i) { Ox.forEach(Array.prototype.slice.call(arguments, 1), function(arg, i) {
@ -745,9 +753,9 @@ Ox.filter = function(obj, fn) {
>>> Ox.filter([2, 1, 0], function(v, i) { return v == i; }) >>> Ox.filter([2, 1, 0], function(v, i) { return v == i; })
[1] [1]
>>> Ox.keys(Ox.filter({a: 'c', b: 'b', c: 'a'}, function(v, k) { return v == k; })) >>> Ox.keys(Ox.filter({a: 'c', b: 'b', c: 'a'}, function(v, k) { return v == k; }))
['b'] ["b"]
>>> Ox.filter(' hello world ', function(v) { return v != ' '; }) >>> Ox.filter(' foo bar ', function(v) { return v != ' '; })
'helloworld' "foobar"
***/ ***/
var type = Ox.typeOf(obj), var type = Ox.typeOf(obj),
ret = type == 'array' ? [] : type == 'object' ? {} : ''; ret = type == 'array' ? [] : type == 'object' ? {} : '';
@ -816,20 +824,14 @@ Ox.forEach <f> forEach loop
callback <f> Callback function callback <f> Callback function
value <*> Value value <*> Value
key <n|s> Key key <n|s> Key
> var string = "";
> Ox.forEach(["f", "o", "o"], function(v, i) { string += i; })
> Ox.forEach({a: "f", b: "o", c: "o"}, function(v, k) { string += k; })
> Ox.forEach("foo", function(v) { string += v; })
> string
"012abcfoo"
@*/ @*/
Ox.forEach = function(obj, fn) { Ox.forEach = function(obj, fn) {
/*
Ox.forEach() works for arrays, objects and strings,
like $.each(), unlike [].forEach()
The arguments of the iterator function are (value, key),
like [].forEach(), unlike $.each()
>>> Ox.forEach('foo', function(v, i) {})
'foo'
>>> Ox.forEach([0, 1, 2], function(v, i) {})
[0, 1, 2]
>>> Ox.forEach({a: 1, b: 2, c: 3}, function(v, k) {}).a
1
*/
var isObject = Ox.isObject(obj), key; var isObject = Ox.isObject(obj), key;
for (key in obj) { for (key in obj) {
key = isObject ? key : parseInt(key); key = isObject ? key : parseInt(key);
@ -888,8 +890,12 @@ Ox.isEqual = function(obj0, obj1) {
true true
>>> Ox.isEqual({a: 1, b: [2, 3], c: {d: '4'}}, {a: 1, b: [2, 3], c: {d: '4'}}) >>> Ox.isEqual({a: 1, b: [2, 3], c: {d: '4'}}, {a: 1, b: [2, 3], c: {d: '4'}})
true true
>>> Ox.isEqual(function(arg) { return arg; }, function(arg) { return arg; }); >>> Ox.isEqual({a: 1, b: 2, c: 3}, {c: 3, b: 2, a: 1})
true // FIXME: is false
>>> Ox.isEqual(function(arg) { return arg; }, function(arg) { return arg; })
true true
>>> Ox.isEqual(function(foo) { return foo; }, function(bar) { return bar; })
false
*/ */
var ret = false; var ret = false;
if (obj0 === obj1) { if (obj0 === obj1) {
@ -967,25 +973,59 @@ Ox.len = function(obj) {
return Ox.isObject(obj) ? Ox.values(obj).length : obj.length; return Ox.isObject(obj) ? Ox.values(obj).length : obj.length;
}; };
/*@
Ox.loop <f> For-loop, functional-style
Returning <code>false</code> from the iterater function acts like a
<code>break</code> statement. Unlike a <code>for</code> loop,
<code>Ox.loop</code> doesn't leak its counter variable to the outer scope,
but returns it.
(stop, callback) -> <n> Next value
equivalent to <code>for (var i = 0; i < stop; i++)</code>
(start, stop, callback) -> <n> Next value
equivalent to <code>for (var i = start; i < stop; i++)</code> or,
if <code>start</code> is larger than <code>stop</code>,
<code>for (var i = start; i > stop; i--)</code>
(start, stop, step) -> <n> Next value
equivalent to <code>for (var i = start; i < stop; i += step)</code>
start <n> Start value
stop <n> Stop value (exclusive)
step <n> Step value
callback <f> Iterator function
i <n> Counter value
> Ox.loop(0, 3, 2, function() {})
4
> Ox.loop(10, function(i) { return i != 4; })
4
@*/
Ox.loop = function() { Ox.loop = function() {
var length = arguments.length, var length = arguments.length,
fn = arguments[length - 1],
step = length == 4 ? arguments[2] : 1,
stop = arguments[length > 2 ? 1 : 0],
start = length > 2 ? arguments[0] : 0, start = length > 2 ? arguments[0] : 0,
stop = arguments[length > 2 ? 1 : 0],
step = length == 4 ? arguments[2] : (start <= stop ? 1 : -1),
callback = arguments[length - 1],
i; i;
for (i = start; i < stop; i += step) { for (i = start; step > 0 ? i < stop : i > stop; i += step) {
fn(i); if (callback(i) === false) {
break;
};
} }
return i;
}; };
Ox.makeArray = function(obj) { /*@
/* Ox.makeArray <f> Takes an array-like object and returns a true array
>>> (function() { return Ox.makeArray(arguments); }('foo', 'bar')) Alias for <code>Array.prototype.slice.call</code>
['foo', 'bar'] (value) -> <a> True array
*/ value <*> Array-like object
return Array.prototype.slice.call(obj); > (function() { return Ox.makeArray(arguments); }("foo", "bar"))
}; ["foo", "bar"]
> Ox.makeArray("foo")
["f", "o", "o"]
> Ox.makeArray({0: "f", 1: "o", 2: "0", length: 3})
["f", "o", "o"]
@*/
Ox.makeArray = Array.prototype.slice.call;
Ox.makeObject = function(obj) { Ox.makeObject = function(obj) {
/* /*
@ -1066,22 +1106,38 @@ Ox.merge = function(arr) {
return arr; return arr;
}; };
Ox.range = function(start, stop, step) { /*@
/* Ox.range <f> Python-style range
>>> Ox.range(3) (stop) -> <[n]> range
Returns an array of integers from <code>0</code> (inclusive) to
<code>stop</code> (exclusive).
(start, stop) -> <[n]> range
Returns an array of integers from <code>start</code> (inclusive) to
<code>stop</code> (exclusive).
(start, stop, step) -> <[n]> range
Returns an array of numbers from <code>start</code> (inclusive) to
<code>stop</code> (exclusive), incrementing by <code>step</step>.
start <n> Start value
stop <n> Stop value
step <n> Step value
> Ox.range(3)
[0, 1, 2] [0, 1, 2]
>>> Ox.range(3, 0) > Ox.range(1, 4)
[1, 2, 3]
> Ox.range(3, 0)
[3, 2, 1] [3, 2, 1]
>>> Ox.range(1, 2, 0.5) > Ox.range(1, 2, 0.5)
[1, 1.5] [1, 1.5]
*/ > Ox.range(-1, -2, -0.5)
stop = arguments.length > 1 ? stop : arguments[0]; [-1, -1.5]
start = arguments.length > 1 ? start : 0; @*/
step = step || (start <= stop ? 1 : -1); Ox.range = function() {
var arr = [], i; var args = Ox.makeArray(arguments),
for (i = start; step > 0 ? i < stop : i > stop; i += step) { arr = [];
args.push(function(i) {
arr.push(i); arr.push(i);
} });
Ox.loop.apply(null, args);
return arr; return arr;
}; };
@ -2084,13 +2140,18 @@ Encoding functions
} }
} }
/*@
Ox.encodeUTF8 <f> Encodes a string as UTF-8
see http://en.wikipedia.org/wiki/UTF-8
(string) -> <s> UTF-8 encoded string
string <s> Any string
> Ox.encodeUTF8("foo")
"foo"
> Ox.encodeUTF8("¥€$")
"\u00C2\u00A5\u00E2\u0082\u00AC\u0024"
@*/
Ox.encodeUTF8 = function(str) { Ox.encodeUTF8 = function(str) {
/* /*
see http://en.wikipedia.org/wiki/UTF-8
>>> Ox.encodeUTF8('foo')
'foo'
>>> Ox.encodeUTF8('¥€$')
'\u00C2\u00A5\u00E2\u0082\u00AC\u0024'
*/ */
return Ox.map(str, function(chr) { return Ox.map(str, function(chr) {
var code = chr.charCodeAt(0), var code = chr.charCodeAt(0),
@ -2109,12 +2170,18 @@ Encoding functions
}).join(''); }).join('');
} }
/*@
Ox.decodeUTF8 <f> Decodes an UTF-8-encoded string
see http://en.wikipedia.org/wiki/UTF-8
(utf8) -> <s> string
utf8 <s> Any UTF-8-encoded string
> Ox.decodeUTF8('foo')
'foo'
> Ox.decodeUTF8('\u00C2\u00A5\u00E2\u0082\u00AC\u0024')
'¥€$'
@*/
Ox.decodeUTF8 = function(str) { Ox.decodeUTF8 = function(str) {
/* /*
>>> Ox.decodeUTF8('foo')
'foo'
>>> Ox.decodeUTF8('\u00C2\u00A5\u00E2\u0082\u00AC\u0024')
'¥€$'
*/ */
var bytes = Ox.map(str, function(v) { var bytes = Ox.map(str, function(v) {
return v.charCodeAt(0); return v.charCodeAt(0);
@ -3208,6 +3275,49 @@ Ox.toDashes = function(str) {
}); });
}; };
Ox.minify = function(source) {
var tokens = Ox.tokenize(source)
return Ox.map(tokens, function(token, i) {
var ret = source.substr(token.offset, token.type == 'whitespace' ? 1 : token.length);
if (token.type == 'comment') {
ret = null;
} else if (
token.type == 'linebreak' && (
i == 0 || i == tokens.length - 1 ||
tokens[i - 1].type == 'linebreak' ||
tokens[i + 1].type == 'linebreak'
)
) {
ret = null;
} else if (
token.type == 'whitespace' && (
i == 0 || i == tokens.length - 1 ||
tokens[i - 1].type != 'keyword' || (
tokens[i + 1].type == 'operator'
)
)
) {
ret = null;
}
return ret;
}).join('');
};
/*@
Ox.tokenize <f> Tokenizes JavaScript
(source) -> <[o]> Array of tokens
length <n> Length of the token
offset <n> Offset of the token
type <s> Type of the token
Type can be <code>"comment"</code>, <code>"constant"</code>,
<code>"identifier"</code>, <code>"keyword"</code>,
<code>"linebreak"</code>, <code>"method"</code>,
<code>"number"</code>, <code>"object"</code>,
<code>"operator"</code>, <code>"property"</code>,
<code>"regexp"</code>, <code>"string"</code>
or <code>"whitespace"</code>
source <s> JavaScript source code
@*/
Ox.tokenize = (function() { Ox.tokenize = (function() {
// see https://github.com/mozilla/narcissus/blob/master/lib/jslex.js // see https://github.com/mozilla/narcissus/blob/master/lib/jslex.js
@ -3657,113 +3767,161 @@ Type functions
================================================================================ ================================================================================
*/ */
Ox.isArguments = function(val) { /*@
/* Ox.isArguments <f> Tests if a value is an arguments "array"
>>> Ox.isArguments((function() { return arguments; }())) (value) -> <b> True if the value is an arguments "array"
value <*> Any value
> Ox.isArguments((function() { return arguments; }()))
true true
*/ @*/
Ox.isArguments = function(val) {
return !!(val && val.toString() == '[object Arguments]'); return !!(val && val.toString() == '[object Arguments]');
} }
Ox.isArray = function(val) { // is in jQuery /*@
/* Ox.isArray <f> Tests if a value is an array
>>> Ox.isArray([]) (value) -> <b> True if the value is a date
value <*> Any value
> Ox.isArray([])
true true
*/ > Ox.isArray((function() { return arguments; }()))
false
> Ox.isArray({0: 0, length: 1})
false
@*/
Ox.isArray = function(val) {
return val instanceof Array; return val instanceof Array;
} }
Ox.isBoolean = function(val) { /*@
/* Ox.isBoolean <f> Tests if a value is boolean
>>> Ox.isBoolean(false) (value) -> <b> True if the value is boolean
value <*> Any value
> Ox.isBoolean(false)
true true
*/ @*/
Ox.isBoolean = function(val) {
return typeof val == 'boolean'; return typeof val == 'boolean';
}; };
Ox.isDate = function(val) { /*@
/* Ox.isDate <f> Tests if a value is a date
>>> Ox.isDate(new Date()) (value) -> <b> True if the value is a date
value <*> Any value
> Ox.isDate(new Date())
true true
*/ @*/
Ox.isDate = function(val) {
return val instanceof Date; return val instanceof Date;
}; };
Ox.isElement = function(val) { /*@
/* Ox.isElement <f> Tests if a value is a DOM element
>>> Ox.isElement(document.createElement()) (value) -> <b> True if the value is a DOM element
value <*> Any value
> Ox.isElement(document.createElement())
true true
*/ @*/
Ox.isElement = function(val) {
return !!(val && val.nodeType == 1); return !!(val && val.nodeType == 1);
}; };
Ox.isFunction = function(val) { // is in jQuery /*@
/* Ox.isFunction <f> Tests if a value is a function
>>> Ox.isFunction(function() {}) (value) -> <b> True if the value is a function
value <*> Any value
> Ox.isFunction(function() {})
true true
>>> Ox.isFunction(/ /) > Ox.isFunction(/ /)
false false
*/ @*/
Ox.isFunction = function(val) { // is in jQuery
return typeof val == 'function' && !Ox.isRegExp(val); return typeof val == 'function' && !Ox.isRegExp(val);
}; };
Ox.isInfinity = function(val) { /*@
/* Ox.isInfinity <f> Tests if a value is infinite
>>> Ox.isInfinity(Infinity) (value) -> <b> True if the value is infinite
value <*> Any value
> Ox.isInfinity(Infinity)
true true
>>> Ox.isInfinity(-Infinity) > Ox.isInfinity(-Infinity)
true true
>>> Ox.isInfinity(NaN) > Ox.isInfinity(NaN)
false false
*/ @*/
Ox.isInfinity = function(val) {
return typeof val == 'number' && !isFinite(val) && !Ox.isNaN(val); return typeof val == 'number' && !isFinite(val) && !Ox.isNaN(val);
}; };
Ox.isNaN = function(val) { /*@
/* Ox.isNaN <f> Tests if a value is "Not a Number"
>>> Ox.isNaN(NaN) (value) -> <b> True if the value is "Not a Number"
value <*> Any value
> Ox.isNaN(NaN)
true true
*/ @*/
Ox.isNaN = function(val) {
return val !== val; return val !== val;
} }
Ox.isNull = function(val) { /*@
/* Ox.isNull <f> Tests if a value is <code>null</code>
>>> Ox.isNull(null) (value) -> <b> True if the value is <code>null</null>
value <*> Any value
> Ox.isNull(null)
true true
*/ @*/
Ox.isNull = function(val) {
return val === null; return val === null;
}; };
Ox.isNumber = function(val) { /*@
/* Ox.isNumber <f> Tests if a value is a number
>>> Ox.isNumber(0) (value) -> <b> True if the value is a number
value <*> Any value
> Ox.isNumber(0)
true true
>>> Ox.isNumber(Infinity) > Ox.isNumber(Infinity)
false false
>>> Ox.isNumber(-Infinity) > Ox.isNumber(-Infinity)
false false
>>> Ox.isNumber(NaN) > Ox.isNumber(NaN)
false false
*/ @*/
Ox.isNumber = function(val) {
return typeof val == 'number' && isFinite(val); return typeof val == 'number' && isFinite(val);
}; };
Ox.isObject = function(val) { /*@
/* Ox.isObject <f> Tests if a value is a an object
>>> Ox.isObject({}) (value) -> <b> True if the value is an object
value <*> Any value
> Ox.isObject({})
true true
>>> Ox.isObject([]) > Ox.isObject([])
false false
>>> Ox.isObject(new Date()) > Ox.isObject(new Date())
false false
>>> Ox.isObject(null) > Ox.isObject(null)
false false
*/ @*/
Ox.isObject = function(val) {
return typeof val == 'object' && !Ox.isArguments(val) && return typeof val == 'object' && !Ox.isArguments(val) &&
!Ox.isArray(val) && !Ox.isDate(val) && !Ox.isNull(val); !Ox.isArray(val) && !Ox.isDate(val) && !Ox.isNull(val);
}; };
/*@
Ox.isRegExp <f> Tests if a value is a regular expression
(value) -> <b> True if the value is a regular expression
value <*> Any value
> Ox.isRegExp(/ /)
true
@*/
Ox.isRegExp = function(val) { Ox.isRegExp = function(val) {
/* /*
>>> Ox.isRegExp(/ /) >>> Ox.isRegExp(/ /)
@ -3772,24 +3930,28 @@ Ox.isRegExp = function(val) {
return val instanceof RegExp; return val instanceof RegExp;
}; };
Ox.isString = function(val) { /*@
/* Ox.isString <f> Tests if a value is a string
>>> Ox.isString('') (value) -> <b> True if the value is a string
value <*> Any value
> Ox.isString('')
true true
*/ @*/
Ox.isString = function(val) {
return typeof val == 'string'; return typeof val == 'string';
}; };
/*@ /*@
Ox.isUndefined <f> Tests if a value is undefined Ox.isUndefined <f> Tests if a value is undefined
(value) -> <b> If true, the value is undefined (value) -> <b> True if the value is undefined
value <*> any value value <*> Any value
> Ox.isUndefined() > Ox.isUndefined()
true true
@*/ @*/
Ox.isUndefined = function(val) { Ox.isUndefined = function(val) {
// fixme: void 0 is also nice // fixme: val === void 0 is also nice
return typeof val == 'undefined'; return typeof val == 'undefined';
}; };