fix(ListWrapper): follow JS semantics
This commit is contained in:
parent
d74dd1126b
commit
2e3e41ba64
|
@ -135,16 +135,7 @@ class ListWrapper {
|
||||||
static String join(List l, String s) => l.join(s);
|
static String join(List l, String s) => l.join(s);
|
||||||
static bool isEmpty(Iterable 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
|
l.fillRange(_startOffset(l, start), _endOffset(l, end), value);
|
||||||
// see https://github.com/google/traceur-compiler/blob/81880cd3f17bac7de90a4cd0339e9f1a9f61d24c/src/runtime/polyfills/Array.js#L94
|
|
||||||
int len = l.length;
|
|
||||||
start = start < 0 ? max(len + start, 0) : min(start, len);
|
|
||||||
if (end == null) {
|
|
||||||
end = len;
|
|
||||||
} else {
|
|
||||||
end = end < 0 ? max(len + end, 0) : min(end, len);
|
|
||||||
}
|
|
||||||
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;
|
||||||
|
@ -153,10 +144,11 @@ class ListWrapper {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
static List slice(List l, int from, int to) {
|
static List slice(List l, [int from = 0, int to]) {
|
||||||
return l.sublist(from, to);
|
return l.sublist(_startOffset(l, from), _endOffset(l, to));
|
||||||
}
|
}
|
||||||
static List splice(List l, int from, int length) {
|
static List splice(List l, int from, int length) {
|
||||||
|
from = _startOffset(l, from);
|
||||||
var to = from + length;
|
var to = from + length;
|
||||||
var sub = l.sublist(from, to);
|
var sub = l.sublist(from, to);
|
||||||
l.removeRange(from, to);
|
l.removeRange(from, to);
|
||||||
|
@ -165,6 +157,21 @@ class ListWrapper {
|
||||||
static void sort(List l, compareFn(a,b)) {
|
static void sort(List l, compareFn(a,b)) {
|
||||||
l.sort(compareFn);
|
l.sort(compareFn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// JS splice, slice, fill functions can take start < 0 which indicates a position relative to
|
||||||
|
// the end of the list
|
||||||
|
static int _startOffset(List l, int start) {
|
||||||
|
int len = l.length;
|
||||||
|
return start = start < 0 ? max(len + start, 0) : min(start, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// JS splice, slice, fill functions can take end < 0 which indicates a position relative to
|
||||||
|
// the end of the list
|
||||||
|
static int _endOffset(List l, int end) {
|
||||||
|
int len = l.length;
|
||||||
|
if (end == null) return len;
|
||||||
|
return end < 0 ? max(len + end, 0) : min(end, len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isListLikeIterable(obj) => obj is Iterable;
|
bool isListLikeIterable(obj) => obj is Iterable;
|
||||||
|
|
|
@ -184,7 +184,7 @@ export class ListWrapper {
|
||||||
return list.length == 0;
|
return list.length == 0;
|
||||||
}
|
}
|
||||||
static fill(list:List, value, start:int = 0, end:int = null) {
|
static fill(list:List, value, start:int = 0, end:int = null) {
|
||||||
list.fill(value, start, end === null ? undefined: end);
|
list.fill(value, start, end === null ? undefined : end);
|
||||||
}
|
}
|
||||||
static equals(a:List, b:List):boolean {
|
static equals(a:List, b:List):boolean {
|
||||||
if(a.length != b.length) return false;
|
if(a.length != b.length) return false;
|
||||||
|
@ -193,8 +193,8 @@ export class ListWrapper {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
static slice(l:List, from:int, to:int):List {
|
static slice(l:List, from:int = 0, to:int = null):List {
|
||||||
return l.slice(from, to);
|
return l.slice(from, to === null ? undefined : to);
|
||||||
}
|
}
|
||||||
static splice(l:List, from:int, length:int):List {
|
static splice(l:List, from:int, length:int):List {
|
||||||
return l.splice(from, length);
|
return l.splice(from, length);
|
||||||
|
|
|
@ -170,7 +170,9 @@ export class ListWrapper {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
static slice<T>(l: List<T>, from: int, to: int): List<T> { return l.slice(from, to); }
|
static slice<T>(l: List<T>, from: int = 0, to: int = null): List<T> {
|
||||||
|
return l.slice(from, to === null ? undefined : to);
|
||||||
|
}
|
||||||
static splice<T>(l: List<T>, from: int, length: int): List<T> { return l.splice(from, length); }
|
static splice<T>(l: List<T>, from: int, length: int): List<T> { return l.splice(from, length); }
|
||||||
static sort<T>(l: List<T>, compareFn: (a: T, b: T) => number) { l.sort(compareFn); }
|
static sort<T>(l: List<T>, compareFn: (a: T, b: T) => number) { l.sort(compareFn); }
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,68 @@ import {List, ListWrapper} from 'angular2/src/facade/collection';
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
describe('ListWrapper', () => {
|
describe('ListWrapper', () => {
|
||||||
|
var l: List;
|
||||||
|
|
||||||
describe('splice', () => {
|
describe('splice', () => {
|
||||||
it('should remove sublist of given length and return it', () => {
|
it('should remove sublist of given length and return it', () => {
|
||||||
var list = [1, 2, 3, 4, 5, 6];
|
var list = [1, 2, 3, 4, 5, 6];
|
||||||
expect(ListWrapper.splice(list, 1, 3)).toEqual([2, 3, 4]);
|
expect(ListWrapper.splice(list, 1, 3)).toEqual([2, 3, 4]);
|
||||||
expect(list).toEqual([1, 5, 6]);
|
expect(list).toEqual([1, 5, 6]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support negative start', () => {
|
||||||
|
var list = [1, 2, 3, 4, 5, 6];
|
||||||
|
expect(ListWrapper.splice(list, -5, 3)).toEqual([2, 3, 4]);
|
||||||
|
expect(list).toEqual([1, 5, 6]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('fill', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
l = [1, 2, 3, 4];
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fill the whole list if neither start nor end are specified', () => {
|
||||||
|
ListWrapper.fill(l, 9);
|
||||||
|
expect(l).toEqual([9, 9, 9, 9]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fill up to the end if end is not specified', () => {
|
||||||
|
ListWrapper.fill(l, 9, 1);
|
||||||
|
expect(l).toEqual([1, 9, 9, 9]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support negative start', () => {
|
||||||
|
ListWrapper.fill(l, 9, -1);
|
||||||
|
expect(l).toEqual([1, 2, 3, 9]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support negative end', () => {
|
||||||
|
ListWrapper.fill(l, 9, -2, -1);
|
||||||
|
expect(l).toEqual([1, 2, 9, 4]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('slice', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
l = [1, 2, 3, 4];
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return the whole list if neither start nor end are specified', () => {
|
||||||
|
expect(ListWrapper.slice(l)).toEqual([1, 2, 3, 4]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return up to the end if end is not specified', () => {
|
||||||
|
expect(ListWrapper.slice(l, 1)).toEqual([2, 3, 4]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support negative start', () => {
|
||||||
|
expect(ListWrapper.slice(l, -1)).toEqual([4]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support negative end', () => {
|
||||||
|
expect(ListWrapper.slice(l, -3, -1)).toEqual([2, 3]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue