Skip to content

Commit

Permalink
refactor new methods to use array awesomeness
Browse files Browse the repository at this point in the history
  • Loading branch information
madrobby committed Mar 1, 2011
1 parent 9c4af7f commit 1babc9a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 35 deletions.
49 changes: 22 additions & 27 deletions src/zepto.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ var Zepto = (function() {
function compact(array){ return array.filter(function(item){ return item !== undefined && item !== null }) }
function flatten(array){ return array.reduce(function(a,b){ return a.concat(b) }, []) }
function camelize(str){ return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' }) }
function uniq(array){
var r = [];
for(var i=0,n=array.length;i<n;i++)
if(r.indexOf(array[i])<0) r.push(array[i]);
return r;
}

fragmentRE = /^\s*<.+>/;
container = document.createElement("div");
Expand Down Expand Up @@ -41,6 +47,10 @@ var Zepto = (function() {
$.extend = function(target, source){ for (key in source) target[key] = source[key]; return target }
$.qsa = $$ = function(element, selector){ return slice.call(element.querySelectorAll(selector)) }

function filtered(nodes, selector){
return selector === undefined ? $(nodes) : $(nodes).filter(selector);
}

$.fn = {
forEach: [].forEach,
map: [].map,
Expand Down Expand Up @@ -68,14 +78,15 @@ var Zepto = (function() {
},
not: function(selector){
var nodes=[];
if (typeof selector == 'function' && selector.call !== undefined){
if (typeof selector == 'function' && selector.call !== undefined)

This comment has been minimized.

Copy link
@stereobooster

stereobooster Mar 21, 2011

what is the case for this check?
selector.call !== undefined

This comment has been minimized.

Copy link
@ekhaled

ekhaled Mar 21, 2011

Contributor

Check this commit:
37d2ed6

This comment has been minimized.

Copy link
@stereobooster

stereobooster Mar 21, 2011

Thanks
Safari thinks typeOf NodeList == function
Chrome thinks typeof NodeList == Object
how about?
function isFunction(value) { return ({}).toString.call(obj) == "[object Function]" }

this.each(function(idx){
if (!selector.call(this,idx)) nodes.push(this);
});
}else{
var ignores=slice.call(typeof selector === "string" ?
this.filter(selector) :
selector instanceof NodeList ? selector : $(selector));
else {
var ignores = slice.call(
typeof selector === "string" ?
this.filter(selector) :
selector instanceof NodeList ? selector : $(selector));
slice.call(this).forEach(function(el){
if (ignores.indexOf(el) < 0) nodes.push(el);
});
Expand Down Expand Up @@ -106,34 +117,18 @@ var Zepto = (function() {
return node;
}
}));
ancestors = $(ancestors);
return selector === undefined ? ancestors : ancestors.filter(selector);
return filtered(ancestors, selector);
},
parent: function(selector){
var node, nodes = [];
this.each(function(){
if ((node = this.parentNode) && nodes.indexOf(node) < 0) nodes.push(node);
});
nodes = $(nodes);
return selector === undefined ? nodes : nodes.filter(selector);
return filtered(uniq(compact(this.pluck('parentNode'))), selector);
},
children: function(selector){
var nodes=[];
this.each(function(){
slice.call(this.children).forEach(function(el){
nodes.push(el);
})
});
return selector === undefined ? $(nodes) : $(nodes).filter(selector);
return filtered(flatten(this.map(function(el){ return slice.call(el.children) })), selector);
},
siblings: function(selector){
var node, nodes=[];
this.each(function(){
slice.call((node = this).parentNode.children).forEach(function(el){
if (node !== el) nodes.push(el);
})
});
return selector === undefined ? $(nodes) : $(nodes).filter(selector);
return filtered(flatten(this.map(function(el){
return slice.call(el.parentNode.children).filter(function(child){ return child!==el });
})), selector);
},
pluck: function(property){ return this.map(function(element){ return element[property] }) },
show: function(){ return this.css('display', 'block') },
Expand Down
2 changes: 1 addition & 1 deletion test/ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Zepto Ajax unit tests</title>
<script src="../src/compat.js"></script>
<script src="../src/polyfill.js"></script>
<script src="../src/zepto.js"></script>
<script src="../src/ajax.js"></script>
<script src="../vendor/evidence.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion test/assets_functional.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<title>Zepto assets functional test</title>
<meta name="viewport" content="maximum-scale=1,initial-scale=1,user-scalable=0">
<script src="../src/compat.js"></script>
<script src="../src/polyfill.js"></script>
<script src="../src/zepto.js"></script>
<style>
#container div {
Expand Down
2 changes: 1 addition & 1 deletion test/detect.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Zepto environment detection unit tests</title>
<script src="../src/compat.js"></script>
<script src="../src/polyfill.js"></script>
<script src="../src/zepto.js"></script>
<script src="../src/detect.js"></script>
<script src="../vendor/evidence.js"></script>
Expand Down
47 changes: 47 additions & 0 deletions test/polyfill.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Zepto Compatibility unit tests</title>
<script src="../src/polyfill.js"></script>
<script src="../src/zepto.js"></script>
<script src="../vendor/evidence.js"></script>
</head>
<body>
<h1>Zepto Compatibility unit tests</h1>
<p>
See the browser console for results.
</p>
<script>
Evidence.TestCase.extend('ZeptoTest', {

// test to see if we augment String.prototype.trim if not supported natively
testTrim: function(t){
t.assertEqual("blah", " blah ".trim());
},

// test to see if we augment Array.prototype.reduceif not supported natively
testReduce: function(t){
t.assertEqual(
10,
[0,1,2,3,4].reduce(function(p,c){ return p+c })
);

t.assertEqual(
20,
[0,1,2,3,4].reduce(function(p,c){ return p+c }, 10)
);

var flattened = [[0,1], [2,3], [4,5]].reduce(function(a,b){
return a.concat(b);
});

t.assertEqual(6, flattened.length);

for(var i=0;i<6;i++) t.assertEqual(i, flattened[i]);
}

});
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion test/touch_functional.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<title>Zepto touch functional test</title>
<meta name="viewport" content="maximum-scale=1,initial-scale=1,user-scalable=0">
<script src="../src/compat.js"></script>
<script src="../src/polyfill.js"></script>
<script src="../src/zepto.js"></script>
<script src="../src/event.js"></script>
<script src="../src/touch.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion test/touchcancel_functional.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="utf-8">
<title>Zepto touch functional test</title>
<meta name="viewport" content="maximum-scale=1,initial-scale=1,user-scalable=0">
<script src="../src/compat.js"></script>
<script src="../src/polyfill.js"></script>
<script src="../src/zepto.js"></script>
<script src="../src/event.js"></script>
<script src="../src/touch.js"></script>
Expand Down
4 changes: 1 addition & 3 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<title>Zepto DOM unit tests</title>
<script src="../src/compat.js"></script>
<script src="../src/polyfill.js"></script>
<script src="../src/zepto.js"></script>
<script src="../src/event.js"></script>
<script src="../vendor/evidence.js"></script>
Expand Down Expand Up @@ -820,8 +820,6 @@ <h1>Zepto DOM unit tests</h1>
var els = $('#attr_with_text_input');

els.get(0).removeAttribute('disabled');
console.log(els[0].disabled);
console.log(els.attr('disabled'))
t.assertFalse(els.attr('disabled'));

els.attr('disabled', 'definitely');
Expand Down

0 comments on commit 1babc9a

Please sign in to comment.