more types in fascades

This commit is contained in:
Kevin Moore 2015-01-29 09:50:49 -08:00 committed by Alex Eagle
parent 05ffdc9b44
commit f014b53a4c
4 changed files with 166 additions and 191 deletions

View File

@ -4,44 +4,36 @@ import 'dart:async';
export 'dart:async' show Future; export 'dart:async' show Future;
class PromiseWrapper { class PromiseWrapper {
static Future resolve(obj) { static Future resolve(obj) => new Future.value(obj);
return new Future.value(obj);
}
static Future reject(obj) { static Future reject(obj) => new Future.error(obj);
return new Future.error(obj);
}
static Future<List> all(List<Future> promises) { static Future<List> all(List<Future> promises) => Future.wait(promises);
return 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); if (success == null) return promise.catchError(onError);
return promise.then(success, onError: onError); return promise.then(success, onError: onError);
} }
static completer(){ static _Completer completer() => new _Completer(new Completer());
return new _Completer(new Completer());
}
static setTimeout(Function fn, int millis) { static void setTimeout(fn(), int millis) {
new Timer(new Duration(milliseconds: millis), fn); new Timer(new Duration(milliseconds: millis), fn);
} }
} }
class _Completer { class _Completer {
Completer c; final Completer c;
_Completer(this.c); _Completer(this.c);
get promise => c.future; Future get promise => c.future;
complete(v) { void complete(v) {
c.complete(v); c.complete(v);
} }
reject(v) { void reject(v) {
c.completeError(v); c.completeError(v);
} }
} }

View File

@ -5,107 +5,119 @@ export 'dart:core' show Map, List, Set;
import 'dart:math' show max, min; import 'dart:math' show max, min;
class MapIterator extends Iterator<List> { class MapIterator extends Iterator<List> {
Iterator _iterator; final Iterator _iterator;
Map _map; 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 { List get current {
return this._iterator.current != null ? return _iterator.current != null
[this._iterator.current, this._map[this._iterator.current]] : ? [_iterator.current, _map[_iterator.current]]
null; : null;
} }
} }
class IterableMap extends IterableBase<List> { class IterableMap extends IterableBase<List> {
Map _map; final Map _map;
IterableMap(Map map) { IterableMap(Map map) : _map = map;
this._map = map;
} Iterator<List> get iterator => new MapIterator(_map);
Iterator<List> get iterator => new MapIterator(this._map);
} }
class MapWrapper { class MapWrapper {
static HashMap create() => new HashMap(); static HashMap create() => new HashMap();
static HashMap clone(Map m) => new HashMap.from(m); static HashMap clone(Map m) => new HashMap.from(m);
static HashMap createFromStringMap(m) => m; static HashMap createFromStringMap(HashMap m) => m;
static HashMap createFromPairs(List pairs) { static HashMap createFromPairs(List pairs) => pairs.fold({}, (m, p) {
return pairs.fold({}, (m, p){ m[p[0]] = p[1];
m[p[0]] = p[1]; return m;
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 contains(Map m, k) => m.containsKey(k);
static void set(m, k, v){ m[k] = v; } static forEach(Map m, fn(v, k)) {
static contains(m, k) => m.containsKey(k); m.forEach((k, v) => fn(v, k));
static forEach(m, fn) {
m.forEach((k,v) => fn(v,k));
} }
static int size(m) {return m.length;} static int size(Map m) => m.length;
static void delete(m, k) { m.remove(k); } static void delete(Map m, k) {
static void clear(m) { m.clear(); } m.remove(k);
static Iterable iterable(m) { return new IterableMap(m); } }
static Iterable keys(m) { return m.keys; } static void clear(Map m) {
static Iterable values(m) { return m.values; } 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? // TODO: how to export StringMap=Map as a type?
class StringMapWrapper { class StringMapWrapper {
static HashMap create() => new HashMap(); static HashMap create() => new HashMap();
static get(map, key) { static get(Map map, key) => map[key];
return map[key]; static void set(Map map, key, value) {
}
static set(map, key, value) {
map[key] = value; map[key] = value;
} }
static forEach(m, fn) { static void forEach(Map m, fn(v, k)) {
m.forEach((k,v) => fn(v,k)); m.forEach((k, v) => fn(v, k));
}
static isEmpty(m) {
return m.isEmpty;
} }
static bool isEmpty(Map m) => m.isEmpty;
} }
class ListWrapper { class ListWrapper {
static List clone(List l) => new List.from(l); static List clone(List l) => new List.from(l);
static List create() => new List(); static List create() => new List();
static List createFixedSize(int size) => new List(size); static List createFixedSize(int size) => new List(size);
static get(m, k) => m[k]; static get(List m, int k) => m[k];
static void set(m, k, v) { m[k] = v; } static void set(List m, int k, v) {
static contains(List m, k) => m.contains(k); m[k] = v;
static map(list, fn) => list.map(fn).toList(); }
static filter(List list, fn) => list.where(fn).toList(); static bool contains(List m, k) => m.contains(k);
static find(List list, fn) => list.firstWhere(fn, orElse:() => null); static List map(list, fn(item)) => list.map(fn).toList();
static any(List list, fn) => list.any(fn); static List filter(List list, bool fn(item)) => list.where(fn).toList();
static forEach(list, Function fn) { 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); list.forEach(fn);
} }
static reduce(List list, Function fn, init) { static reduce(List list, fn(a, b), init) {
return list.fold(init, fn); return list.fold(init, fn);
} }
static first(List list) => list.isEmpty ? null : list.first; static first(List list) => list.isEmpty ? null : list.first;
static last(List list) => list.isEmpty ? null : list.last; static last(List list) => list.isEmpty ? null : list.last;
static List reversed(List list) => list.reversed.toList(); static List reversed(List list) => list.reversed.toList();
static void push(List l, e) { l.add(e); } static void push(List l, e) {
static List concat(List a, List b) {a.addAll(b); return a;} l.add(e);
}
static List concat(List a, List b) {
a.addAll(b);
return a;
}
static bool isList(l) => l is List; static bool isList(l) => l is List;
static void insert(List l, int index, value) { l.insert(index, value); } static void insert(List l, int index, value) {
static void removeAt(List l, int index) { l.removeAt(index); } l.insert(index, value);
}
static void removeAt(List l, int index) {
l.removeAt(index);
}
static void removeAll(List list, List items) { static void removeAll(List list, List items) {
for (var i = 0; i < items.length; ++i) { for (var i = 0; i < items.length; ++i) {
list.remove(items[i]); list.remove(items[i]);
} }
} }
static bool remove(List list, item) => list.remove(item); 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 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]) { static void fill(List l, value, [int start = 0, int end]) {
// JS semantics // JS semantics
// see https://github.com/google/traceur-compiler/blob/81880cd3f17bac7de90a4cd0339e9f1a9f61d24c/src/runtime/polyfills/Array.js#L94 // 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); l.fillRange(start, end, value);
} }
static bool equals(List a, List b){ static bool equals(List a, List b) {
if(a.length != b.length) return false; if (a.length != b.length) return false;
for (var i = 0; i < a.length; ++i) { for (var i = 0; i < a.length; ++i) {
if (a[i] != b[i]) return false; if (a[i] != b[i]) return false;
} }
@ -129,14 +141,14 @@ class ListWrapper {
bool isListLikeIterable(obj) => obj is Iterable; bool isListLikeIterable(obj) => obj is Iterable;
void iterateListLike(iter, Function fn) { void iterateListLike(iter, fn(item)) {
assert(iter is Iterable); assert(iter is Iterable);
for (var item in iter) { for (var item in iter) {
fn (item); fn(item);
} }
} }
class SetWrapper { class SetWrapper {
static Set createFromList(List l) { return new Set.from(l); } static Set createFromList(List l) => new Set.from(l);
static bool has(Set s, key) { return s.contains(key); } static bool has(Set s, key) => s.contains(key);
} }

View File

@ -11,10 +11,10 @@ class IdentitySanitizer implements NodeTreeSanitizer {
void sanitizeTree(Node node) {} void sanitizeTree(Node node) {}
} }
var _window = context['window']; final _window = context['window'];
var _gc = context['gc']; final _gc = context['gc'];
gc() { void gc() {
if (_gc != null) { if (_gc != null) {
_gc.apply(const []); _gc.apply(const []);
} }
@ -23,129 +23,101 @@ gc() {
final identitySanitizer = new IdentitySanitizer(); final identitySanitizer = new IdentitySanitizer();
class DOM { class DOM {
static query(selector) { static query(String selector) => document.querySelector(selector);
return document.querySelector(selector);
} static Element querySelector(el, String selector) =>
static Element querySelector(el, String selector) { el.querySelector(selector);
return el.querySelector(selector);
} static ElementList querySelectorAll(el, String selector) =>
static ElementList querySelectorAll(el, String selector) { el.querySelectorAll(selector);
return el.querySelectorAll(selector);
} static void on(EventTarget element, String event, callback(arg)) {
static on(element, event, callback) {
// due to https://code.google.com/p/dart/issues/detail?id=17406 // due to https://code.google.com/p/dart/issues/detail?id=17406
// addEventListener misses zones so we use element.on. // addEventListener misses zones so we use element.on.
element.on[event].listen(callback); element.on[event].listen(callback);
} }
static dispatchEvent(el, evt) { static void dispatchEvent(EventTarget el, Event evt) {
el.dispatchEvent(evt); el.dispatchEvent(evt);
} }
static createMouseEvent(eventType) { static MouseEvent createMouseEvent(String eventType) =>
return new MouseEvent(eventType, canBubble: true); new MouseEvent(eventType, canBubble: true);
} static String getInnerHTML(Element el) => el.innerHtml;
static getInnerHTML(el) { static String getOuterHTML(Element el) => el.outerHtml;
return el.innerHtml; static void setInnerHTML(Element el, String value) {
}
static getOuterHTML(el) {
return el.outerHtml;
}
static setInnerHTML(el, value) {
el.innerHtml = value; el.innerHtml = value;
} }
static Node firstChild(el) { static Node firstChild(el) => el.firstChild;
return el.firstChild; static Node nextSibling(Node el) => el.nextNode;
static Element parentElement(Node el) => el.parent;
static List<Node> 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) { static void appendChild(Node el, Node node) {
return el.nextNode;
}
static Element parentElement(el) {
return el.parent;
}
static List<Node> childNodes(el) {
return el.childNodes;
}
static childNodesAsList(el) {
return childNodes(el).toList();
}
static clearNodes(el) {
el.nodes = [];
}
static appendChild(el, node) {
el.append(node); el.append(node);
} }
static removeChild(el, node) { static void removeChild(Element el, Node node) {
node.remove(); node.remove();
} }
static insertBefore(el, node) { static void insertBefore(Node el, Node node) {
el.parentNode.insertBefore(node, el); el.parentNode.insertBefore(node, el);
} }
static insertAllBefore(el, nodes) { static void insertAllBefore(Node el, Iterable<Node> nodes) {
el.parentNode.insertAllBefore(nodes, el); el.parentNode.insertAllBefore(nodes, el);
} }
static insertAfter(el, node) { static void insertAfter(Node el, Node node) {
el.parentNode.insertBefore(node, el.nextNode); el.parentNode.insertBefore(node, el.nextNode);
} }
static getText(Element el) { static String getText(Node el) => el.text;
return el.text; static void setText(Text text, String value) {
}
static setText(Text text, String value) {
text.text = value; text.text = value;
} }
static createTemplate(html) { static TemplateElement createTemplate(String html) {
var t = new TemplateElement(); var t = new TemplateElement();
t.setInnerHtml(html, treeSanitizer:identitySanitizer); t.setInnerHtml(html, treeSanitizer: identitySanitizer);
return t; return t;
} }
static createElement(tagName, [doc=null]) { static Element createElement(String tagName, [HtmlDocument doc = null]) {
if (doc == null) doc = document; if (doc == null) doc = document;
return doc.createElement(tagName); 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; if (doc == null) doc = document;
var el = doc.createElement("SCRIPT"); var el = doc.createElement("SCRIPT");
el.setAttribute(attrName, attrValue); el.setAttribute(attrName, attrValue);
return el; return el;
} }
static clone(Node node) { static clone(Node node) => node.clone(true);
return node.clone(true); static bool hasProperty(Element element, String name) =>
} new JsObject.fromBrowserObject(element).hasProperty(name);
static hasProperty(Element element, String name) { static List<Node> getElementsByClassName(Element element, String name) =>
return new JsObject.fromBrowserObject(element).hasProperty(name); element.getElementsByClassName(name);
} static List<Node> getElementsByTagName(Element element, String name) =>
static getElementsByClassName(Element element, String name) { element.querySelectorAll(name);
return element.getElementsByClassName(name); static List<String> classList(Element element) => element.classes.toList();
} static void addClass(Element element, String classname) {
static getElementsByTagName(Element element, String name) {
return element.querySelectorAll(name);
}
static List classList(Element element) {
return element.classes.toList();
}
static addClass(Element element, classname) {
element.classes.add(classname); element.classes.add(classname);
} }
static hasClass(Element element, classname) { static bool hasClass(Element element, String classname) =>
return element.classes.contains(classname); element.classes.contains(classname);
}
static String tagName(Element element) { static String tagName(Element element) => element.tagName;
return element.tagName;
} static Map<String, String> attributeMap(Element element) =>
static attributeMap(Element element) { element.attributes;
return element.attributes;
} static String getAttribute(Element element, String attribute) =>
static getAttribute(Element element, String attribute) { element.getAttribute(attribute);
return element.getAttribute(attribute);
} static Node templateAwareRoot(Element el) =>
static Node templateAwareRoot(Element el) { el is TemplateElement ? el.content : el;
return el is TemplateElement ? el.content : el;
} static HtmlDocument createHtmlDocument() =>
static HtmlDocument createHtmlDocument() { document.implementation.createHtmlDocument('fakeTitle');
return document.implementation.createHtmlDocument('fakeTitle');
} static HtmlDocument defaultDoc() => document;
static HtmlDocument defaultDoc() { static bool elementMatches(n, String selector) =>
return document; n is Element && n.matches(selector);
}
static bool elementMatches(n, String selector) {
return n is Element && n.matches(selector);
}
} }

View File

@ -36,18 +36,18 @@ class StringWrapper {
return new String.fromCharCode(code); return new String.fromCharCode(code);
} }
static charCodeAt(String s, int index) { static int charCodeAt(String s, int index) {
return s.codeUnitAt(index); return s.codeUnitAt(index);
} }
static split(String s, RegExp regExp) { static List<String> split(String s, RegExp regExp) {
var parts = []; var parts = <String>[];
var lastEnd = 0; var lastEnd = 0;
regExp.allMatches(s).forEach((match) { regExp.allMatches(s).forEach((match) {
parts.add(s.substring(lastEnd, match.start)); parts.add(s.substring(lastEnd, match.start));
lastEnd = match.end; lastEnd = match.end;
for (var i=0; i<match.groupCount; i++) { for (var i = 0; i < match.groupCount; i++) {
parts.add(match.group(i+1)); parts.add(match.group(i + 1));
} }
}); });
parts.add(s.substring(lastEnd)); parts.add(s.substring(lastEnd));
@ -64,7 +64,7 @@ class StringWrapper {
} }
class StringJoiner { class StringJoiner {
List<String> _parts = <String>[]; final List<String> _parts = <String>[];
void add(String part) { void add(String part) {
_parts.add(part); _parts.add(part);
@ -73,7 +73,6 @@ class StringJoiner {
String toString() => _parts.join(""); String toString() => _parts.join("");
} }
class NumberWrapper { class NumberWrapper {
static int parseIntAutoRadix(String text) { static int parseIntAutoRadix(String text) {
return int.parse(text); return int.parse(text);
@ -87,7 +86,7 @@ class NumberWrapper {
return double.parse(text); return double.parse(text);
} }
static get NaN => double.NAN; static double get NaN => double.NAN;
static bool isNaN(num value) => value.isNaN; static bool isNaN(num value) => value.isNaN;
@ -95,19 +94,19 @@ class NumberWrapper {
} }
class RegExpWrapper { class RegExpWrapper {
static RegExp create(regExpStr) { static RegExp create(String regExpStr) {
return new RegExp(regExpStr); return new RegExp(regExpStr);
} }
static firstMatch(regExp, input) { static Match firstMatch(RegExp regExp, String input) {
return regExp.firstMatch(input); return regExp.firstMatch(input);
} }
static matcher(regExp, input) { static Iterator<Match> matcher(RegExp regExp, String input) {
return regExp.allMatches(input).iterator; return regExp.allMatches(input).iterator;
} }
} }
class RegExpMatcherWrapper { class RegExpMatcherWrapper {
static next(matcher) { static Match next(Iterator<Match> matcher) {
if (matcher.moveNext()) { if (matcher.moveNext()) {
return matcher.current; return matcher.current;
} }
@ -134,7 +133,8 @@ class BaseException extends Error {
const _NAN_KEY = const Object(); const _NAN_KEY = const Object();
// Dart can have identical(str1, str2) == false while str1 == str2 // 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 // Dart compare map keys by equality and we can have NaN != NaN
dynamic getMapKey(value) { dynamic getMapKey(value) {
@ -150,7 +150,6 @@ bool isJsObject(o) {
return false; return false;
} }
bool assertionsEnabled() { bool assertionsEnabled() {
try { try {
assert(false); assert(false);