From f014b53a4c86bd7a084431af5c7b452f997826b2 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 29 Jan 2015 09:50:49 -0800 Subject: [PATCH] more types in fascades --- modules/facade/src/async.dart | 28 ++--- modules/facade/src/collection.dart | 140 +++++++++++++------------ modules/facade/src/dom.dart | 160 ++++++++++++----------------- modules/facade/src/lang.dart | 29 +++--- 4 files changed, 166 insertions(+), 191 deletions(-) diff --git a/modules/facade/src/async.dart b/modules/facade/src/async.dart index 166edc3ff6..7d72fcedcc 100644 --- a/modules/facade/src/async.dart +++ b/modules/facade/src/async.dart @@ -4,44 +4,36 @@ import 'dart:async'; export 'dart:async' show Future; class PromiseWrapper { - static Future resolve(obj) { - return new Future.value(obj); - } + static Future resolve(obj) => new Future.value(obj); - static Future reject(obj) { - return new Future.error(obj); - } + static Future reject(obj) => new Future.error(obj); - static Future all(List promises) { - return Future.wait(promises); - } + static Future all(List promises) => Future.wait(promises); - static Future then(Future promise, Function success, Function onError) { + static Future then(Future promise, success(value), Function onError) { if (success == null) return promise.catchError(onError); return promise.then(success, onError: onError); } - static completer(){ - return new _Completer(new Completer()); - } + static _Completer completer() => new _Completer(new Completer()); - static setTimeout(Function fn, int millis) { + static void setTimeout(fn(), int millis) { new Timer(new Duration(milliseconds: millis), fn); } } class _Completer { - Completer c; + final Completer c; _Completer(this.c); - get promise => c.future; + Future get promise => c.future; - complete(v) { + void complete(v) { c.complete(v); } - reject(v) { + void reject(v) { c.completeError(v); } } diff --git a/modules/facade/src/collection.dart b/modules/facade/src/collection.dart index ef79726791..338b1ab0ba 100644 --- a/modules/facade/src/collection.dart +++ b/modules/facade/src/collection.dart @@ -5,107 +5,119 @@ export 'dart:core' show Map, List, Set; import 'dart:math' show max, min; class MapIterator extends Iterator { - Iterator _iterator; - Map _map; + final Iterator _iterator; + final Map _map; + + MapIterator(Map map) + : _map = map, + _iterator = map.keys.iterator; + + bool moveNext() => _iterator.moveNext(); - MapIterator(Map map) { - this._map = map; - this._iterator = map.keys.iterator; - } - bool moveNext() { - return this._iterator.moveNext(); - } List get current { - return this._iterator.current != null ? - [this._iterator.current, this._map[this._iterator.current]] : - null; + return _iterator.current != null + ? [_iterator.current, _map[_iterator.current]] + : null; } } class IterableMap extends IterableBase { - Map _map; + final Map _map; - IterableMap(Map map) { - this._map = map; - } - Iterator get iterator => new MapIterator(this._map); + IterableMap(Map map) : _map = map; + + Iterator get iterator => new MapIterator(_map); } class MapWrapper { static HashMap create() => new HashMap(); static HashMap clone(Map m) => new HashMap.from(m); - static HashMap createFromStringMap(m) => m; - static HashMap createFromPairs(List pairs) { - return pairs.fold({}, (m, p){ - m[p[0]] = p[1]; - return m; - }); + static HashMap createFromStringMap(HashMap m) => m; + static HashMap createFromPairs(List pairs) => pairs.fold({}, (m, p) { + m[p[0]] = p[1]; + return m; + }); + static get(Map m, k) => m[k]; + static void set(Map m, k, v) { + m[k] = v; } - static get(m, k) => m[k]; - static void set(m, k, v){ m[k] = v; } - static contains(m, k) => m.containsKey(k); - static forEach(m, fn) { - m.forEach((k,v) => fn(v,k)); + static contains(Map m, k) => m.containsKey(k); + static forEach(Map m, fn(v, k)) { + m.forEach((k, v) => fn(v, k)); } - static int size(m) {return m.length;} - static void delete(m, k) { m.remove(k); } - static void clear(m) { m.clear(); } - static Iterable iterable(m) { return new IterableMap(m); } - static Iterable keys(m) { return m.keys; } - static Iterable values(m) { return m.values; } + static int size(Map m) => m.length; + static void delete(Map m, k) { + m.remove(k); + } + static void clear(Map m) { + m.clear(); + } + static Iterable iterable(Map m) => new IterableMap(m); + static Iterable keys(Map m) => m.keys; + static Iterable values(Map m) => m.values; } // TODO: how to export StringMap=Map as a type? class StringMapWrapper { static HashMap create() => new HashMap(); - static get(map, key) { - return map[key]; - } - static set(map, key, value) { + static get(Map map, key) => map[key]; + static void set(Map map, key, value) { map[key] = value; } - static forEach(m, fn) { - m.forEach((k,v) => fn(v,k)); - } - static isEmpty(m) { - return m.isEmpty; + static void forEach(Map m, fn(v, k)) { + m.forEach((k, v) => fn(v, k)); } + static bool isEmpty(Map m) => m.isEmpty; } class ListWrapper { static List clone(List l) => new List.from(l); static List create() => new List(); static List createFixedSize(int size) => new List(size); - static get(m, k) => m[k]; - static void set(m, k, v) { m[k] = v; } - static contains(List m, k) => m.contains(k); - static map(list, fn) => list.map(fn).toList(); - static filter(List list, fn) => list.where(fn).toList(); - static find(List list, fn) => list.firstWhere(fn, orElse:() => null); - static any(List list, fn) => list.any(fn); - static forEach(list, Function fn) { + static get(List m, int k) => m[k]; + static void set(List m, int k, v) { + m[k] = v; + } + static bool contains(List m, k) => m.contains(k); + static List map(list, fn(item)) => list.map(fn).toList(); + static List filter(List list, bool fn(item)) => list.where(fn).toList(); + static find(List list, bool fn(item)) => + list.firstWhere(fn, orElse: () => null); + static bool any(List list, bool fn(item)) => list.any(fn); + static void forEach(Iterable list, fn(item)) { list.forEach(fn); } - static reduce(List list, Function fn, init) { + static reduce(List list, fn(a, b), init) { return list.fold(init, fn); } static first(List list) => list.isEmpty ? null : list.first; static last(List list) => list.isEmpty ? null : list.last; static List reversed(List list) => list.reversed.toList(); - static void push(List l, e) { l.add(e); } - static List concat(List a, List b) {a.addAll(b); return a;} + static void push(List l, e) { + l.add(e); + } + static List concat(List a, List b) { + a.addAll(b); + return a; + } static bool isList(l) => l is List; - static void insert(List l, int index, value) { l.insert(index, value); } - static void removeAt(List l, int index) { l.removeAt(index); } + static void insert(List l, int index, value) { + l.insert(index, value); + } + static void removeAt(List l, int index) { + l.removeAt(index); + } static void removeAll(List list, List items) { for (var i = 0; i < items.length; ++i) { list.remove(items[i]); } } static bool remove(List list, item) => list.remove(item); - static void clear(List l) { l.clear(); } + static void clear(List l) { + l.clear(); + } static String join(List l, String s) => l.join(s); - static bool isEmpty(list) => list.isEmpty; + static bool isEmpty(Iterable list) => list.isEmpty; static void fill(List l, value, [int start = 0, int end]) { // JS semantics // see https://github.com/google/traceur-compiler/blob/81880cd3f17bac7de90a4cd0339e9f1a9f61d24c/src/runtime/polyfills/Array.js#L94 @@ -118,8 +130,8 @@ class ListWrapper { } l.fillRange(start, end, value); } - static bool equals(List a, List b){ - if(a.length != b.length) return false; + static bool equals(List a, List b) { + if (a.length != b.length) return false; for (var i = 0; i < a.length; ++i) { if (a[i] != b[i]) return false; } @@ -129,14 +141,14 @@ class ListWrapper { bool isListLikeIterable(obj) => obj is Iterable; -void iterateListLike(iter, Function fn) { +void iterateListLike(iter, fn(item)) { assert(iter is Iterable); for (var item in iter) { - fn (item); + fn(item); } } class SetWrapper { - static Set createFromList(List l) { return new Set.from(l); } - static bool has(Set s, key) { return s.contains(key); } + static Set createFromList(List l) => new Set.from(l); + static bool has(Set s, key) => s.contains(key); } diff --git a/modules/facade/src/dom.dart b/modules/facade/src/dom.dart index afcf329a58..4dd1c03644 100644 --- a/modules/facade/src/dom.dart +++ b/modules/facade/src/dom.dart @@ -11,10 +11,10 @@ class IdentitySanitizer implements NodeTreeSanitizer { void sanitizeTree(Node node) {} } -var _window = context['window']; -var _gc = context['gc']; +final _window = context['window']; +final _gc = context['gc']; -gc() { +void gc() { if (_gc != null) { _gc.apply(const []); } @@ -23,129 +23,101 @@ gc() { final identitySanitizer = new IdentitySanitizer(); class DOM { - static query(selector) { - return document.querySelector(selector); - } - static Element querySelector(el, String selector) { - return el.querySelector(selector); - } - static ElementList querySelectorAll(el, String selector) { - return el.querySelectorAll(selector); - } - static on(element, event, callback) { + static query(String selector) => document.querySelector(selector); + + static Element querySelector(el, String selector) => + el.querySelector(selector); + + static ElementList querySelectorAll(el, String selector) => + el.querySelectorAll(selector); + + static void on(EventTarget element, String event, callback(arg)) { // due to https://code.google.com/p/dart/issues/detail?id=17406 // addEventListener misses zones so we use element.on. element.on[event].listen(callback); } - static dispatchEvent(el, evt) { + static void dispatchEvent(EventTarget el, Event evt) { el.dispatchEvent(evt); } - static createMouseEvent(eventType) { - return new MouseEvent(eventType, canBubble: true); - } - static getInnerHTML(el) { - return el.innerHtml; - } - static getOuterHTML(el) { - return el.outerHtml; - } - static setInnerHTML(el, value) { + static MouseEvent createMouseEvent(String eventType) => + new MouseEvent(eventType, canBubble: true); + static String getInnerHTML(Element el) => el.innerHtml; + static String getOuterHTML(Element el) => el.outerHtml; + static void setInnerHTML(Element el, String value) { el.innerHtml = value; } - static Node firstChild(el) { - return el.firstChild; + static Node firstChild(el) => el.firstChild; + static Node nextSibling(Node el) => el.nextNode; + static Element parentElement(Node el) => el.parent; + static List childNodes(Node el) => el.childNodes; + static List childNodesAsList(Node el) => childNodes(el).toList(); + static void clearNodes(Node el) { + el.nodes = const []; } - static Node nextSibling(el) { - return el.nextNode; - } - static Element parentElement(el) { - return el.parent; - } - static List childNodes(el) { - return el.childNodes; - } - static childNodesAsList(el) { - return childNodes(el).toList(); - } - static clearNodes(el) { - el.nodes = []; - } - static appendChild(el, node) { + static void appendChild(Node el, Node node) { el.append(node); } - static removeChild(el, node) { + static void removeChild(Element el, Node node) { node.remove(); } - static insertBefore(el, node) { + static void insertBefore(Node el, Node node) { el.parentNode.insertBefore(node, el); } - static insertAllBefore(el, nodes) { + static void insertAllBefore(Node el, Iterable nodes) { el.parentNode.insertAllBefore(nodes, el); } - static insertAfter(el, node) { + static void insertAfter(Node el, Node node) { el.parentNode.insertBefore(node, el.nextNode); } - static getText(Element el) { - return el.text; - } - static setText(Text text, String value) { + static String getText(Node el) => el.text; + static void setText(Text text, String value) { text.text = value; } - static createTemplate(html) { + static TemplateElement createTemplate(String html) { var t = new TemplateElement(); - t.setInnerHtml(html, treeSanitizer:identitySanitizer); + t.setInnerHtml(html, treeSanitizer: identitySanitizer); return t; } - static createElement(tagName, [doc=null]) { + static Element createElement(String tagName, [HtmlDocument doc = null]) { if (doc == null) doc = document; return doc.createElement(tagName); } - static createScriptTag(String attrName, String attrValue, [doc=null]) { + static createScriptTag(String attrName, String attrValue, + [HtmlDocument doc = null]) { if (doc == null) doc = document; var el = doc.createElement("SCRIPT"); el.setAttribute(attrName, attrValue); return el; } - static clone(Node node) { - return node.clone(true); - } - static hasProperty(Element element, String name) { - return new JsObject.fromBrowserObject(element).hasProperty(name); - } - static getElementsByClassName(Element element, String name) { - return element.getElementsByClassName(name); - } - static getElementsByTagName(Element element, String name) { - return element.querySelectorAll(name); - } - static List classList(Element element) { - return element.classes.toList(); - } - static addClass(Element element, classname) { + static clone(Node node) => node.clone(true); + static bool hasProperty(Element element, String name) => + new JsObject.fromBrowserObject(element).hasProperty(name); + static List getElementsByClassName(Element element, String name) => + element.getElementsByClassName(name); + static List getElementsByTagName(Element element, String name) => + element.querySelectorAll(name); + static List classList(Element element) => element.classes.toList(); + static void addClass(Element element, String classname) { element.classes.add(classname); } - static hasClass(Element element, classname) { - return element.classes.contains(classname); - } - static String tagName(Element element) { - return element.tagName; - } - static attributeMap(Element element) { - return element.attributes; - } - static getAttribute(Element element, String attribute) { - return element.getAttribute(attribute); - } - static Node templateAwareRoot(Element el) { - return el is TemplateElement ? el.content : el; - } - static HtmlDocument createHtmlDocument() { - return document.implementation.createHtmlDocument('fakeTitle'); - } - static HtmlDocument defaultDoc() { - return document; - } - static bool elementMatches(n, String selector) { - return n is Element && n.matches(selector); - } + static bool hasClass(Element element, String classname) => + element.classes.contains(classname); + + static String tagName(Element element) => element.tagName; + + static Map attributeMap(Element element) => + element.attributes; + + static String getAttribute(Element element, String attribute) => + element.getAttribute(attribute); + + static Node templateAwareRoot(Element el) => + el is TemplateElement ? el.content : el; + + static HtmlDocument createHtmlDocument() => + document.implementation.createHtmlDocument('fakeTitle'); + + static HtmlDocument defaultDoc() => document; + static bool elementMatches(n, String selector) => + n is Element && n.matches(selector); } diff --git a/modules/facade/src/lang.dart b/modules/facade/src/lang.dart index 8e524eb05a..16055923eb 100644 --- a/modules/facade/src/lang.dart +++ b/modules/facade/src/lang.dart @@ -36,18 +36,18 @@ class StringWrapper { return new String.fromCharCode(code); } - static charCodeAt(String s, int index) { + static int charCodeAt(String s, int index) { return s.codeUnitAt(index); } - static split(String s, RegExp regExp) { - var parts = []; + static List split(String s, RegExp regExp) { + var parts = []; var lastEnd = 0; regExp.allMatches(s).forEach((match) { parts.add(s.substring(lastEnd, match.start)); lastEnd = match.end; - for (var i=0; i _parts = []; + final List _parts = []; void add(String part) { _parts.add(part); @@ -73,7 +73,6 @@ class StringJoiner { String toString() => _parts.join(""); } - class NumberWrapper { static int parseIntAutoRadix(String text) { return int.parse(text); @@ -87,7 +86,7 @@ class NumberWrapper { return double.parse(text); } - static get NaN => double.NAN; + static double get NaN => double.NAN; static bool isNaN(num value) => value.isNaN; @@ -95,19 +94,19 @@ class NumberWrapper { } class RegExpWrapper { - static RegExp create(regExpStr) { + static RegExp create(String regExpStr) { return new RegExp(regExpStr); } - static firstMatch(regExp, input) { + static Match firstMatch(RegExp regExp, String input) { return regExp.firstMatch(input); } - static matcher(regExp, input) { + static Iterator matcher(RegExp regExp, String input) { return regExp.allMatches(input).iterator; } } class RegExpMatcherWrapper { - static next(matcher) { + static Match next(Iterator matcher) { if (matcher.moveNext()) { return matcher.current; } @@ -134,7 +133,8 @@ class BaseException extends Error { const _NAN_KEY = const Object(); // Dart can have identical(str1, str2) == false while str1 == str2 -bool looseIdentical(a, b) => a is String && b is String ? a == b : identical(a, b); +bool looseIdentical(a, b) => + a is String && b is String ? a == b : identical(a, b); // Dart compare map keys by equality and we can have NaN != NaN dynamic getMapKey(value) { @@ -150,7 +150,6 @@ bool isJsObject(o) { return false; } - bool assertionsEnabled() { try { assert(false); @@ -158,4 +157,4 @@ bool assertionsEnabled() { } catch (e) { return true; } -} \ No newline at end of file +}