1
0
Fork 0
forked from 0x2620/oxjs

various improvements in OxJS and OxUI

This commit is contained in:
rolux 2011-04-29 14:40:51 +02:00
commit 7380595c7e
48 changed files with 184 additions and 161 deletions

View file

@ -47,6 +47,11 @@ Ox.MONTHS = [
Ox.SHORT_MONTHS = Ox.MONTHS.map(function(val) {
return val.substr(0, 3);
});
Ox.PATH = Array.prototype.slice.apply(
document.getElementsByTagName('script')
).filter(function(element) {
return /Ox\.js$/.test(element.src);
})[0].src.replace('Ox.js', '');
Ox.PREFIXES = ['K', 'M', 'G', 'T', 'P'];
Ox.SYMBOLS = {
DOLLAR: '\u0024',
@ -143,12 +148,6 @@ Ox.getset = function(obj, args, callback, context) {
return ret;
}
Ox.PATH = Array.prototype.slice.apply(
document.getElementsByTagName('script')
).filter(function(element) {
return /Ox\.js$/.test(element.src);
})[0].src.replace('Ox.js', '');
Ox.load = function(module, options, callback) {
/***
loads Ox modules
@ -244,12 +243,15 @@ Ox.loadJSON = function(url, callback) {
Ox.print = function() {
/*
like console.log, but prepends timestamp and name of the caller function
*/
if (window.console) {
var args = Ox.makeArray(arguments),
date = new Date;
args.unshift(Ox.formatDate(date, '%H:%M:%S') + '.' +
(Ox.pad(+date % 1000, 3)));
date = new Date();
args.unshift(
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3),
arguments.callee.caller.name || '(anonymous)'
);
window.console.log.apply(window.console, args);
}
};
@ -582,6 +584,23 @@ Ox.keys = function(obj) {
return keys.sort();
};
Ox.last = function(arr, val) {
/***
>>> Ox.last([1, 2, 3])
3
>>> Ox.last([1, 2, 3], 4)
[1, 2, 4]
***/
var ret;
if (arguments.length == 1) {
ret = arr[arr.length - 1];
} else {
arr[arr.length - 1] = val;
ret = arr;
}
return ret;
};
Ox.len = function(obj) {
/*
>>> Ox.len([1, 2, 3])
@ -663,6 +682,7 @@ Ox.map = function(obj, fn) {
>>> Ox.map([,], function(v, i) { return i; })
[0]
*/
// fixme: return null to filter out is a bit esoteric
var isObject = Ox.isObject(obj),
ret = isObject ? {} : [];
Ox.forEach(obj, function(val, key) {
@ -791,13 +811,16 @@ Ox.sort = function(arr) {
['9', '10', 'a', 'B']
*/
var len, matches = {}, sort = {};
// find leading numbers
arr.forEach(function(val, i) {
var match = /^\d+/(val);
matches[val] = match ? match[0] : '';
});
// get length of longest leading number
len = Ox.max(Ox.map(matches, function(val) {
return val.length;
}));
// pad leading numbers, and make lower case
arr.forEach(function(val) {
sort[val] = (
matches[val] ?
@ -884,6 +907,7 @@ Ox.values = function(obj) {
>>> Ox.values([1,])
[1]
*/
// fixme: why doesn't this use map?
var values = [];
Ox.forEach(obj, function(val) {
values.push(val);
@ -2562,6 +2586,10 @@ Ox.contains = function(str, chr) {
false
>>> Ox.contains("foobar", "bar")
true
>>> Ox.contains(['foo', 'bar'], 'foo')
true
// fixme: rename to Ox.has or Ox.isIn?
// then it'd become convenient for arrays
*/
return str.indexOf(chr) > -1;
};
@ -2571,7 +2599,7 @@ Ox.endsWith = function(str, sub) {
>>> Ox.endsWith("foobar", "bar")
true
*/
return str.toString().substr(-sub.length) === sub;
return new RegExp(sub + '$').test(str);
};
Ox.highlight = function(txt, str) {
@ -2590,11 +2618,15 @@ Ox.isValidEmail = function(str) {
true
>>> Ox.isValidEmail("foo@bar")
false
>>> Ox.isValidEmail("foo@bar..com")
false
*/
return !!/^[0-9A-Z\.\+\-_]+@(?:[0-9A-Z\-]+\.)+[A-Z]{2,6}$/i(str);
}
Ox.pad = function(str, len, pad, pos) {
// fixme: slighly obscure signature
// fixme: weird for negative numbers
/*
>>> Ox.pad(1, 2)
"01"
@ -2608,24 +2640,34 @@ Ox.pad = function(str, len, pad, pos) {
"456abc"
*/
str = str.toString().substr(0, len);
pad = Ox.repeat(pad || "0", len - str.length);
pos = pos || "left";
str = pos == "left" ? pad + str : str + pad;
str = pos == "left" ?
pad = Ox.repeat(pad || '0', len - str.length);
pos = pos || 'left';
str = pos == 'left' ? pad + str : str + pad;
str = pos == 'left' ?
str.substr(str.length - len, str.length) :
str.substr(0, len);
return str;
};
Ox.repeat = function(str, num) {
Ox.repeat = function(obj, num) {
/*
fixme: make this work for arrays, like in python
works for arrays, numbers and strings
>>> Ox.repeat(1, 3)
"111"
>>> Ox.repeat("foo", 3)
"foofoofoo"
>>> Ox.repeat([1, 2], 3)
[1, 2, 1, 2, 1, 2]
*/
return num >= 1 ? new Array(num + 1).join(str.toString()) : '';
var ret;
if (Ox.isArray(obj)) {
ret = num >= 1 ? Ox.map(Ox.range(obj.length * num), function(v, i) {
return obj[i % obj.length]
}) : [];
} else {
ret = num >= 1 ? new Array(num + 1).join(obj.toString()) : '';
}
return ret;
};
Ox.reverse = function(str) {
@ -2640,8 +2682,11 @@ Ox.startsWith = function(str, sub) {
/*
>>> Ox.startsWith("foobar", "foo")
true
// fixme:
// !!(/^sub/(str)) is shorter than
// Ox.startsWith(str, sub) anyway
*/
return str.toString().substr(0, sub.length) === sub;
return new RegExp('^' + sub).test(str);
};
Ox.stripTags = function(str) {
@ -2656,7 +2701,7 @@ Ox.substr = function(str, start, stop) {
/***
Ox.substr behaves like str[start:stop] in Python
(or like str.substring() with negative values for stop)
not implemented
// fixme: needed?
>>> Ox.substr('foobar', 1)
"oobar"
>>> Ox.substr('foobar', -1)
@ -2704,12 +2749,11 @@ Ox.toDashes = function(str) {
Ox.tokenize = (function() {
// see https://github.com/mozilla/narcissus/blob/master/lib/jslex.js
// and https://developer.mozilla.org/en/JavaScript/Reference
var identifier = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_',
// see https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words
linebreak = '\n\r',
number = '0123456789',
// see https://developer.mozilla.org/en/JavaScript/Reference
operator = [
// arithmetic
'+', '-', '*', '/', '%', '++', '--',
@ -2837,7 +2881,8 @@ Ox.tokenize = (function() {
'RangeError', 'ReferenceError', 'RegExp',
'String', 'SyntaxError',
'TypeError',
'undefined', 'URIError'
'undefined', 'URIError',
'window'
],
property: [
// Function
@ -2980,7 +3025,6 @@ Ox.tokenize = (function() {
}());
Ox.toSlashes = function(str) {
/*
>>> Ox.toSlashes("fooBarBaz")
@ -3018,7 +3062,7 @@ Ox.toUnderscores = function(str) {
});
};
Ox.trim = function(str) { // is in jQuery
Ox.trim = function(str) { // is in jQuery, and in JavaScript itself
/*
Ox.trim(" foo ")
"foo"
@ -3082,6 +3126,7 @@ Ox.words = function(str) {
}
Ox.wordwrap = function(str, len, sep, bal, spa) {
// fixme: bad API, sep/bal/spa should be in options object
/*
>>> Ox.wordwrap("Anticonstitutionellement, Paris s'eveille", 25, "<br/>")
"Anticonstitutionellement, <br/>Paris s'eveille"