forked from 0x2620/oxjs
merging changes
This commit is contained in:
parent
e2f6492638
commit
9842418f0d
111 changed files with 5275 additions and 930 deletions
170
build/js/ox.js
170
build/js/ox.js
|
|
@ -5,6 +5,19 @@ Ox = {
|
|||
version: "0.1.2"
|
||||
};
|
||||
|
||||
/*
|
||||
================================================================================
|
||||
Constants
|
||||
================================================================================
|
||||
*/
|
||||
|
||||
Ox.AMPM = ["AM", "PM"];
|
||||
Ox.DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
||||
Ox.MONTHS = ["January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December"];
|
||||
Ox.WEEKDAYS = ["Monday", "Tuesday", "Wednesday", "Thursday",
|
||||
"Friday", "Saturday", "Sunday"];
|
||||
|
||||
/*
|
||||
================================================================================
|
||||
Core functions
|
||||
|
|
@ -60,10 +73,12 @@ Ox.print = function() {
|
|||
}
|
||||
|
||||
Ox.uid = function() {
|
||||
/*
|
||||
/***
|
||||
Ox.uid
|
||||
returns a unique id
|
||||
>>> Ox.uid() == Ox.uid()
|
||||
false
|
||||
*/
|
||||
***/
|
||||
var uid = 0;
|
||||
return function() {
|
||||
return uid++;
|
||||
|
|
@ -105,12 +120,14 @@ Array and Object functions
|
|||
*/
|
||||
|
||||
Ox.avg = function(obj) {
|
||||
/*
|
||||
/***
|
||||
Ox.avg(obj)
|
||||
returns the average of an array's values, or an object's properties
|
||||
>>> Ox.avg([-1, 0, 1])
|
||||
0
|
||||
>>> Ox.avg({"a": 1, "b": 2, "c": 3})
|
||||
2
|
||||
*/
|
||||
***/
|
||||
return Ox.sum(obj) / Ox.length(obj);
|
||||
};
|
||||
|
||||
|
|
@ -186,6 +203,40 @@ Ox.filter = function(arr, fn) {
|
|||
return ret;
|
||||
};
|
||||
|
||||
Ox.flatten = function(arr) {
|
||||
/*
|
||||
>>> Ox.flatten([1, [2, [3], 4], 5])
|
||||
[1, 2, 3, 4, 5]
|
||||
*/
|
||||
var ret = [];
|
||||
arr.forEach(function(v) {
|
||||
if (Ox.isArray(v)) {
|
||||
Ox.flatten(v).forEach(function(v) {
|
||||
ret.push(v);
|
||||
});
|
||||
} else {
|
||||
ret.push(v);
|
||||
}
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
Ox.find = function(arr, str) {
|
||||
/*
|
||||
>>> Ox.find(["foo", "bar", "foobar", "barfoo"], "foo")
|
||||
[["foo", "foobar"], ["barfoo"]]
|
||||
*/
|
||||
var arrLowerCase = arr.map(function(v) {
|
||||
return v.toLowerCase();
|
||||
}),
|
||||
ret = [[], []];
|
||||
str && arrLowerCase.forEach(function(v, i) {
|
||||
var index = v.indexOf(str.toLowerCase());
|
||||
index > -1 && ret[index == 0 ? 0 : 1].push(arr[i]);
|
||||
});
|
||||
return ret;
|
||||
}
|
||||
|
||||
Ox.getObjectById = function(arr, id) {
|
||||
var ret = null;
|
||||
Ox.each(arr, function(i, v) {
|
||||
|
|
@ -244,6 +295,7 @@ Ox.makeArray = function(arr) {
|
|||
>>> (function() { return Ox.makeArray(arguments); })(["foo"])
|
||||
["foo"]
|
||||
*/
|
||||
// fixme: this doesn't work for numbers
|
||||
var ret = [], i = 0, len = arr.length;
|
||||
if (Ox.isString(arr)) {
|
||||
ret = [arr];
|
||||
|
|
@ -335,6 +387,10 @@ Ox.range = function(start, stop, step) {
|
|||
};
|
||||
|
||||
Ox.serialize = function(obj) {
|
||||
/*
|
||||
>>> Ox.serialize({a: 0, b: 1})
|
||||
a=0&b=1
|
||||
*/
|
||||
var arr = [];
|
||||
Ox.each(obj, function(k, v) {
|
||||
arr.push(k + "=" + v);
|
||||
|
|
@ -342,6 +398,22 @@ Ox.serialize = function(obj) {
|
|||
return arr.join("&");
|
||||
};
|
||||
|
||||
Ox.setPropertyOnce = function(arr, str) {
|
||||
var pos = -1;
|
||||
Ox.each(arr, function(i, v) {
|
||||
if (pos == -1 && arr[i][str]) {
|
||||
pos = i;
|
||||
} else if (pos > -1 && arr[i][str]) {
|
||||
delete arr[i][str];
|
||||
}
|
||||
});
|
||||
if (pos == -1) {
|
||||
arr[0][str] = true;
|
||||
pos = 0;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
Ox.shuffle = function(arr) {
|
||||
/*
|
||||
>>> Ox.shuffle([1, 2, 3]).length
|
||||
|
|
@ -514,19 +586,27 @@ Date functions
|
|||
================================================================================
|
||||
*/
|
||||
|
||||
Ox.getFirstDayOfTheYear = function(date) {
|
||||
Ox.getDateInWeek = function(date, weekday) {
|
||||
/*
|
||||
Decimal weekday of January 1 (0-6, Sunday as first day)
|
||||
>>> Ox.getFirstDayOfTheYear(new Date("01/01/00"))
|
||||
6
|
||||
>>> Ox.formatDate(Ox.getDateInWeek(new Date("January 1 2000"), "Sunday"), "%A, %B %e, %Y")
|
||||
"Sunday, January 2, 2000"
|
||||
>>> Ox.formatDate(Ox.getDateInWeek(new Date("Jan 1 2000"), "Fri"), "%A, %B %e, %Y")
|
||||
"Friday, December 31, 1999"
|
||||
>>> Ox.formatDate(Ox.getDateInWeek(new Date("1/1/2000"), 1), "%A, %B %e, %Y")
|
||||
"Monday, December 27, 1999"
|
||||
*/
|
||||
var date_ = date ? new Date(date.valueOf()) : new Date();
|
||||
date_.setMonth(0);
|
||||
date_.setDate(1);
|
||||
return date_.getDay();
|
||||
};
|
||||
Ox.print("getDateInWeek", date.toString(), weekday)
|
||||
var date = date || new Date(),
|
||||
sourceWeekday = Ox.formatDate(date, "%u");
|
||||
targetWeekday = Ox.isNumber(weekday) ? weekday :
|
||||
Ox.map(Ox.WEEKDAYS, function(v, i) {
|
||||
return v.substr(0, 3) == weekday.substr(0, 3) ? i + 1 : null;
|
||||
})[0];
|
||||
date.setDate(date.getDate() - sourceWeekday + targetWeekday);
|
||||
return date;
|
||||
}
|
||||
|
||||
Ox.getDayOfTheYear = function() {
|
||||
Ox.getDayOfTheYear = function(date) {
|
||||
/*
|
||||
>>> Ox.getDayOfTheYear(new Date("12/31/2004"))
|
||||
366
|
||||
|
|
@ -547,6 +627,36 @@ Ox.getDayOfTheYear = function() {
|
|||
};
|
||||
}();
|
||||
|
||||
Ox.getDaysInMonth = function(year, month) {
|
||||
/*
|
||||
>>> Ox.getDaysInMonth(2000, 2)
|
||||
28
|
||||
>>> Ox.getDaysInMonth("2002", "Feb")
|
||||
28
|
||||
>>> Ox.getDaysInMonth("2004", "February")
|
||||
29
|
||||
*/
|
||||
Ox.print("getDaysInMonth", year, month)
|
||||
var year = parseInt(year),
|
||||
month = Ox.isNumber(month) ? month :
|
||||
Ox.map(Ox.MONTHS, function(v, i) {
|
||||
return v.substr(0, 3) == month.substr(0, 3) ? i + 1 : null;
|
||||
})[0];
|
||||
return Ox.DAYS[month - 1] + (month == 2 && Ox.isLeapYear(year));
|
||||
}
|
||||
|
||||
Ox.getFirstDayOfTheYear = function(date) {
|
||||
/*
|
||||
Decimal weekday of January 1 (0-6, Sunday as first day)
|
||||
>>> Ox.getFirstDayOfTheYear(new Date("01/01/00"))
|
||||
6
|
||||
*/
|
||||
var date_ = date ? new Date(date.valueOf()) : new Date();
|
||||
date_.setMonth(0);
|
||||
date_.setDate(1);
|
||||
return date_.getDay();
|
||||
};
|
||||
|
||||
Ox.getISODate = function(date) {
|
||||
/*
|
||||
>>> Ox.getISODate(new Date("01/01/2000"))
|
||||
|
|
@ -656,9 +766,7 @@ Ox.canvas = function() {
|
|||
c.context = (c.canvas = Ox.element("canvas").attr({
|
||||
width: image.width, height: image.height
|
||||
})[0]).getContext("2d");
|
||||
if (isImage) {
|
||||
c.context.drawImage(image, 0, 0);
|
||||
}
|
||||
isImage && c.context.drawImage(image, 0, 0);
|
||||
c.data = (c.imageData = c.context.getImageData(0, 0,
|
||||
image.width, image.height)).data;
|
||||
return c;
|
||||
|
|
@ -1321,6 +1429,23 @@ Ox.deg = function(rad) {
|
|||
return rad * 180 / Math.PI;
|
||||
};
|
||||
|
||||
Ox.divideInt = function(num, by) {
|
||||
/*
|
||||
>>> Ox.divideInt(100, 3)
|
||||
[33, 33, 34]
|
||||
>>> Ox.divideInt(100, 6)
|
||||
[16, 16, 17, 17, 17, 17]
|
||||
*/
|
||||
var arr = [],
|
||||
div = parseInt(num / by),
|
||||
mod = num % by,
|
||||
i;
|
||||
for (i = 0; i < by; i++) {
|
||||
arr[i] = div + (i > by - 1 - mod);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
Ox.limit = function(num, min, max) {
|
||||
/*
|
||||
>>> Ox.limit(1, 2, 3)
|
||||
|
|
@ -1328,7 +1453,6 @@ Ox.limit = function(num, min, max) {
|
|||
>>> Ox.limit(2, 1)
|
||||
1
|
||||
*/
|
||||
Ox.print(num, min, max)
|
||||
var len = arguments.length;
|
||||
max = arguments[len - 1];
|
||||
min = len == 3 ? min : 0;
|
||||
|
|
@ -1413,6 +1537,16 @@ Ox.clean = function(str) {
|
|||
return Ox.trim(str.replace(/\s+/g, " "));
|
||||
};
|
||||
|
||||
Ox.contains = function(str, chr) {
|
||||
/*
|
||||
>>> Ox.contains("foo", "bar")
|
||||
false
|
||||
>>> Ox.contains("foobar", "bar")
|
||||
true
|
||||
*/
|
||||
return str.indexOf(chr) > -1;
|
||||
};
|
||||
|
||||
Ox.endsWith = function(str, sub) {
|
||||
/*
|
||||
>>> Ox.endsWith("foobar", "bar")
|
||||
|
|
|
|||
4405
build/js/ox.ui.js
4405
build/js/ox.ui.js
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue