Skip to content
Browse files

M1 refactoring, API updates.

  • Loading branch information...
1 parent b3069bf commit 2f023e41bd249925583bfd7664e35987877d748c @prujohn committed Aug 29, 2012
Showing with 29 additions and 12 deletions.
  1. +19 −2 lib/XmlCollection.dart
  2. +9 −9 lib/XmlElement.dart
  3. +1 −1 tests/queryTests.dart
View
21 lib/XmlCollection.dart
@@ -45,6 +45,23 @@ class XmlCollection<E extends XmlNode> implements Collection<E> {
*/
indexOf(E element) => _collection.indexOf(element);
+
+ /**
+ * Reduce a collection to a single value by iteratively combining each element
+ * of the collection with an existing value using the provided function.
+ * Use [initialValue] as the initial value, and the function [combine] to
+ * create a new value from the previous one and an element.
+ *
+ * Example of calculating the sum of a collection:
+ *
+ * collection.reduce(0, (prev, element) => prev + element);
+ */
+ Dynamic reduce(Dynamic initialValue,
+ Dynamic combine(Dynamic previousValue, E element))
+ => _collection.reduce(initialValue, combine);
+
+
+
/**
* Returns the element at the given [index] in the [XmlCollection] or throws
* an [IndexOutOfRangeException] if [index] is out of bounds.
@@ -102,7 +119,7 @@ class XmlCollection<E extends XmlNode> implements Collection<E> {
/**
* Returns the number of elements in this collection.
*/
- int get length() => _collection.length;
+ int get length => _collection.length;
/**
* Returns an [Iterator] that iterates over this [Iterable] object.
@@ -175,7 +192,7 @@ class XmlCollection<E extends XmlNode> implements Collection<E> {
.filter((el) => el is XmlElement)
.forEach((el){
if (!list.isEmpty()) return;
- el._queryAttributeInternal(aMap, list);
+ (el as XmlElement)._queryAttributeInternal(aMap, list);
});
}
}
View
18 lib/XmlElement.dart
@@ -40,7 +40,7 @@ class XmlElement extends XmlNode {
/**
* Gets a [String] of any text within this [XmlElement].
*/
- String get text() {
+ String get text {
var tNodes = _children.filter((el) => el is XmlText);
if (tNodes.isEmpty()) return '';
@@ -53,29 +53,29 @@ class XmlElement extends XmlNode {
* Gets a map of name/uri namespace pairs associated with
* this [XmlElement].
*/
- Map<String, String> get namespaces() => _namespaces;
+ Map<String, String> get namespaces => _namespaces;
/**
* Gets a map of name/value attributue pairs associated with
* this [XmlElement].
*/
- Map<String, String> get attributes() => _attributes;
+ Map<String, String> get attributes => _attributes;
/**
* Gets a collection of children under this [XmlElement].
*/
- Collection<XmlNode> get children() => _children;
+ Collection<XmlNode> get children => _children;
/**
* Gets a collection of siblings related to this [XmlElement].
*/
- Collection<XmlNode> get siblings() => parent.children;
+ Collection<XmlNode> get siblings => parent.children;
/**
* Gets a collection of [XmlNamespace]s that are in scope to this
* [XmlElement].
*/
- Collection<XmlNamespace> get namespacesInScope() {
+ Collection<XmlNamespace> get namespacesInScope {
List<XmlNamespace> l = [];
_namespaces.forEach((nname, uri){
@@ -99,7 +99,7 @@ class XmlElement extends XmlNode {
/**
* Gets the previous sibling to the this [XmlElement], or null if none exists.
*/
- XmlNode get previousSibling() {
+ XmlNode get previousSibling {
var i = parent._children.indexOf(this);
if (i <= 0) return null;
@@ -111,7 +111,7 @@ class XmlElement extends XmlNode {
* Gets the next sibling adjacent to this [XmlElement], or null if none
* exists.
*/
- XmlNode get nextSibling() {
+ XmlNode get nextSibling {
if (parent._children.last() == this) return null;
var i = parent._children.indexOf(this);
@@ -122,7 +122,7 @@ class XmlElement extends XmlNode {
/**
* Gets a boolean indicating of this [XmlElement] has any child elements.
*/
- bool get hasChildren() => !_children.isEmpty();
+ bool get hasChildren => !_children.isEmpty();
/**
* Adds a child [XmlNode] to this [XmlElement].
View
2 tests/queryTests.dart
@@ -66,7 +66,7 @@ queryTests(){
var result = books
.queryAll('book')
.filter((e) =>
- e.query('author')[0].dynamic.text == 'Stefan Handsomly');
+ (e as XmlElement).query('author')[0].dynamic.text == 'Stefan Handsomly');
Expect.isTrue(result is XmlCollection);
Expect.equals(2, result.length);

0 comments on commit 2f023e4

Please sign in to comment.
Something went wrong with that request. Please try again.