ddd-jquery
Version:
de-bowerified, de-amdified, de-gruntified... jQuery.
278 lines • 9.24 kB
JavaScript
var arr = require('./var/arr'), slice = require('./var/slice'), concat = require('./var/concat'), push = require('./var/push'), indexOf = require('./var/indexOf'), class2type = require('./var/class2type'), toString = require('./var/toString'), hasOwn = require('./var/hasOwn'), trim = require('./var/trim'), support = require('./var/support');
var document = window.document, version = '2.1.0-beta3', jQuery = function (selector, context) {
return new jQuery.fn.init(selector, context);
}, rmsPrefix = /^-ms-/, rdashAlpha = /-([\da-z])/gi, fcamelCase = function (all, letter) {
return letter.toUpperCase();
};
jQuery.fn = jQuery.prototype = {
jquery: version,
constructor: jQuery,
selector: '',
length: 0,
toArray: function () {
return slice.call(this);
},
get: function (num) {
return num != null ? num < 0 ? this[num + this.length] : this[num] : slice.call(this);
},
pushStack: function (elems) {
var ret = jQuery.merge(this.constructor(), elems);
ret.prevObject = this;
ret.context = this.context;
return ret;
},
each: function (callback, args) {
return jQuery.each(this, callback, args);
},
map: function (callback) {
return this.pushStack(jQuery.map(this, function (elem, i) {
return callback.call(elem, i, elem);
}));
},
slice: function () {
return this.pushStack(slice.apply(this, arguments));
},
first: function () {
return this.eq(0);
},
last: function () {
return this.eq(-1);
},
eq: function (i) {
var len = this.length, j = +i + (i < 0 ? len : 0);
return this.pushStack(j >= 0 && j < len ? [this[j]] : []);
},
end: function () {
return this.prevObject || this.constructor(null);
},
push: push,
sort: arr.sort,
splice: arr.splice
};
jQuery.extend = jQuery.fn.extend = function () {
var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false;
if (typeof target === 'boolean') {
deep = target;
target = arguments[i] || {};
i++;
}
if (typeof target !== 'object' && !jQuery.isFunction(target)) {
target = {};
}
if (i === length) {
target = this;
i--;
}
for (; i < length; i++) {
if ((options = arguments[i]) != null) {
for (name in options) {
src = target[name];
copy = options[name];
if (target === copy) {
continue;
}
if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) {
if (copyIsArray) {
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : [];
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
target[name] = jQuery.extend(deep, clone, copy);
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
};
jQuery.extend({
expando: 'jQuery' + (version + Math.random()).replace(/\D/g, ''),
isReady: true,
error: function (msg) {
throw new Error(msg);
},
noop: function () {
},
isFunction: function (obj) {
return jQuery.type(obj) === 'function';
},
isArray: Array.isArray,
isWindow: function (obj) {
return obj != null && obj === obj.window;
},
isNumeric: function (obj) {
return obj - parseFloat(obj) >= 0;
},
isPlainObject: function (obj) {
if (jQuery.type(obj) !== 'object' || obj.nodeType || jQuery.isWindow(obj)) {
return false;
}
try {
if (obj.constructor && !hasOwn.call(obj.constructor.prototype, 'isPrototypeOf')) {
return false;
}
} catch (e) {
return false;
}
return true;
},
isEmptyObject: function (obj) {
var name;
for (name in obj) {
return false;
}
return true;
},
type: function (obj) {
if (obj == null) {
return obj + '';
}
return typeof obj === 'object' || typeof obj === 'function' ? class2type[toString.call(obj)] || 'object' : typeof obj;
},
globalEval: function (code) {
var script, indirect = eval;
code = jQuery.trim(code);
if (code) {
if (code.indexOf('use strict') === 1) {
script = document.createElement('script');
script.text = code;
document.head.appendChild(script).parentNode.removeChild(script);
} else {
indirect(code);
}
}
},
camelCase: function (string) {
return string.replace(rmsPrefix, 'ms-').replace(rdashAlpha, fcamelCase);
},
nodeName: function (elem, name) {
return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase();
},
each: function (obj, callback, args) {
var value, i = 0, length = obj.length, isArray = isArraylike(obj);
if (args) {
if (isArray) {
for (; i < length; i++) {
value = callback.apply(obj[i], args);
if (value === false) {
break;
}
}
} else {
for (i in obj) {
value = callback.apply(obj[i], args);
if (value === false) {
break;
}
}
}
} else {
if (isArray) {
for (; i < length; i++) {
value = callback.call(obj[i], i, obj[i]);
if (value === false) {
break;
}
}
} else {
for (i in obj) {
value = callback.call(obj[i], i, obj[i]);
if (value === false) {
break;
}
}
}
}
return obj;
},
trim: function (text) {
return text == null ? '' : trim.call(text);
},
makeArray: function (arr, results) {
var ret = results || [];
if (arr != null) {
if (isArraylike(Object(arr))) {
jQuery.merge(ret, typeof arr === 'string' ? [arr] : arr);
} else {
push.call(ret, arr);
}
}
return ret;
},
inArray: function (elem, arr, i) {
return arr == null ? -1 : indexOf.call(arr, elem, i);
},
merge: function (first, second) {
var len = +second.length, j = 0, i = first.length;
for (; j < len; j++) {
first[i++] = second[j];
}
first.length = i;
return first;
},
grep: function (elems, callback, invert) {
var callbackInverse, matches = [], i = 0, length = elems.length, callbackExpect = !invert;
for (; i < length; i++) {
callbackInverse = !callback(elems[i], i);
if (callbackInverse !== callbackExpect) {
matches.push(elems[i]);
}
}
return matches;
},
map: function (elems, callback, arg) {
var value, i = 0, length = elems.length, isArray = isArraylike(elems), ret = [];
if (isArray) {
for (; i < length; i++) {
value = callback(elems[i], i, arg);
if (value != null) {
ret.push(value);
}
}
} else {
for (i in elems) {
value = callback(elems[i], i, arg);
if (value != null) {
ret.push(value);
}
}
}
return concat.apply([], ret);
},
guid: 1,
proxy: function (fn, context) {
var tmp, args, proxy;
if (typeof context === 'string') {
tmp = fn[context];
context = fn;
fn = tmp;
}
if (!jQuery.isFunction(fn)) {
return undefined;
}
args = slice.call(arguments, 2);
proxy = function () {
return fn.apply(context || this, args.concat(slice.call(arguments)));
};
proxy.guid = fn.guid = fn.guid || jQuery.guid++;
return proxy;
},
now: Date.now,
support: support
});
jQuery.each('Boolean Number String Function Array Date RegExp Object Error'.split(' '), function (i, name) {
class2type['[object ' + name + ']'] = name.toLowerCase();
});
function isArraylike(obj) {
var length = obj.length, type = jQuery.type(obj);
if (type === 'function' || jQuery.isWindow(obj)) {
return false;
}
if (obj.nodeType === 1 && length) {
return true;
}
return type === 'array' || length === 0 || typeof length === 'number' && length > 0 && length - 1 in obj;
}
module.exports = jQuery;