react-query
Advanced tools
Comparing version 0.0.4 to 0.0.5
177
index.js
@@ -9,11 +9,11 @@ var React = require("react"); | ||
var $ = function $(vnode) { | ||
var $ = function $(vnodes) { | ||
if(!(this instanceof $)) { | ||
return new $(vnode); | ||
return new $(vnodes); | ||
} | ||
if(_.isArray(vnode)) { | ||
this.vnodes = vnode; | ||
if(_.isArray(vnodes)) { | ||
this.vnodes = _.map(vnodes, $.coerceTextNode); | ||
} | ||
else { | ||
this.vnodes = [vnode]; | ||
this.vnodes = [$.coerceTextNode(vnodes)]; | ||
} | ||
@@ -67,12 +67,64 @@ }; | ||
toString: function toString(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode.props; | ||
} | ||
else if(_.isString(vnode)) { | ||
return vnode; | ||
} | ||
return $._toStringWithTabs(0)(vnode); | ||
}, | ||
squeezeChildren: function squeezeChildren(vnodes) { | ||
if(_.isArray(vnodes)) { | ||
if(vnodes.length === 1) { | ||
return vnodes[0]; | ||
} | ||
else { | ||
return vnodes; | ||
} | ||
} | ||
else { | ||
return vnodes; | ||
} | ||
}, | ||
maybeChildren: function maybeChildren(hash) { | ||
if(!hash.children) { | ||
return hash; | ||
} | ||
else if($.isTextNode(hash) || _.isString(hash.children)) { | ||
return $.coerceTextNode(hash); | ||
} | ||
else if(_.isArray(hash.children) && hash.children.length === 1) { | ||
return _.extend({}, hash, { children: $.coerceTextNode(hash.children) }); | ||
} | ||
else if(_.isArray(hash.children) && hash.children.length === 0) { | ||
return _.omit(hash, "children"); | ||
} | ||
else { | ||
return hash; | ||
} | ||
}, | ||
isTextNode: function isTextNode(vnode) { | ||
return _.isString(vnode.props); | ||
}, | ||
coerceTextNode: function coerceTextNode(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode.props; | ||
} | ||
else { | ||
return vnode; | ||
} | ||
}, | ||
_toStringWithTabs: function _toStringWithTabs(depth) { | ||
return function(vnode) { | ||
var children = $.getChildren(vnode); | ||
var s = []; | ||
var tabs = ""; | ||
for(var k = 0; k < depth; k++) { | ||
s.push("\t"); | ||
tabs += "\t"; | ||
} | ||
var tabs = s.join(""); | ||
if($.isTextNode(vnode)) { | ||
return tabs + vnode.props; | ||
} | ||
else if(_.isString(vnode)) { | ||
return tabs + vnode; | ||
} | ||
var children = $.getChildren(vnode); | ||
var begin = tabs + "<" + $.getTagName(vnode); | ||
@@ -94,2 +146,8 @@ var propsWithoutChildren = $.getPropsWithoutChildren(vnode); | ||
getProps: function getProps(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode; | ||
} | ||
else if(_.isString(vnode)) { | ||
return {}; | ||
} | ||
if(!vnode.props) { | ||
@@ -104,2 +162,8 @@ return {}; | ||
return function(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode.props; | ||
} | ||
else if(_.isString(vnode)) { | ||
return vnode; | ||
} | ||
return new vnode.constructor(_.extend({}, $.getProps(vnode), props)); | ||
@@ -112,9 +176,15 @@ }; | ||
getChildren: function getChildren(vnode) { | ||
if(!vnode.props.children) { | ||
if($.isTextNode(vnode)) { | ||
return []; | ||
} | ||
else if(_.isString(vnode)) { | ||
return []; | ||
} | ||
else if(!vnode.props.children) { | ||
return []; | ||
} | ||
else { | ||
var res = []; | ||
React.Children.forEach(vnode.props.children, function(child) { | ||
res.push(child); | ||
res.push($.coerceTextNode(child)); | ||
}); | ||
@@ -126,5 +196,8 @@ return res; | ||
var children = $.getChildren(vnode); | ||
return _.union(children, _.flatten(_.flatten(_.map(children, $.getDescendants), true))); | ||
return _.union(children, _.flatten(_.flatten(_.map(children, $.getDescendants), true), true)); | ||
}, | ||
getTree: function getTree(vnode) { | ||
if(_.isString(vnode)) { | ||
return []; | ||
} | ||
var tree = $.getDescendants(vnode); | ||
@@ -135,2 +208,8 @@ tree.unshift(vnode); | ||
getClassList: function getClassList(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return []; | ||
} | ||
else if(_.isString(vnode)) { | ||
return []; | ||
} | ||
if(!vnode.props.className) { | ||
@@ -150,2 +229,8 @@ return []; | ||
return function(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode.props; | ||
} | ||
else if(_.isString(vnode)) { | ||
return vnode; | ||
} | ||
var classList = $.getClassList(vnode); | ||
@@ -158,2 +243,8 @@ classList.push(className); | ||
return function(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode.props; | ||
} | ||
else if(_.isString(vnode)) { | ||
return vnode; | ||
} | ||
var classList = _.without($.getClassList(vnode), className); | ||
@@ -184,2 +275,5 @@ return $.setProps({ className: classList.join(" ") })(vnode); | ||
getTagName: function getTagName(vnode) { | ||
if($.isTextNode(vnode) || _.isString(vnode)) { | ||
return "Text"; | ||
} | ||
return vnode.type.displayName; | ||
@@ -189,2 +283,8 @@ }, | ||
return function(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode.props; | ||
} | ||
else if(_.isString(vnode)) { | ||
return vnode; | ||
} | ||
return new vnode.constructor(_.extend({}, $.getProps(vnode), { className: $.getClassList(vnode).join(" "), })); | ||
@@ -194,3 +294,12 @@ }; | ||
equals: function equals(other) { | ||
if($.isTextNode(other)) { | ||
other = other.props; | ||
} | ||
return function(vnode) { | ||
if($.isTextNode(vnode)) { | ||
vnode = vnode.props; | ||
} | ||
if(_.isString(other)) { | ||
return _.isEqual(vnode, other); | ||
} | ||
if($.getTagName(other) !== $.getTagName(vnode)) { | ||
@@ -224,13 +333,27 @@ return false; | ||
wrap: function wrap(wrapper) { | ||
wrapper = $.coerceTextNode(wrapper); | ||
return function(vnode) { | ||
var children = $.getChildren(wrapper); | ||
if($.isTextNode(vnode)) { | ||
vnode = vnode.props; | ||
} | ||
children.push(vnode); | ||
return new wrapper.constructor(_.extend({}, $.getProps(wrapper), { children: children })); | ||
return new wrapper.constructor(_.extend({}, $.getProps(wrapper), $.maybeChildren({ children: $.squeezeChildren(children) }))); | ||
}; | ||
}, | ||
append: function append(other) { | ||
other = $.coerceTextNode(other); | ||
return function(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode.props; | ||
} | ||
if(_.isString(vnode)) { | ||
return vnode; | ||
} | ||
var children = $.getChildren(vnode); | ||
children.push(other); | ||
return new vnode.constructor(_.extend({}, $.getProps(vnode), { children: children })); | ||
console.warn("append", children); | ||
console.warn("squeezed", $.squeezeChildren(children)); | ||
console.warn("maybe", $.maybeChildren(children)); | ||
return new vnode.constructor(_.extend({}, $.getProps(vnode), $.maybeChildren({ children: $.squeezeChildren(children) }))); | ||
}; | ||
@@ -240,2 +363,8 @@ }, | ||
return function(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return vnode.props; | ||
} | ||
if(_.isString(vnode)) { | ||
return vnode; | ||
} | ||
if($match.contains(vnode)) { | ||
@@ -257,5 +386,5 @@ var r; | ||
else { | ||
return new vnode.constructor(_.extend({}, $.getPropsWithoutChildren(vnode), { | ||
children: _.map($.getChildren(vnode), $.replace($match, replaceWithVNode)), | ||
})); | ||
return new vnode.constructor(_.extend({}, $.getPropsWithoutChildren(vnode), $.maybeChildren({ | ||
children: $.squeezeChildren(_.map($.getChildren(vnode), $.replace($match, replaceWithVNode))), | ||
}))); | ||
} | ||
@@ -301,2 +430,8 @@ }; | ||
return function(vnode) { | ||
if($.isTextNode(vnode)) { | ||
return false; | ||
} | ||
if(_.isString(vnode)) { | ||
return false; | ||
} | ||
if(rule.tagName && rule.tagName !== "*") { | ||
@@ -492,3 +627,3 @@ if($.getTagName(vnode) !== rule.tagName) { | ||
else { | ||
return "[" + this.map($.toString).join(",") + "]"; | ||
return "[" + this.map($.toString).join(", ") + "]"; | ||
} | ||
@@ -526,3 +661,3 @@ }, | ||
return $( | ||
new vnode.constructor(_.extend({}, vnode.props, { children: this.vnodes })) | ||
new vnode.constructor(_.extend({}, vnode.props, { children: $.squeezeChildren($(this).toChildren()) })) | ||
); | ||
@@ -533,2 +668,6 @@ }, | ||
}, | ||
text: function text(str) { | ||
assert(_.isString(str), "Should be called with a string."); | ||
return $(this.map($.append(str))); | ||
}, | ||
expose: function expose() { | ||
@@ -535,0 +674,0 @@ return this.toChildren(); |
{ | ||
"name": "react-query", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "React virtual DOM querying made easy.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -61,2 +61,5 @@ /** @jsx React.DOM */ | ||
}); | ||
console.warn("replaceFn", $replaceFn.toString()); | ||
console.warn("replaceFn", $replaceFn.toString()); | ||
var $li = $nested.replace({ ".inner": function() { return $(this).addClass("hello").props({ foo: "bar" }).text("hello world !!!"); }}); | ||
console.warn($li.toString()); |
@@ -61,2 +61,5 @@ /** @jsx React.DOM */ | ||
}); | ||
console.warn("replaceFn", $replaceFn.toString()); | ||
console.warn("replaceFn", $replaceFn.toString()); | ||
var $li = $nested.replace({ ".inner": function() { return $(this).addClass("hello").props({ foo: "bar" }).text("hello world !!!"); }}); | ||
console.warn($li.toString()); |
38396
786