mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
Merge from master
This commit is contained in:
@@ -3,12 +3,16 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
export interface IteratorResult<T> {
|
||||
readonly done: boolean;
|
||||
readonly value: T | undefined;
|
||||
export interface IteratorDefinedResult<T> {
|
||||
readonly done: false;
|
||||
readonly value: T;
|
||||
}
|
||||
export interface IteratorUndefinedResult {
|
||||
readonly done: true;
|
||||
readonly value: undefined;
|
||||
}
|
||||
export const FIN: IteratorUndefinedResult = { done: true, value: undefined };
|
||||
export type IteratorResult<T> = IteratorDefinedResult<T> | IteratorUndefinedResult;
|
||||
|
||||
export interface Iterator<T> {
|
||||
next(): IteratorResult<T>;
|
||||
@@ -17,7 +21,7 @@ export interface Iterator<T> {
|
||||
export module Iterator {
|
||||
const _empty: Iterator<any> = {
|
||||
next() {
|
||||
return { done: true, value: undefined };
|
||||
return FIN;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,11 +29,11 @@ export module Iterator {
|
||||
return _empty;
|
||||
}
|
||||
|
||||
export function iterate<T>(array: T[], index = 0, length = array.length): Iterator<T> {
|
||||
export function fromArray<T>(array: T[], index = 0, length = array.length): Iterator<T> {
|
||||
return {
|
||||
next(): IteratorResult<T> {
|
||||
if (index >= length) {
|
||||
return { done: true, value: undefined };
|
||||
return FIN;
|
||||
}
|
||||
|
||||
return { done: false, value: array[index++] };
|
||||
@@ -37,11 +41,25 @@ export module Iterator {
|
||||
};
|
||||
}
|
||||
|
||||
export function from<T>(elements: Iterator<T> | T[] | undefined): Iterator<T> {
|
||||
if (!elements) {
|
||||
return Iterator.empty();
|
||||
} else if (Array.isArray(elements)) {
|
||||
return Iterator.fromArray(elements);
|
||||
} else {
|
||||
return elements;
|
||||
}
|
||||
}
|
||||
|
||||
export function map<T, R>(iterator: Iterator<T>, fn: (t: T) => R): Iterator<R> {
|
||||
return {
|
||||
next() {
|
||||
const { done, value } = iterator.next();
|
||||
return { done, value: done ? undefined : fn(value) };
|
||||
const element = iterator.next();
|
||||
if (element.done) {
|
||||
return FIN;
|
||||
} else {
|
||||
return { done: false, value: fn(element.value) };
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -50,14 +68,12 @@ export module Iterator {
|
||||
return {
|
||||
next() {
|
||||
while (true) {
|
||||
const { done, value } = iterator.next();
|
||||
|
||||
if (done) {
|
||||
return { done, value: undefined };
|
||||
const element = iterator.next();
|
||||
if (element.done) {
|
||||
return FIN;
|
||||
}
|
||||
|
||||
if (fn(value)) {
|
||||
return { done, value };
|
||||
if (fn(element.value)) {
|
||||
return { done: false, value: element.value };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,14 +97,14 @@ export type ISequence<T> = Iterator<T> | T[];
|
||||
|
||||
export function getSequenceIterator<T>(arg: Iterator<T> | T[]): Iterator<T> {
|
||||
if (Array.isArray(arg)) {
|
||||
return Iterator.iterate(arg);
|
||||
return Iterator.fromArray(arg);
|
||||
} else {
|
||||
return arg;
|
||||
}
|
||||
}
|
||||
|
||||
export interface INextIterator<T> {
|
||||
next(): T;
|
||||
next(): T | null;
|
||||
}
|
||||
|
||||
export class ArrayIterator<T> implements INextIterator<T> {
|
||||
@@ -105,17 +121,17 @@ export class ArrayIterator<T> implements INextIterator<T> {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public first(): T {
|
||||
public first(): T | null {
|
||||
this.index = this.start;
|
||||
return this.current();
|
||||
}
|
||||
|
||||
public next(): T {
|
||||
public next(): T | null {
|
||||
this.index = Math.min(this.index + 1, this.end);
|
||||
return this.current();
|
||||
}
|
||||
|
||||
protected current(): T {
|
||||
protected current(): T | null {
|
||||
if (this.index === this.start - 1 || this.index === this.end) {
|
||||
return null;
|
||||
}
|
||||
@@ -130,34 +146,33 @@ export class ArrayNavigator<T> extends ArrayIterator<T> implements INavigator<T>
|
||||
super(items, start, end, index);
|
||||
}
|
||||
|
||||
public current(): T {
|
||||
public current(): T | null {
|
||||
return super.current();
|
||||
}
|
||||
|
||||
public previous(): T {
|
||||
public previous(): T | null {
|
||||
this.index = Math.max(this.index - 1, this.start - 1);
|
||||
return this.current();
|
||||
}
|
||||
|
||||
public first(): T {
|
||||
public first(): T | null {
|
||||
this.index = this.start;
|
||||
return this.current();
|
||||
}
|
||||
|
||||
public last(): T {
|
||||
public last(): T | null {
|
||||
this.index = this.end - 1;
|
||||
return this.current();
|
||||
}
|
||||
|
||||
public parent(): T {
|
||||
public parent(): T | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class MappedIterator<T, R> implements INextIterator<R> {
|
||||
|
||||
constructor(protected iterator: INextIterator<T>, protected fn: (item: T) => R) {
|
||||
constructor(protected iterator: INextIterator<T>, protected fn: (item: T | null) => R) {
|
||||
// noop
|
||||
}
|
||||
|
||||
@@ -165,12 +180,12 @@ export class MappedIterator<T, R> implements INextIterator<R> {
|
||||
}
|
||||
|
||||
export interface INavigator<T> extends INextIterator<T> {
|
||||
current(): T;
|
||||
previous(): T;
|
||||
parent(): T;
|
||||
first(): T;
|
||||
last(): T;
|
||||
next(): T;
|
||||
current(): T | null;
|
||||
previous(): T | null;
|
||||
parent(): T | null;
|
||||
first(): T | null;
|
||||
last(): T | null;
|
||||
next(): T | null;
|
||||
}
|
||||
|
||||
export class MappedNavigator<T, R> extends MappedIterator<T, R> implements INavigator<R> {
|
||||
|
||||
Reference in New Issue
Block a user