feat(Change Detection): Implement collection changes

This commit is contained in:
Victor Berchet
2014-10-28 18:56:15 +01:00
committed by Rado Kirov
parent 7d0a83a24c
commit 1bd304e7ab
6 changed files with 974 additions and 3 deletions

View File

@ -12,6 +12,8 @@ class MapWrapper {
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(); }
}
// TODO: how to export StringMap=Map as a type?
@ -47,6 +49,10 @@ class ListWrapper {
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 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 clear(List l) { l.clear(); }
}
class SetWrapper {

View File

@ -1,3 +1,5 @@
import {int} from 'facade/lang';
export var List = window.Array;
export var Map = window.Map;
export var Set = window.Set;
@ -11,6 +13,8 @@ export class MapWrapper {
m.forEach(fn);
}
static size(m) {return m.size;}
static delete(m, k) { m.delete(k); }
static clear(m) { m.clear(); }
}
// TODO: cannot export StringMap as a type as Dart does not support
@ -80,9 +84,21 @@ export class ListWrapper {
return a.reverse();
}
static concat(a, b) {return a.concat(b);}
static isList(list) {
return Array.isArray(list);
}
static insert(list, index:int, value) {
list.splice(index, 0, value);
}
static removeAt(list, index:int) {
list.splice(index, 1);
}
static clear(list) {
list.splice(0, list.length);
}
}
export class SetWrapper {
static createFromList(lst:List) { return new Set(lst); }
static has(s:Set, key):boolean { return s.has(key); }
}
}

View File

@ -83,6 +83,10 @@ class NumberWrapper {
static double parseFloat(String text) {
return double.parse(text);
}
static get NaN => double.NAN;
static bool isNaN(num value) => value.isNaN;
}
class RegExpWrapper {
@ -117,4 +121,14 @@ class BaseException extends Error {
String toString() {
return this.message;
}
}
}
// 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);
// Dart compare map keys by equality and we can have NaN != NaN
dynamic getMapKey(value) {
if (value is! num) return value;
return value.isNaN ? _NAN_KEY : value;
}

View File

@ -110,6 +110,14 @@ export class NumberWrapper {
static parseFloat(text:string):number {
return parseFloat(text);
}
static isNaN(value) {
return isNaN(value);
}
static get NaN():number {
return NaN;
}
}
export function int() {};
@ -151,4 +159,16 @@ export class BaseException extends Error {
toString():String {
return this.message;
}
}
}
// JS has NaN !== NaN
export function looseIdentical(a, b):boolean {
return a === b ||
typeof a === "number" && typeof b === "number" && isNaN(a) && isNaN(b);
}
// JS considers NaN is the same as NaN for map Key (while NaN !== NaN otherwise)
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
export function getMapKey(value) {
return value;
}