diff --git a/.babelrc b/.babelrc deleted file mode 100644 index f4bbc58..0000000 --- a/.babelrc +++ /dev/null @@ -1,12 +0,0 @@ -{ - "plugins": [ - [ - "transform-runtime", - { - "polyfill": false, - "regenerator": true - } - ] - ], - "presets": ["es2015", "stage-0"] -} diff --git a/.gitignore b/.gitignore index 2946f04..f4f7c5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ .DS_Store +.nyc_output /coverage /dist /node_modules +/typings npm-debug.log diff --git a/.travis.yml b/.travis.yml index 42b0d6e..bf1485d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ script: - npm run lint - npm test after_success: - - npm run coverage + - npm run cover - cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js cache: directories: diff --git a/package.json b/package.json index 8063278..2f1961e 100644 --- a/package.json +++ b/package.json @@ -5,11 +5,12 @@ "main": "index.js", "scripts": { "build": "tsc --declaration && cp LICENSE README.md package.json dist/", - "clean": "rm -fr coverage dist", - "coverage": "remap-istanbul -i coverage/coverage.json -t lcovonly -o coverage/lcov.info", - "coverage-html": "remap-istanbul -i coverage/coverage.json -t html -o coverage", - "lint": "tslint src/**/*.ts", - "test": "babel-node ./node_modules/.bin/istanbul cover --include-all-sources _mocha --report none -- --compilers js:babel-core/register --recursive", + "cover": "nyc npm test", + "lint": "tslint src/**/*.ts test/**/*.ts", + "postinstall": "typings install", + "prebuild": "rm -fr dist", + "precover": "rm -fr .nyc_output coverage", + "test": "mocha --recursive --compilers ts:ts-node/register", "watch": "tsc --watch" }, "author": "Shota Nozaki", @@ -20,19 +21,34 @@ }, "homepage": "https://github.com/emonkak/js-enumerable", "devDependencies": { - "babel-cli": "^6.16.0", - "babel-core": "^6.17.0", - "babel-plugin-transform-runtime": "^6.15.0", - "babel-preset-es2015": "^6.16.0", - "babel-preset-stage-0": "^6.16.0", "cash-cp": "^0.2.0", "cash-rm": "^0.2.0", "coveralls": "^2.11.14", - "istanbul": "^0.4.5", "mocha": "^3.1.0", - "remap-istanbul": "^0.6.4", + "nyc": "^8.3.0", "sinon": "^1.17.6", + "ts-node": "^1.3.0", "tslint": "^3.15.1", - "typescript": "^2.0.3" + "typescript": "^2.0.3", + "typings": "^1.4.0" + }, + "nyc": { + "extension": [ + ".ts" + ], + "exclude": [ + "dist", + "node_modules", + "typings" + ], + "require": [ + "ts-node/register" + ], + "reporter": [ + "lcov", + "html", + "text" + ], + "all": true } } diff --git a/src/Enumerable.ts b/src/Enumerable.ts index 8198077..f258da9 100644 --- a/src/Enumerable.ts +++ b/src/Enumerable.ts @@ -1,12 +1,3 @@ -export class Enumerable implements Iterable { - constructor(private _source: Iterable) { - } +import { Enumerable } from './internal/Enumerable'; - [Symbol.iterator](): Iterator { - return this._source[Symbol.iterator](); - } - - lift(source: Iterable): Enumerable { - return new Enumerable(source); - } -} +export default Enumerable; diff --git a/src/extensions/aggregate.ts b/src/extensions/aggregate.ts index 8b6caf8..c00e794 100644 --- a/src/extensions/aggregate.ts +++ b/src/extensions/aggregate.ts @@ -1,10 +1,10 @@ import aggregate from '../aggregate'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.aggregate = aggregate; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - aggregate: typeof aggregate; + aggregate(seed: TAccumulate, func: (result: TAccumulate, element: TSource) => TAccumulate): TAccumulate; } } diff --git a/src/extensions/all.ts b/src/extensions/all.ts index bd1b376..4f78cc8 100644 --- a/src/extensions/all.ts +++ b/src/extensions/all.ts @@ -1,10 +1,10 @@ import all from '../all'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.all = all; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - all: typeof all; + all(predicate?: (element: TSource) => boolean): boolean; } } diff --git a/src/extensions/any.ts b/src/extensions/any.ts index 8d9f34b..7ad75f8 100644 --- a/src/extensions/any.ts +++ b/src/extensions/any.ts @@ -1,10 +1,10 @@ import any from '../any'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.any = any; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - any: typeof any; + any(predicate?: (element: TSource) => boolean): boolean; } } diff --git a/src/extensions/average.ts b/src/extensions/average.ts index 511fe55..424c838 100644 --- a/src/extensions/average.ts +++ b/src/extensions/average.ts @@ -1,10 +1,10 @@ import average from '../average'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.average = average; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - average: typeof average; + average(selector?: (element: TSource) => number): number; } } diff --git a/src/extensions/buffer.ts b/src/extensions/buffer.ts index 8b91565..661fb71 100644 --- a/src/extensions/buffer.ts +++ b/src/extensions/buffer.ts @@ -1,10 +1,14 @@ -import buffer from '../lifted/buffer'; -import { Enumerable } from '../Enumerable'; +import bufferFn from '../buffer'; +import { Enumerable } from '../internal/Enumerable'; + +function buffer(this: Enumerable, count: number, skip?: number): Enumerable { + return this.lift(bufferFn.call(this, count, skip)); +} Enumerable.prototype.buffer = buffer; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - buffer: typeof buffer; + buffer(count: number, skip?: number): Enumerable; } } diff --git a/src/extensions/catch.ts b/src/extensions/catch.ts index d2997ce..c1c5a61 100644 --- a/src/extensions/catch.ts +++ b/src/extensions/catch.ts @@ -1,10 +1,14 @@ -import _catch from '../lifted/catch'; -import { Enumerable } from '../Enumerable'; +import catchFn from '../catch'; +import { Enumerable } from '../internal/Enumerable'; + +function _catch(this: Enumerable, handler: (exception: TException) => Iterable): Enumerable { + return this.lift(catchFn.call(this, handler)); +} Enumerable.prototype.catch = _catch; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - catch: typeof _catch; + catch(handler: (exception: TException) => Iterable): Enumerable; } } diff --git a/src/extensions/concat.ts b/src/extensions/concat.ts index 0a4bf5f..aa86513 100644 --- a/src/extensions/concat.ts +++ b/src/extensions/concat.ts @@ -1,10 +1,14 @@ -import concat from '../lifted/concat'; -import { Enumerable } from '../Enumerable'; +import concatFn from '../concat'; +import { Enumerable } from '../internal/Enumerable'; + +function concat(this: Enumerable, ...sources: Iterable[]): Enumerable { + return this.lift(concatFn.apply(this, sources)); +} Enumerable.prototype.concat = concat; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - concat: typeof concat; + concat(...sources: Iterable[]): Enumerable; } } diff --git a/src/extensions/count.ts b/src/extensions/count.ts index 62c0e2d..04bce8b 100644 --- a/src/extensions/count.ts +++ b/src/extensions/count.ts @@ -1,10 +1,10 @@ import count from '../count'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.count = count; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - count: typeof count; + count(predicate?: (item: TSource) => boolean): number; } } diff --git a/src/extensions/defaultIfEmpty.ts b/src/extensions/defaultIfEmpty.ts index a2a89d3..49ac050 100644 --- a/src/extensions/defaultIfEmpty.ts +++ b/src/extensions/defaultIfEmpty.ts @@ -1,10 +1,14 @@ -import defaultIfEmpty from '../lifted/defaultIfEmpty'; -import { Enumerable } from '../Enumerable'; +import defaultIfEmptyFn from '../defaultIfEmpty'; +import { Enumerable } from '../internal/Enumerable'; + +function defaultIfEmpty(this: Enumerable, defaultValue: TSource): Enumerable { + return this.lift(defaultIfEmptyFn.call(this, defaultValue)); +} Enumerable.prototype.defaultIfEmpty = defaultIfEmpty; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - defaultIfEmpty: typeof defaultIfEmpty; + defaultIfEmpty(defaultValue: TSource): Enumerable; } } diff --git a/src/extensions/distinct.ts b/src/extensions/distinct.ts index e98f8be..90731ee 100644 --- a/src/extensions/distinct.ts +++ b/src/extensions/distinct.ts @@ -1,10 +1,16 @@ -import distinct from '../lifted/distinct'; -import { Enumerable } from '../Enumerable'; +import distinctFn from '../distinct'; +import { Enumerable } from '../internal/Enumerable'; + +function distinct(this: Enumerable): Enumerable; +function distinct(this: Enumerable, keySelector?: (element: TSource) => TKey): Enumerable { + return this.lift(distinctFn.call(this, keySelector)); +} Enumerable.prototype.distinct = distinct; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - distinct: typeof distinct; + distinct(): Enumerable; + distinct(keySelector?: (element: TSource) => TKey): Enumerable; } } diff --git a/src/extensions/distinctUntilChanged.ts b/src/extensions/distinctUntilChanged.ts index e6133c2..76bb22e 100644 --- a/src/extensions/distinctUntilChanged.ts +++ b/src/extensions/distinctUntilChanged.ts @@ -1,10 +1,16 @@ -import distinctUntilChanged from '../lifted/distinctUntilChanged'; -import { Enumerable } from '../Enumerable'; +import distinctUntilChangedFn from '../distinctUntilChanged'; +import { Enumerable } from '../internal/Enumerable'; + +function distinctUntilChanged(this: Enumerable): Enumerable; +function distinctUntilChanged(this: Enumerable, keySelector?: (element: TSource) => TKey): Enumerable { + return this.lift(distinctUntilChangedFn.call(this, keySelector)); +} Enumerable.prototype.distinctUntilChanged = distinctUntilChanged; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - distinctUntilChanged: typeof distinctUntilChanged; + distinctUntilChanged(): Enumerable; + distinctUntilChanged(keySelector?: (element: TSource) => TKey): Enumerable; } } diff --git a/src/extensions/do.ts b/src/extensions/do.ts index 7e5a4ad..e768d24 100644 --- a/src/extensions/do.ts +++ b/src/extensions/do.ts @@ -1,10 +1,14 @@ -import _do from '../lifted/do'; -import { Enumerable } from '../Enumerable'; +import doFn from '../do'; +import { Enumerable } from '../internal/Enumerable'; + +function _do(this: Enumerable, action: (element: TSource) => void): Enumerable { + return this.lift(doFn.call(this, action)); +} Enumerable.prototype.do = _do; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - do: typeof _do; + do(action: (element: TSource) => void): Enumerable; } } diff --git a/src/extensions/doWhile.ts b/src/extensions/doWhile.ts index 14f97dd..95072bb 100644 --- a/src/extensions/doWhile.ts +++ b/src/extensions/doWhile.ts @@ -1,10 +1,14 @@ -import doWhile from '../lifted/doWhile'; -import { Enumerable } from '../Enumerable'; +import doWhileFn from '../doWhile'; +import { Enumerable } from '../internal/Enumerable'; + +function doWhile(this: Enumerable, condition: () => boolean): Enumerable { + return this.lift(doWhileFn.call(this, condition)); +} Enumerable.prototype.doWhile = doWhile; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - doWhile: typeof doWhile; + doWhile(condition: () => boolean): Enumerable; } } diff --git a/src/extensions/elementAt.ts b/src/extensions/elementAt.ts index 52459c1..e352893 100644 --- a/src/extensions/elementAt.ts +++ b/src/extensions/elementAt.ts @@ -1,10 +1,10 @@ import elementAt from '../elementAt'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.elementAt = elementAt; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - elementAt: typeof elementAt; + elementAt(index: number): TSource; } } diff --git a/src/extensions/elementAtOrDefault.ts b/src/extensions/elementAtOrDefault.ts index 01c1b4a..24d12a6 100644 --- a/src/extensions/elementAtOrDefault.ts +++ b/src/extensions/elementAtOrDefault.ts @@ -1,10 +1,10 @@ import elementAtOrDefault from '../elementAtOrDefault'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.elementAtOrDefault = elementAtOrDefault; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - elementAtOrDefault: typeof elementAtOrDefault; + elementAtOrDefault(index: number, defaultValue?: TSource): TSource; } } diff --git a/src/extensions/except.ts b/src/extensions/except.ts index 86fc7ee..3efdc91 100644 --- a/src/extensions/except.ts +++ b/src/extensions/except.ts @@ -1,10 +1,14 @@ -import except from '../except'; -import { Enumerable } from '../Enumerable'; +import exceptFn from '../except'; +import { Enumerable } from '../internal/Enumerable'; + +function except(this: Enumerable, second: Iterable): Enumerable { + return this.lift(exceptFn.call(this, second)); +} Enumerable.prototype.except = except; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - except: typeof except; + except(second: Iterable): Enumerable; } } diff --git a/src/extensions/finally.ts b/src/extensions/finally.ts index 6994af1..0cf7f00 100644 --- a/src/extensions/finally.ts +++ b/src/extensions/finally.ts @@ -1,10 +1,14 @@ -import _finally from '../finally'; -import { Enumerable } from '../Enumerable'; +import finallyFn from '../finally'; +import { Enumerable } from '../internal/Enumerable'; + +function _finally(this: Enumerable, finallyAction: () => void): Enumerable { + return this.lift(finallyFn.call(this, finallyAction)); +} Enumerable.prototype.finally = _finally; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - finally: typeof _finally; + finally(finallyAction: () => void): Enumerable; } } diff --git a/src/extensions/first.ts b/src/extensions/first.ts index 6ae4d73..5796a5c 100644 --- a/src/extensions/first.ts +++ b/src/extensions/first.ts @@ -1,10 +1,10 @@ import first from '../first'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.first = first; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - first: typeof first; + first(predicate?: (element: TSource) => boolean): TSource; } } diff --git a/src/extensions/firstOrDefault.ts b/src/extensions/firstOrDefault.ts index eca73b9..2bdd620 100644 --- a/src/extensions/firstOrDefault.ts +++ b/src/extensions/firstOrDefault.ts @@ -1,10 +1,10 @@ import firstOrDefault from '../firstOrDefault'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.firstOrDefault = firstOrDefault; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - firstOrDefault: typeof firstOrDefault; + firstOrDefault(predicate?: (element: TSource) => boolean, defaultValue?: TSource): TSource; } } diff --git a/src/extensions/forEach.ts b/src/extensions/forEach.ts index 23357a1..5ee9957 100644 --- a/src/extensions/forEach.ts +++ b/src/extensions/forEach.ts @@ -1,10 +1,10 @@ import forEach from '../forEach'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.forEach = forEach; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - forEach: typeof forEach; + forEach(action: (element: TSource) => void): void; } } diff --git a/src/extensions/groupBy.ts b/src/extensions/groupBy.ts index eb37c7a..390d801 100644 --- a/src/extensions/groupBy.ts +++ b/src/extensions/groupBy.ts @@ -1,10 +1,18 @@ -import groupBy from '../lifted/groupBy'; -import { Enumerable } from '../Enumerable'; +import groupByFn from '../groupBy'; +import { Enumerable } from '../internal/Enumerable'; + +function groupBy(this: Enumerable, keySelector: (element: TSource) => TKey): Enumerable<[TKey, TSource]>; +function groupBy(this: Enumerable, keySelector: (element: TSource) => TKey, elementSelector: (element: TSource) => TElement): Enumerable<[TKey, TElement]>; +function groupBy(this: Enumerable, keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement, resultSelector?: (key: TKey, elements: TElement[]) => TResult): Enumerable { + return this.lift(groupByFn.call(this, keySelector, elementSelector, resultSelector)); +} Enumerable.prototype.groupBy = groupBy; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - groupBy: typeof groupBy; + groupBy(keySelector: (element: TSource) => TKey): Enumerable<[TKey, TSource]>; + groupBy(keySelector: (element: TSource) => TKey, elementSelector: (element: TSource) => TElement): Enumerable<[TKey, TElement]>; + groupBy(keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement, resultSelector?: (key: TKey, elements: TElement[]) => TResult): Enumerable; } } diff --git a/src/extensions/groupJoin.ts b/src/extensions/groupJoin.ts index 6d0241d..2caec2f 100644 --- a/src/extensions/groupJoin.ts +++ b/src/extensions/groupJoin.ts @@ -1,10 +1,14 @@ -import groupJoin from '../lifted/groupJoin'; -import { Enumerable } from '../Enumerable'; +import groupJoinFn from '../groupJoin'; +import { Enumerable } from '../internal/Enumerable'; + +function groupJoin(this: Enumerable, inner: Iterable, outerKeySelector: (element: TOuter) => TKey, innerKeySelector: (element: TInner) => TKey, resultSelector: (outer: TOuter, inner: TInner[]) => TResult): Enumerable { + return this.lift(groupJoinFn.call(this, inner, outerKeySelector, innerKeySelector, resultSelector)); +} Enumerable.prototype.groupJoin = groupJoin; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - groupJoin: typeof groupJoin; + groupJoin(inner: Iterable, outerKeySelector: (element: TSource) => TKey, innerKeySelector: (element: TInner) => TKey, resultSelector: (outer: TSource, inner: TInner[]) => TResult): Enumerable; } } diff --git a/src/extensions/ignoreElements.ts b/src/extensions/ignoreElements.ts index 81cfe23..6d7808f 100644 --- a/src/extensions/ignoreElements.ts +++ b/src/extensions/ignoreElements.ts @@ -1,10 +1,14 @@ -import ignoreElements from '../lifted/ignoreElements'; -import { Enumerable } from '../Enumerable'; +import ignoreElementsFn from '../ignoreElements'; +import { Enumerable } from '../internal/Enumerable'; + +function ignoreElements(this: Enumerable): Enumerable { + return this.lift(ignoreElementsFn.call(this)); +} Enumerable.prototype.ignoreElements = ignoreElements; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - ignoreElements: typeof ignoreElements; + ignoreElements(): Enumerable; } } diff --git a/src/extensions/intersect.ts b/src/extensions/intersect.ts index 0b41c8e..9c2632d 100644 --- a/src/extensions/intersect.ts +++ b/src/extensions/intersect.ts @@ -1,10 +1,14 @@ -import intersect from '../lifted/intersect'; -import { Enumerable } from '../Enumerable'; +import intersectFn from '../intersect'; +import { Enumerable } from '../internal/Enumerable'; + +function intersect(this: Enumerable, second: Iterable): Enumerable { + return this.lift(intersectFn.call(this, second)); +} Enumerable.prototype.intersect = intersect; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - intersect: typeof intersect; + intersect(second: Iterable): Enumerable; } } diff --git a/src/extensions/isEmpty.ts b/src/extensions/isEmpty.ts index ce4d392..50e1bb5 100644 --- a/src/extensions/isEmpty.ts +++ b/src/extensions/isEmpty.ts @@ -1,10 +1,10 @@ import isEmpty from '../isEmpty'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.isEmpty = isEmpty; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - isEmpty: typeof isEmpty; + isEmpty(): boolean; } } diff --git a/src/extensions/join.ts b/src/extensions/join.ts index 53efb0b..18a0487 100644 --- a/src/extensions/join.ts +++ b/src/extensions/join.ts @@ -1,10 +1,14 @@ -import join from '../lifted/join'; -import { Enumerable } from '../Enumerable'; +import joinFn from '../join'; +import { Enumerable } from '../internal/Enumerable'; + +function join(this: Enumerable, inner: Iterable, outerKeySelector: (element: TOuter) => TKey, innerKeySelector: (element: TInner) => TKey, resultSelector: (outer: TOuter, inner: TInner) => TResult): Enumerable { + return this.lift(joinFn.call(this, inner, outerKeySelector, innerKeySelector, resultSelector)); +} Enumerable.prototype.join = join; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - join: typeof join; + join(inner: Iterable, outerKeySelector: (element: TSource) => TKey, innerKeySelector: (element: TInner) => TKey, resultSelector: (outer: TSource, inner: TInner) => TResult): Enumerable; } } diff --git a/src/extensions/last.ts b/src/extensions/last.ts index 8e638a7..2379617 100644 --- a/src/extensions/last.ts +++ b/src/extensions/last.ts @@ -1,10 +1,10 @@ import last from '../last'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.last = last; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - last: typeof last; + last(predicate?: (value: TSource) => boolean): TSource; } } diff --git a/src/extensions/lastOrDefault.ts b/src/extensions/lastOrDefault.ts index 3df4f86..80cd507 100644 --- a/src/extensions/lastOrDefault.ts +++ b/src/extensions/lastOrDefault.ts @@ -1,10 +1,10 @@ import lastOrDefault from '../lastOrDefault'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.lastOrDefault = lastOrDefault; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - lastOrDefault: typeof lastOrDefault; + lastOrDefault(predicate?: (value: TSource) => boolean, defaultValue?: TSource): TSource; } } diff --git a/src/extensions/max.ts b/src/extensions/max.ts index ce90d51..f7c0abf 100644 --- a/src/extensions/max.ts +++ b/src/extensions/max.ts @@ -1,10 +1,10 @@ import max from '../max'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.max = max; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - max: typeof max; + max(selector?: (element: TSource) => number): number; } } diff --git a/src/extensions/maxBy.ts b/src/extensions/maxBy.ts index e29ae17..a737b1a 100644 --- a/src/extensions/maxBy.ts +++ b/src/extensions/maxBy.ts @@ -1,10 +1,10 @@ import maxBy from '../maxBy'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.maxBy = maxBy; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - maxBy: typeof maxBy; + maxBy(keySelector?: (element: TSource) => TKey): TSource[]; } } diff --git a/src/extensions/memoize.ts b/src/extensions/memoize.ts index 1675c26..ee0e76f 100644 --- a/src/extensions/memoize.ts +++ b/src/extensions/memoize.ts @@ -1,10 +1,14 @@ -import memoize from '../lifted/memoize'; -import { Enumerable } from '../Enumerable'; +import memoizeFn from '../memoize'; +import { Enumerable } from '../internal/Enumerable'; + +function memoize(this: Enumerable): Enumerable { + return this.lift(memoizeFn.call(this)); +} Enumerable.prototype.memoize = memoize; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - memoize: typeof memoize; + memoize(): Enumerable; } } diff --git a/src/extensions/min.ts b/src/extensions/min.ts index 08970a4..81abe66 100644 --- a/src/extensions/min.ts +++ b/src/extensions/min.ts @@ -1,10 +1,10 @@ import min from '../min'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.min = min; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - min: typeof min; + min(selector?: (element: TSource) => number): number; } } diff --git a/src/extensions/minBy.ts b/src/extensions/minBy.ts index 86cff2e..cac2575 100644 --- a/src/extensions/minBy.ts +++ b/src/extensions/minBy.ts @@ -1,10 +1,10 @@ import minBy from '../minBy'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.minBy = minBy; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - minBy: typeof minBy; + minBy(keySelector?: (element: TSource) => TKey): TSource[]; } } diff --git a/src/extensions/onErrorResumeNext.ts b/src/extensions/onErrorResumeNext.ts index 3cb3cb0..d975639 100644 --- a/src/extensions/onErrorResumeNext.ts +++ b/src/extensions/onErrorResumeNext.ts @@ -1,10 +1,14 @@ -import onErrorResumeNext from '../lifted/onErrorResumeNext'; -import { Enumerable } from '../Enumerable'; +import onErrorResumeNextFn from '../onErrorResumeNext'; +import { Enumerable } from '../internal/Enumerable'; + +function onErrorResumeNext(this: Enumerable, ...sources: Iterable[]): Enumerable { + return this.lift(onErrorResumeNextFn.apply(this, sources)); +} Enumerable.prototype.onErrorResumeNext = onErrorResumeNext; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - onErrorResumeNext: typeof onErrorResumeNext; + onErrorResumeNext(...sources: Iterable[]): Enumerable; } } diff --git a/src/extensions/orderBy.ts b/src/extensions/orderBy.ts index 61bac4c..d746306 100644 --- a/src/extensions/orderBy.ts +++ b/src/extensions/orderBy.ts @@ -1,10 +1,12 @@ -import orderBy from '../lifted/orderBy'; -import { Enumerable } from '../Enumerable'; +import OrderedEnumerable from '../internal/OrderedEnumerable'; +import orderBy from '../orderBy'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.orderBy = orderBy; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - orderBy: typeof orderBy; + orderBy(): OrderedEnumerable; + orderBy(keySelector?: (value: TSource) => TKey): OrderedEnumerable; } } diff --git a/src/extensions/orderByDescending.ts b/src/extensions/orderByDescending.ts index be4f0af..97fc6ae 100644 --- a/src/extensions/orderByDescending.ts +++ b/src/extensions/orderByDescending.ts @@ -1,10 +1,12 @@ -import orderByDescending from '../lifted/orderByDescending'; -import { Enumerable } from '../Enumerable'; +import OrderedEnumerable from '../internal/OrderedEnumerable'; +import orderByDescending from '../orderByDescending'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.orderByDescending = orderByDescending; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - orderByDescending: typeof orderByDescending; + orderByDescending(): OrderedEnumerable; + orderByDescending(keySelector?: (value: TSource) => TKey): OrderedEnumerable; } } diff --git a/src/extensions/repeat.ts b/src/extensions/repeat.ts index a3d2612..46a2d4b 100644 --- a/src/extensions/repeat.ts +++ b/src/extensions/repeat.ts @@ -1,10 +1,14 @@ -import repeat from '../lifted/repeat'; -import { Enumerable } from '../Enumerable'; +import repeatFn from '../repeat'; +import { Enumerable } from '../internal/Enumerable'; + +function repeat(this: Enumerable, count?: number): Enumerable { + return this.lift(repeatFn.call(this, count)); +} Enumerable.prototype.repeat = repeat; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - repeat: typeof repeat; + repeat(count?: number): Enumerable; } } diff --git a/src/extensions/retry.ts b/src/extensions/retry.ts index 3bc06fb..f1e2e5d 100644 --- a/src/extensions/retry.ts +++ b/src/extensions/retry.ts @@ -1,10 +1,14 @@ -import retry from '../lifted/retry'; -import { Enumerable } from '../Enumerable'; +import retryFn from '../retry'; +import { Enumerable } from '../internal/Enumerable'; + +function retry(this: Enumerable, retryCount?: number): Enumerable { + return this.lift(retryFn.call(this, retryCount)); +} Enumerable.prototype.retry = retry; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - retry: typeof retry; + retry(retryCount?: number): Enumerable; } } diff --git a/src/extensions/reverse.ts b/src/extensions/reverse.ts index c0b8bf6..fe197d3 100644 --- a/src/extensions/reverse.ts +++ b/src/extensions/reverse.ts @@ -1,10 +1,14 @@ -import reverse from '../lifted/reverse'; -import { Enumerable } from '../Enumerable'; +import reverseFn from '../reverse'; +import { Enumerable } from '../internal/Enumerable'; + +function reverse(this: Enumerable): Enumerable { + return this.lift(reverseFn.call(this)); +} Enumerable.prototype.reverse = reverse; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - reverse: typeof reverse; + reverse(): Enumerable; } } diff --git a/src/extensions/scan.ts b/src/extensions/scan.ts index 405be06..f6ae705 100644 --- a/src/extensions/scan.ts +++ b/src/extensions/scan.ts @@ -1,10 +1,14 @@ -import scan from '../lifted/scan'; -import { Enumerable } from '../Enumerable'; +import scanFn from '../scan'; +import { Enumerable } from '../internal/Enumerable'; + +function scan(this: Enumerable, seed: TAccumulate, func: (result: TAccumulate, element: TSource) => TAccumulate): Enumerable { + return this.lift(scanFn.call(this, seed, func)); +} Enumerable.prototype.scan = scan; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - scan: typeof scan; + scan(seed: TAccumulate, func: (result: TAccumulate, element: TSource) => TAccumulate): Enumerable; } } diff --git a/src/extensions/select.ts b/src/extensions/select.ts index acae32b..e6b3d6b 100644 --- a/src/extensions/select.ts +++ b/src/extensions/select.ts @@ -1,10 +1,14 @@ -import select from '../lifted/select'; -import { Enumerable } from '../Enumerable'; +import selectFn from '../select'; +import { Enumerable } from '../internal/Enumerable'; + +function select(this: Enumerable, selector: (element: TSource) => TResult): Enumerable { + return this.lift(selectFn.call(this, selector)); +} Enumerable.prototype.select = select; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - select: typeof select; + select(selector: (element: TSource) => TResult): Enumerable; } } diff --git a/src/extensions/selectMany.ts b/src/extensions/selectMany.ts index 13ddb94..96fd0ea 100644 --- a/src/extensions/selectMany.ts +++ b/src/extensions/selectMany.ts @@ -1,10 +1,14 @@ -import selectMany from '../lifted/selectMany'; -import { Enumerable } from '../Enumerable'; +import selectManyFn from '../selectMany'; +import { Enumerable } from '../internal/Enumerable'; + +function selectMany(this: Enumerable, collectionSelector: (element: TSource) => Iterable): Enumerable { + return this.lift(selectManyFn.call(this, collectionSelector)); +} Enumerable.prototype.selectMany = selectMany; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - selectMany: typeof selectMany; + selectMany(collectionSelector: (element: TSource) => Iterable): Enumerable; } } diff --git a/src/extensions/single.ts b/src/extensions/single.ts index 023cf35..91800e2 100644 --- a/src/extensions/single.ts +++ b/src/extensions/single.ts @@ -1,10 +1,10 @@ import single from '../single'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.single = single; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - single: typeof single; + single(predicate?: (element: TSource) => boolean): TSource; } } diff --git a/src/extensions/singleOrDefault.ts b/src/extensions/singleOrDefault.ts index 59a43bb..6734cd0 100644 --- a/src/extensions/singleOrDefault.ts +++ b/src/extensions/singleOrDefault.ts @@ -1,10 +1,10 @@ import singleOrDefault from '../singleOrDefault'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.singleOrDefault = singleOrDefault; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - singleOrDefault: typeof singleOrDefault; + singleOrDefault(predicate?: (element: TSource) => boolean, defaultValue?: TSource): TSource; } } diff --git a/src/extensions/skip.ts b/src/extensions/skip.ts index 120790c..8c5d9ce 100644 --- a/src/extensions/skip.ts +++ b/src/extensions/skip.ts @@ -1,10 +1,14 @@ -import skip from '../lifted/skip'; -import { Enumerable } from '../Enumerable'; +import skipFn from '../skip'; +import { Enumerable } from '../internal/Enumerable'; + +function skip(this: Enumerable, count: number): Enumerable { + return this.lift(skipFn.call(this, count)); +} Enumerable.prototype.skip = skip; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - skip: typeof skip; + skip(count: number): Enumerable; } } diff --git a/src/extensions/skipLast.ts b/src/extensions/skipLast.ts index 0c10851..174d32e 100644 --- a/src/extensions/skipLast.ts +++ b/src/extensions/skipLast.ts @@ -1,10 +1,14 @@ -import skipLast from '../lifted/skipLast'; -import { Enumerable } from '../Enumerable'; +import skipLastFn from '../skipLast'; +import { Enumerable } from '../internal/Enumerable'; + +function skipLast(this: Enumerable, count: number): Enumerable { + return this.lift(skipLastFn.call(this, count)); +} Enumerable.prototype.skipLast = skipLast; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - skipLast: typeof skipLast; + skipLast(count: number): Enumerable; } } diff --git a/src/extensions/skipWhile.ts b/src/extensions/skipWhile.ts index b33b9ab..3bb75e8 100644 --- a/src/extensions/skipWhile.ts +++ b/src/extensions/skipWhile.ts @@ -1,10 +1,14 @@ -import skipWhile from '../lifted/skipWhile'; -import { Enumerable } from '../Enumerable'; +import skipWhileFn from '../skipWhile'; +import { Enumerable } from '../internal/Enumerable'; + +function skipWhile(this: Enumerable, predicate: (element: TSource) => boolean): Enumerable { + return this.lift(skipWhileFn.call(this, predicate)); +} Enumerable.prototype.skipWhile = skipWhile; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - skipWhile: typeof skipWhile; + skipWhile(predicate: (element: TSource) => boolean): Enumerable; } } diff --git a/src/extensions/startWith.ts b/src/extensions/startWith.ts index f451bb1..70c8b0f 100644 --- a/src/extensions/startWith.ts +++ b/src/extensions/startWith.ts @@ -1,10 +1,14 @@ -import startWith from '../lifted/startWith'; -import { Enumerable } from '../Enumerable'; +import startWithFn from '../startWith'; +import { Enumerable } from '../internal/Enumerable'; + +function startWith(this: Enumerable, ...elements: TSource[]): Enumerable { + return this.lift(startWithFn.apply(this, elements)); +} Enumerable.prototype.startWith = startWith; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - startWith: typeof startWith; + startWith(...elements: TSource[]): Enumerable; } } diff --git a/src/extensions/static/catch.ts b/src/extensions/static/catch.ts index 7dde07f..19ba7b6 100644 --- a/src/extensions/static/catch.ts +++ b/src/extensions/static/catch.ts @@ -1,10 +1,14 @@ -import staticCatch from '../../lifted/static/catch'; -import { Enumerable } from '../../Enumerable'; +import catchFn from '../../static/catch'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable._catch = staticCatch; +function _catch(...sources: Iterable[]): Enumerable { + return new Enumerable(catchFn(...sources)); +} + +Enumerable._catch = _catch; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let _catch: typeof staticCatch; + export function _catch(...sources: Iterable[]): Enumerable; } } diff --git a/src/extensions/static/concat.ts b/src/extensions/static/concat.ts index 3759ef7..728e40d 100644 --- a/src/extensions/static/concat.ts +++ b/src/extensions/static/concat.ts @@ -1,10 +1,14 @@ -import staticConcat from '../../lifted/static/concat'; -import { Enumerable } from '../../Enumerable'; +import concatFn from '../../static/concat'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable.concat = staticConcat; +function concat(...sources: Iterable[]): Enumerable { + return new Enumerable(concatFn(...sources)); +} + +Enumerable.concat = concat; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let concat: typeof staticConcat; + export function concat(...sources: Iterable[]): Enumerable; } } diff --git a/src/extensions/static/defer.ts b/src/extensions/static/defer.ts index 4b1ea28..1183722 100644 --- a/src/extensions/static/defer.ts +++ b/src/extensions/static/defer.ts @@ -1,10 +1,14 @@ -import staticDefer from '../../lifted/static/defer'; -import { Enumerable } from '../../Enumerable'; +import deferFn from '../../static/defer'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable.defer = staticDefer; +function defer(iterableFactory: () => Iterable): Enumerable { + return new Enumerable(deferFn(iterableFactory)); +} + +Enumerable.defer = defer; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let defer: typeof staticDefer; + export function defer(iterableFactory: () => Iterable): Enumerable; } } diff --git a/src/extensions/static/generate.ts b/src/extensions/static/generate.ts index 383d092..1eed21d 100644 --- a/src/extensions/static/generate.ts +++ b/src/extensions/static/generate.ts @@ -1,10 +1,14 @@ -import staticGenerate from '../../lifted/static/generate'; -import { Enumerable } from '../../Enumerable'; +import generateFn from '../../static/generate'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable.generate = staticGenerate; +function generate(initialState: TState, condition: (state: TState) => boolean, iterate: (state: TState) => TState, resultSelector: (state: TState) => TResult): Enumerable { + return new Enumerable(generateFn(initialState, condition, iterate, resultSelector)); +} + +Enumerable.generate = generate; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let generate: typeof staticGenerate; + export function generate(initialState: TState, condition: (state: TState) => boolean, iterate: (state: TState) => TState, resultSelector: (state: TState) => TResult): Enumerable; } } diff --git a/src/extensions/static/if.ts b/src/extensions/static/if.ts index fed30d1..b3b9766 100644 --- a/src/extensions/static/if.ts +++ b/src/extensions/static/if.ts @@ -1,10 +1,14 @@ -import staticIf from '../../lifted/static/if'; -import { Enumerable } from '../../Enumerable'; +import ifFn from '../../static/if'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable._if = staticIf; +function _if(condition: () => boolean, thenSource: Iterable, elseSource: Iterable): Enumerable { + return new Enumerable(ifFn(condition, thenSource, elseSource)); +} + +Enumerable._if = _if; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let _if: typeof staticIf; + export function _if(condition: () => boolean, thenSource: Iterable, elseSource: Iterable): Enumerable; } } diff --git a/src/extensions/static/range.ts b/src/extensions/static/range.ts index 246984e..a9a4ecb 100644 --- a/src/extensions/static/range.ts +++ b/src/extensions/static/range.ts @@ -1,10 +1,14 @@ -import staticRange from '../../lifted/static/range'; -import { Enumerable } from '../../Enumerable'; +import rangeFn from '../../static/range'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable.range = staticRange; +function range(start: number, count: number): Enumerable { + return new Enumerable(rangeFn(start, count)); +} + +Enumerable.range = range; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let range: typeof staticRange; + export function range(start: number, count: number): Enumerable; } } diff --git a/src/extensions/static/repeat.ts b/src/extensions/static/repeat.ts index bf3ca2d..b148fc2 100644 --- a/src/extensions/static/repeat.ts +++ b/src/extensions/static/repeat.ts @@ -1,10 +1,14 @@ -import staticRepeat from '../../lifted/static/repeat'; -import { Enumerable } from '../../Enumerable'; +import repeatFn from '../../static/repeat'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable.repeat = staticRepeat; +function repeat(element: TSource, count?: number): Enumerable { + return new Enumerable(repeatFn(element, count)); +} + +Enumerable.repeat = repeat; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let repeat: typeof staticRepeat; + export function repeat(element: TSource, count?: number): Enumerable; } } diff --git a/src/extensions/static/return.ts b/src/extensions/static/return.ts index 42e0821..171a01e 100644 --- a/src/extensions/static/return.ts +++ b/src/extensions/static/return.ts @@ -1,10 +1,14 @@ -import staticReturn from '../../lifted/static/return'; -import { Enumerable } from '../../Enumerable'; +import returnFn from '../../static/return'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable._return = staticReturn; +function _return(element: TSource): Enumerable { + return new Enumerable(returnFn(element)); +} + +Enumerable._return = _return; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let _return: typeof staticReturn; + export function _return(element: TSource): Enumerable; } } diff --git a/src/extensions/static/zip.ts b/src/extensions/static/zip.ts index 76831e0..98fb8bd 100644 --- a/src/extensions/static/zip.ts +++ b/src/extensions/static/zip.ts @@ -1,10 +1,14 @@ -import staticZip from '../../lifted/static/zip'; -import { Enumerable } from '../../Enumerable'; +import zipFn from '../../static/zip'; +import { Enumerable } from '../../internal/Enumerable'; -Enumerable.zip = staticZip; +function zip(first: Iterable, second: Iterable, resultSelector: (first: TFirst, second: TSecond) => TResult): Enumerable { + return new Enumerable(zipFn(first, second, resultSelector)); +} + +Enumerable.zip = zip; -declare module '../../Enumerable' { +declare module '../../internal/Enumerable' { namespace Enumerable { - export let zip: typeof staticZip; + export function zip(first: Iterable, second: Iterable, resultSelector: (first: TFirst, second: TSecond) => TResult): Enumerable; } } diff --git a/src/extensions/sum.ts b/src/extensions/sum.ts index 3bd063d..0f5bde9 100644 --- a/src/extensions/sum.ts +++ b/src/extensions/sum.ts @@ -1,10 +1,10 @@ import sum from '../sum'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.sum = sum; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - sum: typeof sum; + sum(selector?: (element: TSource) => number): number; } } diff --git a/src/extensions/take.ts b/src/extensions/take.ts index 53a6892..574d3b2 100644 --- a/src/extensions/take.ts +++ b/src/extensions/take.ts @@ -1,10 +1,14 @@ -import take from '../lifted/take'; -import { Enumerable } from '../Enumerable'; +import takeFn from '../take'; +import { Enumerable } from '../internal/Enumerable'; + +function take(this: Enumerable, count: number): Enumerable { + return this.lift(takeFn.call(this, count)); +} Enumerable.prototype.take = take; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - take: typeof take; + take(count: number): Enumerable; } } diff --git a/src/extensions/takeLast.ts b/src/extensions/takeLast.ts index 4edf059..882a57c 100644 --- a/src/extensions/takeLast.ts +++ b/src/extensions/takeLast.ts @@ -1,10 +1,14 @@ -import takeLast from '../lifted/takeLast'; -import { Enumerable } from '../Enumerable'; +import takeLastFn from '../takeLast'; +import { Enumerable } from '../internal/Enumerable'; + +function takeLast(this: Enumerable, count: number): Enumerable { + return this.lift(takeLastFn.call(this, count)); +} Enumerable.prototype.takeLast = takeLast; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - takeLast: typeof takeLast; + takeLast(count: number): Enumerable; } } diff --git a/src/extensions/takeWhile.ts b/src/extensions/takeWhile.ts index 6dcab0b..697d0c7 100644 --- a/src/extensions/takeWhile.ts +++ b/src/extensions/takeWhile.ts @@ -1,10 +1,14 @@ -import takeWhile from '../lifted/takeWhile'; -import { Enumerable } from '../Enumerable'; +import takeWhileFn from '../takeWhile'; +import { Enumerable } from '../internal/Enumerable'; + +function takeWhile(this: Enumerable, predicate: (element: TSource) => boolean): Enumerable { + return this.lift(takeWhileFn.call(this, predicate)); +} Enumerable.prototype.takeWhile = takeWhile; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - takeWhile: typeof takeWhile; + takeWhile(predicate: (element: TSource) => boolean): Enumerable; } } diff --git a/src/extensions/toArray.ts b/src/extensions/toArray.ts index cd66936..83e964c 100644 --- a/src/extensions/toArray.ts +++ b/src/extensions/toArray.ts @@ -1,10 +1,10 @@ import toArray from '../toArray'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.toArray = toArray; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - toArray: typeof toArray; + toArray(): TSource[]; } } diff --git a/src/extensions/toDictionary.ts b/src/extensions/toDictionary.ts index a4f1e3b..1755f2d 100644 --- a/src/extensions/toDictionary.ts +++ b/src/extensions/toDictionary.ts @@ -1,10 +1,11 @@ import toDictionary from '../toDictionary'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.toDictionary = toDictionary; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - toDictionary: typeof toDictionary; + toDictionary(keySelector?: (element: TSource) => TKey): Map; + toDictionary(keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map; } } diff --git a/src/extensions/toLookup.ts b/src/extensions/toLookup.ts index 449a79e..b745b46 100644 --- a/src/extensions/toLookup.ts +++ b/src/extensions/toLookup.ts @@ -1,10 +1,11 @@ import toLookup from '../toLookup'; -import { Enumerable } from '../Enumerable'; +import { Enumerable } from '../internal/Enumerable'; Enumerable.prototype.toLookup = toLookup; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - toLookup: typeof toLookup; + toLookup(keySelector?: (element: TSource) => TKey): Map; + toLookup(keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map; } } diff --git a/src/extensions/union.ts b/src/extensions/union.ts index e1d49ae..303bf6b 100644 --- a/src/extensions/union.ts +++ b/src/extensions/union.ts @@ -1,10 +1,14 @@ -import union from '../lifted/union'; -import { Enumerable } from '../Enumerable'; +import unionFn from '../union'; +import { Enumerable } from '../internal/Enumerable'; + +function union(this: Enumerable, second: Iterable): Enumerable { + return this.lift(unionFn.call(this, second)); +} Enumerable.prototype.union = union; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - union: typeof union; + union(second: Iterable): Enumerable; } } diff --git a/src/extensions/where.ts b/src/extensions/where.ts index b4e7247..895c274 100644 --- a/src/extensions/where.ts +++ b/src/extensions/where.ts @@ -1,10 +1,14 @@ -import where from '../lifted/where'; -import { Enumerable } from '../Enumerable'; +import whereFn from '../where'; +import { Enumerable } from '../internal/Enumerable'; + +function where(this: Enumerable, predicate: (item: TSource) => boolean): Enumerable { + return this.lift(whereFn.call(this, predicate)); +} Enumerable.prototype.where = where; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - where: typeof where; + where(predicate: (item: TSource) => boolean): Enumerable; } } diff --git a/src/extensions/while.ts b/src/extensions/while.ts index cbbf1c0..4a852b2 100644 --- a/src/extensions/while.ts +++ b/src/extensions/while.ts @@ -1,10 +1,14 @@ -import _while from '../lifted/while'; -import { Enumerable } from '../Enumerable'; +import whileFn from '../while'; +import { Enumerable } from '../internal/Enumerable'; + +function _while(this: Enumerable, condition: () => boolean): Enumerable { + return this.lift(whileFn.call(this, condition)); +} Enumerable.prototype.while = _while; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - while: typeof _while; + while(condition: () => boolean): Enumerable; } } diff --git a/src/extensions/zip.ts b/src/extensions/zip.ts index 491ba11..0e6e9a0 100644 --- a/src/extensions/zip.ts +++ b/src/extensions/zip.ts @@ -1,10 +1,14 @@ -import zip from '../lifted/zip'; -import { Enumerable } from '../Enumerable'; +import zipFn from '../zip'; +import { Enumerable } from '../internal/Enumerable'; + +function zip(this: Enumerable, second: Iterable, resultSelector: (first: TFirst, second: TSecond) => TResult): Enumerable { + return this.lift(zipFn.call(this, second, resultSelector)); +} Enumerable.prototype.zip = zip; -declare module '../Enumerable' { +declare module '../internal/Enumerable' { interface Enumerable { - zip: typeof zip; + zip(second: Iterable, resultSelector: (first: TSource, second: TSecond) => TResult): Enumerable; } } diff --git a/src/index.ts b/src/index.ts index fe1f20f..3b17c73 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,72 @@ -import { Enumerable } from './Enumerable'; +import { Enumerable } from './internal/Enumerable'; + +import './extensions/aggregate'; +import './extensions/all'; +import './extensions/any'; +import './extensions/average'; +import './extensions/buffer'; +import './extensions/catch'; +import './extensions/concat'; +import './extensions/count'; +import './extensions/defaultIfEmpty'; +import './extensions/distinct'; +import './extensions/distinctUntilChanged'; +import './extensions/do'; +import './extensions/doWhile'; +import './extensions/elementAt'; +import './extensions/elementAtOrDefault'; +import './extensions/except'; +import './extensions/finally'; +import './extensions/first'; +import './extensions/firstOrDefault'; +import './extensions/forEach'; +import './extensions/groupBy'; +import './extensions/groupJoin'; +import './extensions/ignoreElements'; +import './extensions/intersect'; +import './extensions/isEmpty'; +import './extensions/join'; +import './extensions/last'; +import './extensions/lastOrDefault'; +import './extensions/max'; +import './extensions/maxBy'; +import './extensions/memoize'; +import './extensions/min'; +import './extensions/minBy'; +import './extensions/onErrorResumeNext'; +import './extensions/orderBy'; +import './extensions/orderByDescending'; +import './extensions/repeat'; +import './extensions/retry'; +import './extensions/reverse'; +import './extensions/scan'; +import './extensions/select'; +import './extensions/selectMany'; +import './extensions/single'; +import './extensions/singleOrDefault'; +import './extensions/skip'; +import './extensions/skipLast'; +import './extensions/skipWhile'; +import './extensions/startWith'; +import './extensions/static/catch'; +import './extensions/static/concat'; +import './extensions/static/defer'; +import './extensions/static/generate'; +import './extensions/static/if'; +import './extensions/static/range'; +import './extensions/static/repeat'; +import './extensions/static/return'; +import './extensions/static/zip'; +import './extensions/sum'; +import './extensions/take'; +import './extensions/takeLast'; +import './extensions/takeWhile'; +import './extensions/toArray'; +import './extensions/toDictionary'; +import './extensions/toLookup'; +import './extensions/union'; +import './extensions/where'; +import './extensions/while'; +import './extensions/zip'; export default Enumerable; diff --git a/src/internal/EmptyPartition.ts b/src/internal/EmptyPartition.ts index 088b784..c265700 100644 --- a/src/internal/EmptyPartition.ts +++ b/src/internal/EmptyPartition.ts @@ -26,7 +26,7 @@ export default class EmptyPartition extends Partition implem throw noElements(); } - firstOrDefault(defaultValue?: TElement): TElement { + firstOrDefault(defaultValue: TElement = null): TElement { return defaultValue; } @@ -34,7 +34,7 @@ export default class EmptyPartition extends Partition implem throw noElements(); } - lastOrDefault(defaultValue?: TElement): TElement { + lastOrDefault(defaultValue: TElement = null): TElement { return defaultValue; } @@ -42,7 +42,7 @@ export default class EmptyPartition extends Partition implem throw noElements(); } - elementAtOrDefault(index: number, defaultValue?: TElement): TElement { + elementAtOrDefault(index: number, defaultValue: TElement = null): TElement { return defaultValue; } } diff --git a/src/internal/Enumerable.ts b/src/internal/Enumerable.ts new file mode 100644 index 0000000..d2e8f09 --- /dev/null +++ b/src/internal/Enumerable.ts @@ -0,0 +1,12 @@ +export class Enumerable implements Iterable { + constructor(protected _source: Iterable) { + } + + [Symbol.iterator](): Iterator { + return this._source[Symbol.iterator](); + } + + lift(source: Iterable): Enumerable { + return new Enumerable(source); + } +} diff --git a/src/internal/OrderedEnumerable.ts b/src/internal/OrderedEnumerable.ts index d25fe3d..87e31d2 100644 --- a/src/internal/OrderedEnumerable.ts +++ b/src/internal/OrderedEnumerable.ts @@ -3,14 +3,15 @@ import OrderedPartition from './OrderedPartition'; import Partition from './Partition'; import partialQuickSort from './partialQuickSort'; import quickSelect from './quickSelect'; +import { Enumerable } from './Enumerable'; import { noElements } from './errors'; -export default class OrderedEnumerable extends Partition { - constructor(private _source: Iterable, +export default class OrderedEnumerable extends Enumerable { + constructor(_source: Iterable, private _keySelector: (value: TElement) => TKey, private _descending: boolean, private _parent?: OrderedEnumerable) { - super(); + super(_source); } [Symbol.iterator](): Iterator { @@ -25,20 +26,27 @@ export default class OrderedEnumerable extends Partition { - return new OrderedPartition(this, 0, count - 1); + take(count: number): Enumerable { + return new Enumerable(new OrderedPartition(this, 0, count - 1)); } - skip(count: number): Partition { - return new OrderedPartition(this, count, Number.MAX_VALUE); + skip(count: number): Enumerable { + return new Enumerable(new OrderedPartition(this, count, Number.MAX_VALUE)); } - first(): TElement { + first(predicate?: (element: TElement) => boolean): TElement { const iterator = this._source[Symbol.iterator](); - let result = iterator.next(); + if (predicate) { + let result: IteratorResult; + + do { + result = iterator.next(); + if (result.done) { + throw noElements(); + } + } while (!predicate(result.value)); - if (!result.done) { const comparer = this._getComparer(); let value = result.value; @@ -46,25 +54,50 @@ export default class OrderedEnumerable extends Partition boolean, defaultValue: TElement = null): TElement { const iterator = this._source[Symbol.iterator](); - let result = iterator.next(); + if (predicate) { + let result: IteratorResult; + + do { + result = iterator.next(); + if (result.done) { + return defaultValue; + } + } while (!predicate(result.value)); - if (!result.done) { const comparer = this._getComparer(); let value = result.value; @@ -72,25 +105,50 @@ export default class OrderedEnumerable extends Partition boolean): TElement { const iterator = this._source[Symbol.iterator](); - let result = iterator.next(); + if (predicate) { + let result: IteratorResult; + + do { + result = iterator.next(); + if (result.done) { + throw noElements(); + } + } while (!predicate(result.value)); - if (!result.done) { const comparer = this._getComparer(); let value = result.value; @@ -98,25 +156,50 @@ export default class OrderedEnumerable extends Partition 0) { + if (predicate(result.value) && comparer(result.value, value) > 0) { value = result.value; } + result = iterator.next(); + } + return value; + } else { + let result = iterator.next(); + + if (result.done) { + throw noElements(); + } + + const comparer = this._getComparer(); + + let value = result.value; + + result = iterator.next(); + + while (!result.done) { + if (comparer(result.value, value) > 0) { + value = result.value; + } result = iterator.next(); } return value; } - - throw noElements(); } - lastOrDefault(defaultValue?: TElement): TElement { + lastOrDefault(predicate?: (element: TElement) => boolean, defaultValue: TElement = null): TElement { const iterator = this._source[Symbol.iterator](); - let result = iterator.next(); + if (predicate) { + let result: IteratorResult; + + do { + result = iterator.next(); + if (result.done) { + return defaultValue; + } + } while (!predicate(result.value)); - if (!result.done) { const comparer = this._getComparer(); let value = result.value; @@ -124,17 +207,35 @@ export default class OrderedEnumerable extends Partition 0) { + if (predicate(result.value) && comparer(result.value, value) > 0) { value = result.value; } + result = iterator.next(); + } + return value; + } else { + let result = iterator.next(); + + if (result.done) { + return defaultValue; + } + + const comparer = this._getComparer(); + + let value = result.value; + + result = iterator.next(); + + while (!result.done) { + if (comparer(result.value, value) > 0) { + value = result.value; + } result = iterator.next(); } return value; } - - return defaultValue; } lastInPartition(minIndex: number, maxIndex: number): TElement { @@ -159,7 +260,7 @@ export default class OrderedEnumerable extends Partition extends Partition count - 1) return defaultValue; diff --git a/src/internal/OrderedPartition.ts b/src/internal/OrderedPartition.ts index dd3706c..cdc4e13 100644 --- a/src/internal/OrderedPartition.ts +++ b/src/internal/OrderedPartition.ts @@ -31,7 +31,7 @@ export default class OrderedPartition extends Partition { return this._source.elementAt(this._minIndex); } - firstOrDefault(defaultValue?: TElement): TElement { + firstOrDefault(defaultValue: TElement = null): TElement { return this._source.elementAtOrDefault(this._minIndex, defaultValue); } @@ -39,7 +39,7 @@ export default class OrderedPartition extends Partition { return this._source.lastInPartition(this._minIndex, this._maxIndex); } - lastOrDefault(defaultValue?: TElement): TElement { + lastOrDefault(defaultValue: TElement = null): TElement { return this._source.lastInPartitionOrDefault(this._minIndex, this._maxIndex, defaultValue); } @@ -50,7 +50,7 @@ export default class OrderedPartition extends Partition { return this._source.elementAt(index + this._minIndex); } - elementAtOrDefault(index: number, defaultValue?: TElement): TElement { + elementAtOrDefault(index: number, defaultValue: TElement = null): TElement { if (index > this._maxIndex - this._minIndex) { return defaultValue; } diff --git a/src/lifted/buffer.ts b/src/lifted/buffer.ts deleted file mode 100644 index d3599dc..0000000 --- a/src/lifted/buffer.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import bufferFn from '../buffer'; - -export default function buffer(this: Enumerable, count: number, skip?: number): Enumerable { - return this.lift(bufferFn.call(this, count, skip)); -} diff --git a/src/lifted/catch.ts b/src/lifted/catch.ts deleted file mode 100644 index 91cd8d2..0000000 --- a/src/lifted/catch.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import catchFn from '../catch'; - -export default function _catch(this: Enumerable, handler: (exception: TException) => Iterable): Enumerable { - return this.lift(catchFn.call(this, handler)); -} diff --git a/src/lifted/concat.ts b/src/lifted/concat.ts deleted file mode 100644 index b700db6..0000000 --- a/src/lifted/concat.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import concatFn from '../concat'; - -export default function concat(this: Enumerable, ...sources: Iterable[]): Enumerable { - return this.lift(concatFn.apply(this, sources)); -} diff --git a/src/lifted/defaultIfEmpty.ts b/src/lifted/defaultIfEmpty.ts deleted file mode 100644 index 60153e5..0000000 --- a/src/lifted/defaultIfEmpty.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import defaultIfEmptyFn from '../defaultIfEmpty'; - -export default function defaultIfEmpty(this: Enumerable, defaultValue: TSource): Enumerable { - return this.lift(defaultIfEmptyFn.call(this, defaultValue)); -} diff --git a/src/lifted/distinct.ts b/src/lifted/distinct.ts deleted file mode 100644 index da832eb..0000000 --- a/src/lifted/distinct.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import distinctFn from '../distinct'; - -export default function distinct(this: Enumerable): Enumerable; -export default function distinct(this: Enumerable, keySelector?: (element: TSource) => TKey): Enumerable { - return this.lift(distinctFn.call(this, keySelector)); -} diff --git a/src/lifted/distinctUntilChanged.ts b/src/lifted/distinctUntilChanged.ts deleted file mode 100644 index 882bd2f..0000000 --- a/src/lifted/distinctUntilChanged.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import distinctUntilChangedFn from '../distinctUntilChanged'; - -export default function distinctUntilChanged(this: Enumerable): Enumerable; -export default function distinctUntilChanged(this: Enumerable, keySelector?: (element: TSource) => TKey): Enumerable { - return this.lift(distinctUntilChangedFn.call(this, keySelector)); -} diff --git a/src/lifted/do.ts b/src/lifted/do.ts deleted file mode 100644 index 1b28b4f..0000000 --- a/src/lifted/do.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import doFn from '../do'; - -export default function _do(this: Enumerable, action: (element: TSource) => void): Enumerable { - return this.lift(doFn.call(this, action)); -} - diff --git a/src/lifted/doWhile.ts b/src/lifted/doWhile.ts deleted file mode 100644 index 64703ed..0000000 --- a/src/lifted/doWhile.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import doWhileFn from '../doWhile'; - -export default function doWhile(this: Enumerable, condition: () => boolean): Enumerable { - return this.lift(doWhileFn.call(this, condition)); -} diff --git a/src/lifted/except.ts b/src/lifted/except.ts deleted file mode 100644 index f05c541..0000000 --- a/src/lifted/except.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import exceptFn from '../except'; - -export default function except(this: Enumerable, second: Iterable): Enumerable { - return this.lift(exceptFn.call(this, second)); -} diff --git a/src/lifted/finally.ts b/src/lifted/finally.ts deleted file mode 100644 index ccb8b25..0000000 --- a/src/lifted/finally.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import finallyFn from '../finally'; - -export default function _finally(this: Enumerable, finallyAction: () => void): Enumerable { - return this.lift(finallyFn.call(this, finallyAction)); -} diff --git a/src/lifted/groupBy.ts b/src/lifted/groupBy.ts deleted file mode 100644 index d685db1..0000000 --- a/src/lifted/groupBy.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import groupByFn from '../groupBy'; - -export default function groupBy(this: Enumerable, keySelector: (element: TSource) => TKey): Enumerable<[TKey, TSource]>; -export default function groupBy(this: Enumerable, keySelector: (element: TSource) => TKey, elementSelector: (element: TSource) => TElement): Enumerable<[TKey, TElement]>; -export default function groupBy(this: Enumerable, keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement, resultSelector?: (key: TKey, elements: TElement[]) => TResult): Enumerable { - return this.lift(groupByFn.call(this, keySelector, elementSelector, resultSelector)); -} diff --git a/src/lifted/groupJoin.ts b/src/lifted/groupJoin.ts deleted file mode 100644 index fd39138..0000000 --- a/src/lifted/groupJoin.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import groupJoinFn from '../groupJoin'; - -export default function groupJoin(this: Enumerable, inner: Iterable, outerKeySelector: (element: TOuter) => TKey, innerKeySelector: (element: TInner) => TKey, resultSelector: (outer: TOuter, inner: TInner[]) => TResult): Enumerable { - return this.lift(groupJoinFn.call(this, inner, outerKeySelector, innerKeySelector, resultSelector)); -} diff --git a/src/lifted/ignoreElements.ts b/src/lifted/ignoreElements.ts deleted file mode 100644 index a2b11b9..0000000 --- a/src/lifted/ignoreElements.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import ignoreElementsFn from '../ignoreElements'; - -export default function ignoreElements(this: Enumerable): Enumerable { - return this.lift(ignoreElementsFn.call(this)); -} diff --git a/src/lifted/intersect.ts b/src/lifted/intersect.ts deleted file mode 100644 index b216b06..0000000 --- a/src/lifted/intersect.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import intersectFn from '../intersect'; - -export default function intersect(this: Enumerable, second: Iterable): Enumerable { - return this.lift(intersectFn.call(this, second)); -} diff --git a/src/lifted/join.ts b/src/lifted/join.ts deleted file mode 100644 index a042e37..0000000 --- a/src/lifted/join.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import joinFn from '../join'; - -export default function join(this: Enumerable, inner: Iterable, outerKeySelector: (element: TOuter) => TKey, innerKeySelector: (element: TInner) => TKey, resultSelector: (outer: TOuter, inner: TInner) => TResult): Enumerable { - return this.lift(joinFn.call(this, inner, outerKeySelector, innerKeySelector, resultSelector)); -} diff --git a/src/lifted/memoize.ts b/src/lifted/memoize.ts deleted file mode 100644 index 72b1045..0000000 --- a/src/lifted/memoize.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import memoizeFn from '../memoize'; - -export default function memoize(this: Enumerable): Enumerable { - return this.lift(memoizeFn.call(this)); -} diff --git a/src/lifted/onErrorResumeNext.ts b/src/lifted/onErrorResumeNext.ts deleted file mode 100644 index a5f3e36..0000000 --- a/src/lifted/onErrorResumeNext.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import onErrorResumeNextFn from '../onErrorResumeNext'; - -export default function onErrorResumeNext(this: Enumerable, ...sources: Iterable[]): Enumerable { - return this.lift(onErrorResumeNextFn.apply(this, sources)); -} diff --git a/src/lifted/orderBy.ts b/src/lifted/orderBy.ts deleted file mode 100644 index b6947f4..0000000 --- a/src/lifted/orderBy.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import orderByFn from '../orderBy'; - -export default function orderBy(this: Enumerable): Enumerable; -export default function orderBy(this: Enumerable, keySelector?: (value: TSource) => TKey): Enumerable { - return this.lift(orderByFn.call(this, keySelector)); -} diff --git a/src/lifted/orderByDescending.ts b/src/lifted/orderByDescending.ts deleted file mode 100644 index 7a7d77d..0000000 --- a/src/lifted/orderByDescending.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import orderByDescendingFn from '../orderByDescending'; - -export default function orderByDescending(this: Enumerable): Enumerable; -export default function orderByDescending(this: Enumerable, keySelector?: (value: TSource) => TKey): Enumerable { - return this.lift(orderByDescendingFn.call(this, keySelector)); -} diff --git a/src/lifted/repeat.ts b/src/lifted/repeat.ts deleted file mode 100644 index 7f697b1..0000000 --- a/src/lifted/repeat.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import repeatFn from '../repeat'; - -export default function repeat(this: Enumerable, count?: number): Enumerable { - return this.lift(repeatFn.call(this, count)); -} diff --git a/src/lifted/retry.ts b/src/lifted/retry.ts deleted file mode 100644 index 68d3d96..0000000 --- a/src/lifted/retry.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import retryFn from '../retry'; - -export default function retry(this: Enumerable, retryCount?: number): Enumerable { - return this.lift(retryFn.call(this, retryCount)); -} diff --git a/src/lifted/reverse.ts b/src/lifted/reverse.ts deleted file mode 100644 index 41a1d95..0000000 --- a/src/lifted/reverse.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import reverseFn from '../reverse'; - -export default function reverse(this: Enumerable): Enumerable { - return this.lift(reverseFn.call(this)); -} diff --git a/src/lifted/scan.ts b/src/lifted/scan.ts deleted file mode 100644 index 60df73b..0000000 --- a/src/lifted/scan.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import scanFn from '../scan'; - -export default function scan(this: Enumerable, seed: TAccumulate, func: (result: TAccumulate, element: TSource) => TAccumulate): Enumerable { - return this.lift(scanFn.call(this, seed, func)); -} diff --git a/src/lifted/select.ts b/src/lifted/select.ts deleted file mode 100644 index 79a93d8..0000000 --- a/src/lifted/select.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import selectFn from '../select'; - -export default function select(this: Enumerable, selector: (element: TSource) => TResult): Enumerable { - return this.lift(selectFn.call(this, selector)); -} diff --git a/src/lifted/selectMany.ts b/src/lifted/selectMany.ts deleted file mode 100644 index 31d9fcb..0000000 --- a/src/lifted/selectMany.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import selectManyFn from '../selectMany'; - -export default function selectMany(this: Enumerable, collectionSelector: (element: TSource) => Iterable): Enumerable { - return this.lift(selectManyFn.call(this, collectionSelector)); -} diff --git a/src/lifted/skip.ts b/src/lifted/skip.ts deleted file mode 100644 index 585ad9d..0000000 --- a/src/lifted/skip.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import skipFn from '../skip'; - -export default function skip(this: Enumerable, count: number): Enumerable { - return this.lift(skipFn.call(this, count)); -} diff --git a/src/lifted/skipLast.ts b/src/lifted/skipLast.ts deleted file mode 100644 index e59a104..0000000 --- a/src/lifted/skipLast.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import skipLastFn from '../skipLast'; - -export default function skipLast(this: Enumerable, count: number): Enumerable { - return this.lift(skipLastFn.call(this, count)); -} diff --git a/src/lifted/skipWhile.ts b/src/lifted/skipWhile.ts deleted file mode 100644 index 718d909..0000000 --- a/src/lifted/skipWhile.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import skipWhileFn from '../skipWhile'; - -export default function skipWhile(this: Enumerable, predicate: (element: TSource) => boolean): Enumerable { - return this.lift(skipWhileFn.call(this, predicate)); -} diff --git a/src/lifted/startWith.ts b/src/lifted/startWith.ts deleted file mode 100644 index e23f57e..0000000 --- a/src/lifted/startWith.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import startWithFn from '../startWith'; - -export default function startWith(this: Enumerable, ...elements: TSource[]): Enumerable { - return this.lift(startWithFn.apply(this, elements)); -} diff --git a/src/lifted/static/catch.ts b/src/lifted/static/catch.ts deleted file mode 100644 index 6a286b8..0000000 --- a/src/lifted/static/catch.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticCatchFn from '../../static/catch'; -import { Enumerable } from '../../Enumerable'; - -export default function staticCatch(...sources: Iterable[]): Enumerable { - return new Enumerable(staticCatchFn(...sources)); -} diff --git a/src/lifted/static/concat.ts b/src/lifted/static/concat.ts deleted file mode 100644 index f0f6a66..0000000 --- a/src/lifted/static/concat.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticConcatFn from '../../static/concat'; -import { Enumerable } from '../../Enumerable'; - -export default function staticConcat(...sources: Iterable[]): Enumerable { - return new Enumerable(staticConcatFn(...sources)); -} diff --git a/src/lifted/static/defer.ts b/src/lifted/static/defer.ts deleted file mode 100644 index d23688a..0000000 --- a/src/lifted/static/defer.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticDeferFn from '../../static/defer'; -import { Enumerable } from '../../Enumerable'; - -export default function staticDefer(iterableFactory: () => Iterable): Enumerable { - return new Enumerable(staticDeferFn(iterableFactory)); -} diff --git a/src/lifted/static/generate.ts b/src/lifted/static/generate.ts deleted file mode 100644 index 3eb84c8..0000000 --- a/src/lifted/static/generate.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticGenerateFn from '../../static/generate'; -import { Enumerable } from '../../Enumerable'; - -export default function staticGenerate(initialState: TState, condition: (state: TState) => boolean, iterate: (state: TState) => TState, resultSelector: (state: TState) => TResult): Enumerable { - return new Enumerable(staticGenerateFn(initialState, condition, iterate, resultSelector)); -} diff --git a/src/lifted/static/if.ts b/src/lifted/static/if.ts deleted file mode 100644 index 887e387..0000000 --- a/src/lifted/static/if.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticIfFn from '../../static/if'; -import { Enumerable } from '../../Enumerable'; - -export default function staticIf(condition: () => boolean, thenSource: Iterable, elseSource: Iterable): Enumerable { - return new Enumerable(staticIfFn(condition, thenSource, elseSource)); -} diff --git a/src/lifted/static/range.ts b/src/lifted/static/range.ts deleted file mode 100644 index 3be908e..0000000 --- a/src/lifted/static/range.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticRangeFn from '../../static/range'; -import { Enumerable } from '../../Enumerable'; - -export default function staticRange(start: number, count: number): Enumerable { - return new Enumerable(staticRangeFn(start, count)); -} diff --git a/src/lifted/static/repeat.ts b/src/lifted/static/repeat.ts deleted file mode 100644 index 8519d41..0000000 --- a/src/lifted/static/repeat.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticRepeatFn from '../../static/repeat'; -import { Enumerable } from '../../Enumerable'; - -export default function staticRepeat(element: TSource, count?: number): Enumerable { - return new Enumerable(staticRepeatFn(element, count)); -} diff --git a/src/lifted/static/return.ts b/src/lifted/static/return.ts deleted file mode 100644 index 9c62a39..0000000 --- a/src/lifted/static/return.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticReturnFn from '../../static/return'; -import { Enumerable } from '../../Enumerable'; - -export default function staticReturn(element: TSource): Enumerable { - return new Enumerable(staticReturnFn(element)); -} diff --git a/src/lifted/static/zip.ts b/src/lifted/static/zip.ts deleted file mode 100644 index 703c8ac..0000000 --- a/src/lifted/static/zip.ts +++ /dev/null @@ -1,6 +0,0 @@ -import staticZipFn from '../../static/zip'; -import { Enumerable } from '../../Enumerable'; - -export default function staticZip(first: Iterable, second: Iterable, resultSelector: (first: TFirst, second: TSecond) => TResult): Enumerable { - return new Enumerable(staticZipFn(first, second, resultSelector)); -} diff --git a/src/lifted/take.ts b/src/lifted/take.ts deleted file mode 100644 index c7fe457..0000000 --- a/src/lifted/take.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import takeFn from '../take'; - -export default function take(this: Enumerable, count: number): Enumerable { - return this.lift(takeFn.call(this, count)); -} diff --git a/src/lifted/takeLast.ts b/src/lifted/takeLast.ts deleted file mode 100644 index a2a2d21..0000000 --- a/src/lifted/takeLast.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import takeLastFn from '../takeLast'; - -export default function takeLast(this: Enumerable, count: number): Enumerable { - return this.lift(takeLastFn.call(this, count)); -} diff --git a/src/lifted/takeWhile.ts b/src/lifted/takeWhile.ts deleted file mode 100644 index 4b7210d..0000000 --- a/src/lifted/takeWhile.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import takeWhileFn from '../takeWhile'; - -export default function takeWhile(this: Enumerable, predicate: (element: TSource) => boolean): Enumerable { - return this.lift(takeWhileFn.call(this, predicate)); -} diff --git a/src/lifted/union.ts b/src/lifted/union.ts deleted file mode 100644 index a251d2e..0000000 --- a/src/lifted/union.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import unionFn from '../union'; - -export default function union(this: Enumerable, second: Iterable): Enumerable { - return this.lift(unionFn.call(this, second)); -} diff --git a/src/lifted/where.ts b/src/lifted/where.ts deleted file mode 100644 index 5aa5a94..0000000 --- a/src/lifted/where.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import whereFn from '../where'; - -export default function where(this: Enumerable, predicate: (item: TSource) => boolean): Enumerable { - return this.lift(whereFn.call(this, predicate)); -} diff --git a/src/lifted/while.ts b/src/lifted/while.ts deleted file mode 100644 index 08f1a0a..0000000 --- a/src/lifted/while.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import whileFn from '../while'; - -export default function _while(this: Enumerable, condition: () => boolean): Enumerable { - return this.lift(whileFn.call(this, condition)); -} diff --git a/src/lifted/zip.ts b/src/lifted/zip.ts deleted file mode 100644 index 27e738d..0000000 --- a/src/lifted/zip.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Enumerable } from '../Enumerable'; -import zipFn from '../zip'; - -export default function zip(this: Enumerable, second: Iterable, resultSelector: (first: TFirst, second: TSecond) => TResult): Enumerable { - return this.lift(zipFn.call(this, second, resultSelector)); -} diff --git a/src/max.ts b/src/max.ts index 49e7a48..d7ddb17 100644 --- a/src/max.ts +++ b/src/max.ts @@ -1,4 +1,4 @@ -export default function max(this: Iterable, selector: (element: TSource) => number): number { +export default function max(this: Iterable, selector?: (element: TSource) => number): number { if (selector == null) selector = (x) => x as any; let max = -Infinity; for (const element of this) { diff --git a/src/maxBy.ts b/src/maxBy.ts index 535baf4..4d91711 100644 --- a/src/maxBy.ts +++ b/src/maxBy.ts @@ -1,4 +1,4 @@ -export default function maxBy(this: Iterable, keySelector: (element: TSource) => TKey): TSource[] { +export default function maxBy(this: Iterable, keySelector?: (element: TSource) => TKey): TSource[] { const iterator = this[Symbol.iterator]() as Iterator; let result: TSource[] = []; diff --git a/src/min.ts b/src/min.ts index ea5d2a2..ae0c341 100644 --- a/src/min.ts +++ b/src/min.ts @@ -1,4 +1,4 @@ -export default function min(this: Iterable, selector: (element: TSource) => number): number { +export default function min(this: Iterable, selector?: (element: TSource) => number): number { if (selector == null) selector = (x) => x as any; let min = Infinity; for (const element of this) { diff --git a/src/minBy.ts b/src/minBy.ts index a6c2ccf..58501f2 100644 --- a/src/minBy.ts +++ b/src/minBy.ts @@ -1,4 +1,4 @@ -export default function minBy(this: Iterable, keySelector: (element: TSource) => TKey): TSource[] { +export default function minBy(this: Iterable, keySelector?: (element: TSource) => TKey): TSource[] { const iterator = this[Symbol.iterator]() as Iterator; let result: TSource[] = []; diff --git a/src/static/defer.ts b/src/static/defer.ts index f334284..5e6ff41 100644 --- a/src/static/defer.ts +++ b/src/static/defer.ts @@ -1,3 +1,7 @@ -export default function* defer(iterableFactory: () => Iterable): Iterable { - yield* iterableFactory(); +export default function defer(iterableFactory: () => Iterable): Iterable { + return { + [Symbol.iterator]() { + return iterableFactory()[Symbol.iterator](); + } + }; } diff --git a/src/static/range.ts b/src/static/range.ts index 315cfdb..c9764b7 100644 --- a/src/static/range.ts +++ b/src/static/range.ts @@ -27,10 +27,6 @@ class RangePartition extends Partition { } while (current < end); } - get length(): number { - return this._end - this._start; - } - skip(count: number): Partition { if (count >= this._end - this._start) return new EmptyPartition(); return new RangePartition(this._start + count, this._end - this._start - count); diff --git a/src/toDictionary.ts b/src/toDictionary.ts index 0c181b1..619bda7 100644 --- a/src/toDictionary.ts +++ b/src/toDictionary.ts @@ -1,5 +1,5 @@ -export default function toDictionary(this: Iterable, keySelector: (element: TSource) => TKey): Map; -export default function toDictionary(this: Iterable, keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map { +export default function toDictionary(this: Iterable, keySelector?: (element: TSource) => TKey): Map; +export default function toDictionary(this: Iterable, keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map { if (elementSelector == null) elementSelector = x => x as any; const dict = new Map(); diff --git a/src/toLookup.ts b/src/toLookup.ts index b82d034..2eea994 100644 --- a/src/toLookup.ts +++ b/src/toLookup.ts @@ -1,5 +1,5 @@ -export default function toLookup(this: Iterable, keySelector: (element: TSource) => TKey): Map; -export default function toLookup(this: Iterable, keySelector: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map { +export default function toLookup(this: Iterable, keySelector?: (element: TSource) => TKey): Map; +export default function toLookup(this: Iterable, keySelector?: (element: TSource) => TKey, elementSelector?: (element: TSource) => TElement): Map { if (elementSelector == null) elementSelector = x => x as any; const lookup = new Map(); diff --git a/test/aggregateTest.js b/test/aggregateTest.js deleted file mode 100644 index 3f4b1cc..0000000 --- a/test/aggregateTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import aggregate from '../dist/aggregate'; -import assert from 'assert'; - -describe('aggregate()', () => { - it('should applies an accumulator function over a sequence', () => { - assert.strictEqual([1, 2, 3, 4]::aggregate(0, (total, n) => total + n), 10); - assert.strictEqual([]::aggregate(2, (total, n) => total + n), 2); - }); -}); diff --git a/test/aggregateTest.ts b/test/aggregateTest.ts new file mode 100644 index 0000000..55cc0bb --- /dev/null +++ b/test/aggregateTest.ts @@ -0,0 +1,9 @@ +import Enumerable from '../src'; +import * as assert from 'assert'; + +describe('aggregate()', () => { + it('should applies an accumulator function over a sequence', () => { + assert.strictEqual(new Enumerable([1, 2, 3, 4]).aggregate(0, (total, n) => total + n), 10); + assert.strictEqual(new Enumerable([]).aggregate(2, (total, n) => total + n), 2); + }); +}); diff --git a/test/allTest.js b/test/allTest.js deleted file mode 100644 index 54e2bb8..0000000 --- a/test/allTest.js +++ /dev/null @@ -1,13 +0,0 @@ -import all from '../dist/all'; -import assert from 'assert'; - -describe('all()', () => { - it('should determines whether all elements of a sequence satisfy a condition', () => { - assert.strictEqual([1, 2, 3, 4]::all(), true); - assert.strictEqual([2, 4]::all(n => n % 2 === 0), true); - assert.strictEqual([0, 1, 2, 3]::all(n => n % 2 === 0), false); - assert.strictEqual([0, 1, 2, 3]::all(), false); - assert.strictEqual([]::all(), true); - assert.strictEqual([]::all(n => n % 2 === 0), true); - }); -}); diff --git a/test/allTest.ts b/test/allTest.ts new file mode 100644 index 0000000..b73955d --- /dev/null +++ b/test/allTest.ts @@ -0,0 +1,13 @@ +import Enumerable from '../src'; +import * as assert from 'assert'; + +describe('all()', () => { + it('should determines whether all elements of a sequence satisfy a condition', () => { + assert.strictEqual(new Enumerable([1, 2, 3, 4]).all(), true); + assert.strictEqual(new Enumerable([2, 4]).all(n => n % 2 === 0), true); + assert.strictEqual(new Enumerable([0, 1, 2, 3]).all(n => n % 2 === 0), false); + assert.strictEqual(new Enumerable([0, 1, 2, 3]).all(), false); + assert.strictEqual(new Enumerable([]).all(), true); + assert.strictEqual(new Enumerable([]).all(n => n % 2 === 0), true); + }); +}); diff --git a/test/anyTest.js b/test/anyTest.js deleted file mode 100644 index fcc7ea7..0000000 --- a/test/anyTest.js +++ /dev/null @@ -1,13 +0,0 @@ -import any from '../dist/any'; -import assert from 'assert'; - -describe('any()', () => { - it('should determines whether a sequence contains any elements', () => { - assert.strictEqual([0, 1, 2, 3, 4]::any(), true); - assert.strictEqual([1, 2, 3, 4]::any(n => n % 2 === 0), true); - assert.strictEqual([0]::any(), false); - assert.strictEqual([1, 3]::any(n => n % 2 === 0), false); - assert.strictEqual([]::any(), false); - assert.strictEqual([]::any(n => n % 2, 0), false); - }); -}); diff --git a/test/anyTest.ts b/test/anyTest.ts new file mode 100644 index 0000000..ec5b41e --- /dev/null +++ b/test/anyTest.ts @@ -0,0 +1,13 @@ +import Enumerable from '../src'; +import * as assert from 'assert'; + +describe('any()', () => { + it('should determines whether a sequence contains any elements', () => { + assert.strictEqual(new Enumerable([0, 1, 2, 3, 4]).any(), true); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).any(n => n % 2 === 0), true); + assert.strictEqual(new Enumerable([0]).any(), false); + assert.strictEqual(new Enumerable([1, 3]).any(n => n % 2 === 0), false); + assert.strictEqual(new Enumerable([]).any(), false); + assert.strictEqual(new Enumerable([]).any(n => n % 2 === 0), false); + }); +}); diff --git a/test/averageTest.js b/test/averageTest.js deleted file mode 100644 index c8b35f3..0000000 --- a/test/averageTest.js +++ /dev/null @@ -1,14 +0,0 @@ -import assert from 'assert'; -import average from '../dist/average'; - -describe('average()', () => { - it('should computes the average of a sequence of numbers', () => { - assert.strictEqual([0, 1, 2, 3, 4]::average(), 2); - assert.strictEqual([0, 1, 2, 3, 4]::average(n => n * 2), 4); - }); - - it('should throws the exception if the sequence contains no elements', () => { - assert.throws(() => []::average(), Error); - assert.throws(() => []::average(n => n * 2), Error); - }); -}); diff --git a/test/averageTest.ts b/test/averageTest.ts new file mode 100644 index 0000000..e70f87e --- /dev/null +++ b/test/averageTest.ts @@ -0,0 +1,14 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('average()', () => { + it('should computes the average of a sequence of numbers', () => { + assert.strictEqual(new Enumerable([0, 1, 2, 3, 4]).average(), 2); + assert.strictEqual(new Enumerable([0, 1, 2, 3, 4]).average(n => n * 2), 4); + }); + + it('should throws the exception if the sequence contains no elements', () => { + assert.throws(() => new Enumerable([]).average(), Error); + assert.throws(() => new Enumerable([]).average(n => n * 2), Error); + }); +}); diff --git a/test/bufferTest.js b/test/bufferTest.js deleted file mode 100644 index 05c1b87..0000000 --- a/test/bufferTest.js +++ /dev/null @@ -1,20 +0,0 @@ -import assert from 'assert'; -import buffer from '../dist/buffer'; - -describe('buffer()', () => { - it('should generates a sequence of non-overlapping adjacent buffers over the source sequence', () => { - const xs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - assert.deepEqual(Array.from(xs::buffer(3)), [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]); - assert.deepEqual(Array.from(xs::buffer(5)), [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]); - assert.deepEqual(Array.from([]::buffer(5)), []); - assert.deepEqual(Array.from(xs::buffer(3, 2)), [[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8], [8, 9]]); - assert.deepEqual(Array.from(xs::buffer(3, 4)), [[0, 1, 2], [4, 5, 6], [8, 9]]); - }); - - it('should throws the exception if "count" or "skip" arguments is less than or equal to 0', () => { - assert.throws(() => Array.from([]::buffer(0)), RangeError); - assert.throws(() => Array.from([]::buffer(1, 0)), RangeError); - assert.throws(() => Array.from([]::buffer(0, 1)), RangeError); - assert.throws(() => Array.from([]::buffer(0, 0)), RangeError); - }); -}); diff --git a/test/bufferTest.ts b/test/bufferTest.ts new file mode 100644 index 0000000..8c282c7 --- /dev/null +++ b/test/bufferTest.ts @@ -0,0 +1,20 @@ +import * as assert from 'assert'; +import buffer from '../src/buffer'; + +describe('buffer()', () => { + it('should generates a sequence of non-overlapping adjacent buffers over the source sequence', () => { + const xs = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + assert.deepEqual(Array.from(buffer.call(xs, 3)), [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]); + assert.deepEqual(Array.from(buffer.call(xs, 5)), [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]); + assert.deepEqual(Array.from(buffer.call([], 5)), []); + assert.deepEqual(Array.from(buffer.call(xs, 3, 2)), [[0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8], [8, 9]]); + assert.deepEqual(Array.from(buffer.call(xs, 3, 4)), [[0, 1, 2], [4, 5, 6], [8, 9]]); + }); + + it('should throws the exception if "count" or "skip" arguments is less than or equal to 0', () => { + assert.throws(() => Array.from(buffer.call([], 0)), RangeError); + assert.throws(() => Array.from(buffer.call([], 1, 0)), RangeError); + assert.throws(() => Array.from(buffer.call([], 0, 1)), RangeError); + assert.throws(() => Array.from(buffer.call([], 0, 0)), RangeError); + }); +}); diff --git a/test/catchTest.js b/test/catchTest.ts similarity index 55% rename from test/catchTest.js rename to test/catchTest.ts index d22fdbb..78bf2cc 100644 --- a/test/catchTest.js +++ b/test/catchTest.ts @@ -1,28 +1,28 @@ -import _catch from '../dist/catch'; -import assert from 'assert'; -import sinon from 'sinon'; +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import Enumerable from '../src'; describe('catch()', () => { it('should creates a sequence that corresponds to the source sequence', () => { const xs = [1, 2, 3]; const ys = [4, 5, 6]; - const handler = sinon.spy(e => ys); - assert.deepEqual(Array.from(xs::_catch(handler)), [1, 2, 3]); + const handler = sinon.spy((e: Error) => ys); + assert.deepEqual(new Enumerable(xs).catch(handler).toArray(), [1, 2, 3]); sinon.assert.notCalled(handler); }); it('should concatenate it with the sequence resulting from calling an exception handler function in case of an error', () => { const xs = { [Symbol.iterator]: function*() { - yield 1 - yield 2 - yield 3 - throw new Error() + yield 1; + yield 2; + yield 3; + throw new Error(); } }; const ys = [4, 5, 6]; - const handler = sinon.spy(e => ys); - assert.deepEqual(Array.from(xs::_catch(handler)), [1, 2, 3, 4, 5, 6]); + const handler = sinon.spy((e: Error) => ys); + assert.deepEqual(new Enumerable(xs).catch(handler).toArray(), [1, 2, 3, 4, 5, 6]); sinon.assert.called(handler); sinon.assert.calledWith(handler, sinon.match.instanceOf(Error)); }); diff --git a/test/concatTest.js b/test/concatTest.js deleted file mode 100644 index 7c777a9..0000000 --- a/test/concatTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import concat from '../dist/concat'; - -describe('concat()', () => { - it('should concatenates two sequences', () => { - assert.deepEqual(Array.from([]::concat([])), []); - assert.deepEqual(Array.from([1, 2, 3]::concat([4, 5, 6])), [1, 2, 3, 4, 5, 6]); - assert.deepEqual(Array.from([1, 2, 3]::concat([4, 5, 6], [7, 8, 9])), [1, 2, 3, 4, 5, 6, 7, 8, 9]); - }); -}); diff --git a/test/concatTest.ts b/test/concatTest.ts new file mode 100644 index 0000000..667e7e5 --- /dev/null +++ b/test/concatTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import concat from '../src/concat'; + +describe('concat()', () => { + it('should concatenates two sequences', () => { + assert.deepEqual(Array.from(concat.call([], [])), []); + assert.deepEqual(Array.from(concat.call([1, 2, 3], [4, 5, 6])), [1, 2, 3, 4, 5, 6]); + assert.deepEqual(Array.from(concat.call([1, 2, 3], [4, 5, 6], [7, 8, 9])), [1, 2, 3, 4, 5, 6, 7, 8, 9]); + }); +}); diff --git a/test/countTest.js b/test/countTest.js deleted file mode 100644 index 22716aa..0000000 --- a/test/countTest.js +++ /dev/null @@ -1,11 +0,0 @@ -import assert from 'assert'; -import count from '../dist/count'; - -describe('count()', () => { - it('should returns the number of elements in a sequence', () => { - assert.strictEqual([1, 2, 3, 4]::count(), 4); - assert.strictEqual([1, 2, 3, 4][Symbol.iterator]()::count(), 4); - assert.strictEqual([]::count(), 0); - assert.strictEqual([1, 2, 3, 4]::count(n => n % 2 === 0), 2); - }); -}); diff --git a/test/countTest.ts b/test/countTest.ts new file mode 100644 index 0000000..bc105f4 --- /dev/null +++ b/test/countTest.ts @@ -0,0 +1,11 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('count()', () => { + it('should returns the number of elements in a sequence', () => { + assert.strictEqual(new Enumerable([1, 2, 3, 4]).count(), 4); + assert.strictEqual(new Enumerable([1, 2, 3, 4][Symbol.iterator]()).count(), 4); + assert.strictEqual(new Enumerable([]).count(), 0); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).count(n => n % 2 === 0), 2); + }); +}); diff --git a/test/defaultIfEmptyTest.js b/test/defaultIfEmptyTest.js deleted file mode 100644 index c232317..0000000 --- a/test/defaultIfEmptyTest.js +++ /dev/null @@ -1,12 +0,0 @@ -import assert from 'assert'; -import defaultIfEmpty from '../dist/defaultIfEmpty'; - -describe('defaultIfEmpty()', () => { - it('should returns the elements of the specified sequence', () => { - assert.deepEqual(Array.from([1, 2, 3]::defaultIfEmpty(123)), [1, 2, 3]); - }); - - it('should returns the specified value in a singleton collection if the sequence is empty', () => { - assert.deepEqual(Array.from([]::defaultIfEmpty(123)), [123]); - }); -}); diff --git a/test/defaultIfEmptyTest.ts b/test/defaultIfEmptyTest.ts new file mode 100644 index 0000000..8a48982 --- /dev/null +++ b/test/defaultIfEmptyTest.ts @@ -0,0 +1,12 @@ +import * as assert from 'assert'; +import defaultIfEmpty from '../src/defaultIfEmpty'; + +describe('defaultIfEmpty()', () => { + it('should returns the elements of the specified sequence', () => { + assert.deepEqual(Array.from(defaultIfEmpty.call([1, 2, 3], 123)), [1, 2, 3]); + }); + + it('should returns the specified value in a singleton collection if the sequence is empty', () => { + assert.deepEqual(Array.from(defaultIfEmpty.call([], 123)), [123]); + }); +}); diff --git a/test/distinctTest.js b/test/distinctTest.js deleted file mode 100644 index fe7c395..0000000 --- a/test/distinctTest.js +++ /dev/null @@ -1,11 +0,0 @@ -import assert from 'assert'; -import distinct from '../dist/distinct'; - -describe('distinct()', () => { - it('should returns distinct elements from a sequence', () => { - assert.deepEqual(Array.from([1, 2, 1, 2]::distinct()), [1, 2]); - assert.deepEqual(Array.from([1, 1, 2, 2]::distinct()), [1, 2]); - assert.deepEqual(Array.from([1, 2, 3, 4]::distinct(x => x % 2 === 0)), [1, 2]); - assert.deepEqual(Array.from([1, 3, 2, 4]::distinct(x => x % 2 === 0)), [1, 2]); - }); -}); diff --git a/test/distinctTest.ts b/test/distinctTest.ts new file mode 100644 index 0000000..8fb7ad0 --- /dev/null +++ b/test/distinctTest.ts @@ -0,0 +1,11 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('distinct()', () => { + it('should returns distinct elements from a sequence', () => { + assert.deepEqual(new Enumerable([1, 2, 1, 2]).distinct().toArray(), [1, 2]); + assert.deepEqual(new Enumerable([1, 1, 2, 2]).distinct().toArray(), [1, 2]); + assert.deepEqual(new Enumerable([1, 2, 3, 4]).distinct(x => x % 2 === 0).toArray(), [1, 2]); + assert.deepEqual(new Enumerable([1, 3, 2, 4]).distinct(x => x % 2 === 0).toArray(), [1, 2]); + }); +}); diff --git a/test/distinctUntilChangedTest.js b/test/distinctUntilChangedTest.js deleted file mode 100644 index f2e43e1..0000000 --- a/test/distinctUntilChangedTest.js +++ /dev/null @@ -1,12 +0,0 @@ -import assert from 'assert'; -import distinctUntilChanged from '../dist/distinctUntilChanged'; - -describe('distinctUntilChanged()', () => { - it('should returns consecutive distinct elements', () => { - assert.deepEqual(Array.from([1, 2, 1, 2]::distinctUntilChanged()), [1, 2, 1, 2]); - assert.deepEqual(Array.from([1, 1, 2, 2]::distinctUntilChanged()), [1, 2]); - assert.deepEqual(Array.from([1, 2, 3, 4]::distinctUntilChanged(x => x % 2 === 0)), [1, 2, 3, 4]); - assert.deepEqual(Array.from([1, 3, 2, 4]::distinctUntilChanged(x => x % 2 === 0)), [1, 2]); - }); -}); - diff --git a/test/distinctUntilChangedTest.ts b/test/distinctUntilChangedTest.ts new file mode 100644 index 0000000..e6327b2 --- /dev/null +++ b/test/distinctUntilChangedTest.ts @@ -0,0 +1,13 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('distinctUntilChanged()', () => { + it('should returns consecutive distinct elements', () => { + assert.deepEqual(new Enumerable([1, 2, 1, 2]).distinctUntilChanged().toArray(), [1, 2, 1, 2]); + assert.deepEqual(new Enumerable([1, 1, 2, 2]).distinctUntilChanged().toArray(), [1, 2]); + assert.deepEqual(new Enumerable([1, 2, 3, 4]).distinctUntilChanged(x => x % 2 === 0).toArray(), [1, 2, 3, 4]); + assert.deepEqual(new Enumerable([1, 3, 2, 4]).distinctUntilChanged(x => x % 2 === 0).toArray(), [1, 2]); + }); +}); + + diff --git a/test/doTest.js b/test/doTest.js deleted file mode 100644 index 336b44c..0000000 --- a/test/doTest.js +++ /dev/null @@ -1,17 +0,0 @@ -import _do from '../dist/do'; -import assert from 'assert'; -import sinon from 'sinon'; - -describe('do()', () => { - it('should lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination', () => { - const spy = sinon.spy(); - const result = [1, 2, 3, 4]::_do(spy); - sinon.assert.notCalled(spy); - assert.deepEqual(Array.from(result), [1, 2, 3, 4]); - sinon.assert.callCount(spy, 4); - sinon.assert.calledWith(spy.getCall(0), 1); - sinon.assert.calledWith(spy.getCall(1), 2); - sinon.assert.calledWith(spy.getCall(2), 3); - sinon.assert.calledWith(spy.getCall(3), 4); - }); -}); diff --git a/test/doTest.ts b/test/doTest.ts new file mode 100644 index 0000000..d3e81f0 --- /dev/null +++ b/test/doTest.ts @@ -0,0 +1,17 @@ +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import Enumerable from '../src'; + +describe('do()', () => { + it('should lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination', () => { + const spy = sinon.spy(); + const result = new Enumerable([1, 2, 3, 4]).do(spy); + sinon.assert.notCalled(spy); + assert.deepEqual(Array.from(result), [1, 2, 3, 4]); + sinon.assert.callCount(spy, 4); + sinon.assert.calledWith(spy.getCall(0) as sinon.SinonSpy, 1); + sinon.assert.calledWith(spy.getCall(1) as sinon.SinonSpy, 2); + sinon.assert.calledWith(spy.getCall(2) as sinon.SinonSpy, 3); + sinon.assert.calledWith(spy.getCall(3) as sinon.SinonSpy, 4); + }); +}); diff --git a/test/doWhileTest.js b/test/doWhileTest.ts similarity index 51% rename from test/doWhileTest.js rename to test/doWhileTest.ts index 4dc6b89..10881e3 100644 --- a/test/doWhileTest.js +++ b/test/doWhileTest.ts @@ -1,15 +1,15 @@ -import assert from 'assert'; -import doWhile from '../dist/doWhile'; +import * as assert from 'assert'; +import Enumerable from '../src'; describe('doWhile()', () => { it('should generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds', () => { let x = 0; const xs = { [Symbol.iterator]: function*() { - yield x++ + yield x++; } }; - assert.deepEqual(Array.from(xs::doWhile(() => x < 5)), [0, 1, 2, 3, 4]); - assert.deepEqual(Array.from(xs::doWhile(() => x < 5)), [5]); + assert.deepEqual(new Enumerable(xs).doWhile(() => x < 5).toArray(), [0, 1, 2, 3, 4]); + assert.deepEqual(new Enumerable(xs).doWhile(() => x < 5).toArray(), [5]); }); }); diff --git a/test/elementAtOrDefaultTest.js b/test/elementAtOrDefaultTest.js deleted file mode 100644 index 5234e70..0000000 --- a/test/elementAtOrDefaultTest.js +++ /dev/null @@ -1,43 +0,0 @@ -import assert from 'assert'; -import elementAtOrDefault from '../dist/elementAtOrDefault'; -import orderBy from '../dist/orderBy'; -import skip from '../dist/skip'; -import take from '../dist/take'; - -describe('elementAtOrDefault()', () => { - it('should returns the element at a specified index in a sequence', () => { - const xs = [1, 2, 3, 4]; - - assert.strictEqual(xs::elementAtOrDefault(0), 1); - assert.strictEqual(xs::elementAtOrDefault(1), 2); - assert.strictEqual(xs::elementAtOrDefault(2), 3); - assert.strictEqual(xs::elementAtOrDefault(3), 4); - }); - - it('should returns a default value if the index is out of range', () => { - const xs = [1, 2, 3, 4]; - - assert.strictEqual(xs::elementAtOrDefault(4, 123), 123); - assert.strictEqual(xs::elementAtOrDefault(4), null); - }); - - it('should works with orderBy()', () => { - const xs = [3, 2, 4, 1]; - - assert.strictEqual(xs::orderBy()::elementAtOrDefault(0), 1); - assert.strictEqual(xs::orderBy()::elementAtOrDefault(1), 2); - assert.strictEqual(xs::orderBy()::elementAtOrDefault(2), 3); - assert.strictEqual(xs::orderBy()::elementAtOrDefault(3), 4); - assert.strictEqual(xs::orderBy()::take(2)::elementAtOrDefault(0), 1); - assert.strictEqual(xs::orderBy()::take(2)::elementAtOrDefault(1), 2); - assert.strictEqual(xs::orderBy()::skip(2)::elementAtOrDefault(0), 3); - assert.strictEqual(xs::orderBy()::skip(2)::elementAtOrDefault(1), 4); - assert.strictEqual(xs::orderBy()::take(2)::skip(1)::elementAtOrDefault(0), 2); - assert.strictEqual(xs::orderBy()::skip(2)::take(1)::elementAtOrDefault(0), 3); - - assert.strictEqual(xs::orderBy()::elementAtOrDefault(4, 123), 123); - assert.strictEqual(xs::orderBy()::take(2)::elementAtOrDefault(3, 123), 123); - assert.strictEqual(xs::orderBy()::take(2)::skip(1)::elementAtOrDefault(1, 123), 123); - assert.strictEqual(xs::orderBy()::skip(2)::take(1)::elementAtOrDefault(1, 123), 123); - }); -}); diff --git a/test/elementAtOrDefaultTest.ts b/test/elementAtOrDefaultTest.ts new file mode 100644 index 0000000..bd0b291 --- /dev/null +++ b/test/elementAtOrDefaultTest.ts @@ -0,0 +1,43 @@ +import * as assert from 'assert'; +import elementAtOrDefault from '../src/elementAtOrDefault'; +import orderBy from '../src/orderBy'; +import skip from '../src/skip'; +import take from '../src/take'; + +describe('elementAtOrDefault()', () => { + it('should returns the element at a specified index in a sequence', () => { + const xs = [1, 2, 3, 4]; + + assert.strictEqual(elementAtOrDefault.call(xs, 0), 1); + assert.strictEqual(elementAtOrDefault.call(xs, 1), 2); + assert.strictEqual(elementAtOrDefault.call(xs, 2), 3); + assert.strictEqual(elementAtOrDefault.call(xs, 3), 4); + }); + + it('should returns a default value if the index is out of range', () => { + const xs = [1, 2, 3, 4]; + + assert.strictEqual(elementAtOrDefault.call(xs, 4, 123), 123); + assert.strictEqual(elementAtOrDefault.call(xs, 4), null); + }); + + it('should works with orderBy()', () => { + const xs = [3, 2, 4, 1]; + + assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 0), 1); + assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 1), 2); + assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 2), 3); + assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 3), 4); + assert.strictEqual(elementAtOrDefault.call(take.call(orderBy.call(xs), 2), 0), 1); + assert.strictEqual(elementAtOrDefault.call(take.call(orderBy.call(xs), 2), 1), 2); + assert.strictEqual(elementAtOrDefault.call(skip.call(orderBy.call(xs), 2), 0), 3); + assert.strictEqual(elementAtOrDefault.call(skip.call(orderBy.call(xs), 2), 1), 4); + assert.strictEqual(elementAtOrDefault.call(skip.call(take.call(orderBy.call(xs), 2), 1), 0), 2); + assert.strictEqual(elementAtOrDefault.call(take.call(skip.call(orderBy.call(xs), 2), 1), 0), 3); + + assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 4, 123), 123); + assert.strictEqual(elementAtOrDefault.call(take.call(orderBy.call(xs), 2), 3, 123), 123); + assert.strictEqual(elementAtOrDefault.call(skip.call(take.call(orderBy.call(xs), 2), 1), 1, 123), 123); + assert.strictEqual(elementAtOrDefault.call(take.call(skip.call(orderBy.call(xs), 2), 1), 1, 123), 123); + }); +}); diff --git a/test/elementAtTest.js b/test/elementAtTest.js deleted file mode 100644 index 93ede64..0000000 --- a/test/elementAtTest.js +++ /dev/null @@ -1,42 +0,0 @@ -import assert from 'assert'; -import elementAt from '../dist/elementAt'; -import orderBy from '../dist/orderBy'; -import skip from '../dist/skip'; -import take from '../dist/take'; - -describe('elementAt()', () => { - it('should returns the element at a specified index in a sequence', () => { - const xs = [1, 2, 3, 4]; - - assert.strictEqual(xs::elementAt(0), 1); - assert.strictEqual(xs::elementAt(1), 2); - assert.strictEqual(xs::elementAt(2), 3); - assert.strictEqual(xs::elementAt(3), 4); - }); - - it('should throws the exception if index is out of range', () => { - const xs = [1, 2, 3, 4]; - - assert.throws(() => xs::elementAt(4)); - }); - - it('should works with orderBy()', () => { - const xs = [3, 2, 4, 1]; - - assert.strictEqual(xs::orderBy()::elementAt(0), 1); - assert.strictEqual(xs::orderBy()::elementAt(1), 2); - assert.strictEqual(xs::orderBy()::elementAt(2), 3); - assert.strictEqual(xs::orderBy()::elementAt(3), 4); - assert.strictEqual(xs::orderBy()::take(2)::elementAt(0), 1); - assert.strictEqual(xs::orderBy()::take(2)::elementAt(1), 2); - assert.strictEqual(xs::orderBy()::skip(2)::elementAt(0), 3); - assert.strictEqual(xs::orderBy()::skip(2)::elementAt(1), 4); - assert.strictEqual(xs::orderBy()::take(2)::skip(1)::elementAt(0), 2); - assert.strictEqual(xs::orderBy()::skip(2)::take(1)::elementAt(0), 3); - - assert.throws(() => xs::orderBy()::elementAt(4)); - assert.throws(() => xs::orderBy()::take(2)::elementAt(3)); - assert.throws(() => xs::orderBy()::take(2)::skip(1)::elementAt(1)); - assert.throws(() => xs::orderBy()::skip(2)::take(1)::elementAt(1)); - }); -}); diff --git a/test/elementAtTest.ts b/test/elementAtTest.ts new file mode 100644 index 0000000..923bdb5 --- /dev/null +++ b/test/elementAtTest.ts @@ -0,0 +1,42 @@ +import * as assert from 'assert'; +import elementAt from '../src/elementAt'; +import orderBy from '../src/orderBy'; +import skip from '../src/skip'; +import take from '../src/take'; + +describe('elementAt()', () => { + it('should returns the element at a specified index in a sequence', () => { + const xs = [1, 2, 3, 4]; + + assert.strictEqual(elementAt.call(xs, 0), 1); + assert.strictEqual(elementAt.call(xs, 1), 2); + assert.strictEqual(elementAt.call(xs, 2), 3); + assert.strictEqual(elementAt.call(xs, 3), 4); + }); + + it('should throws the exception if index is out of range', () => { + const xs = [1, 2, 3, 4]; + + assert.throws(() => elementAt.call(xs, 4)); + }); + + it('should works with orderBy()', () => { + const xs = [3, 2, 4, 1]; + + assert.strictEqual(elementAt.call(orderBy.call(xs), 0), 1); + assert.strictEqual(elementAt.call(orderBy.call(xs), 1), 2); + assert.strictEqual(elementAt.call(orderBy.call(xs), 2), 3); + assert.strictEqual(elementAt.call(orderBy.call(xs), 3), 4); + assert.strictEqual(elementAt.call(take.call(orderBy.call(xs), 2), 0), 1); + assert.strictEqual(elementAt.call(take.call(orderBy.call(xs), 2), 1), 2); + assert.strictEqual(elementAt.call(skip.call(orderBy.call(xs), 2), 0), 3); + assert.strictEqual(elementAt.call(skip.call(orderBy.call(xs), 2), 1), 4); + assert.strictEqual(elementAt.call(skip.call(take.call(orderBy.call(xs), 2), 1), 0), 2); + assert.strictEqual(elementAt.call(take.call(skip.call(orderBy.call(xs), 2), 1), 0), 3); + + assert.throws(() => elementAt.call(orderBy.call(xs), 4)); + assert.throws(() => elementAt.call(take.call(orderBy.call(xs), 2), 3)); + assert.throws(() => elementAt.call(skip.call(take.call(orderBy.call(xs), 2), 1), 1)); + assert.throws(() => elementAt.call(take.call(skip.call(orderBy.call(xs), 2), 1), 1)); + }); +}); diff --git a/test/exceptTest.js b/test/exceptTest.js deleted file mode 100644 index 7968242..0000000 --- a/test/exceptTest.js +++ /dev/null @@ -1,11 +0,0 @@ -import assert from 'assert'; -import except from '../dist/except'; - -describe('except()', () => { - it('should produces the set difference of two sequences', () => { - assert.deepEqual(Array.from([]::except([])), []); - assert.deepEqual(Array.from([1, 2, 3, 4, 5, 6]::except([2, 3, 4])), [1, 5, 6]); - assert.deepEqual(Array.from([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]::except([2, 3, 4])), [1, 5, 6]); - assert.deepEqual(Array.from([2, 3, 4]::except([1, 2, 3, 4, 5, 6])), []); - }); -}); diff --git a/test/exceptTest.ts b/test/exceptTest.ts new file mode 100644 index 0000000..5258a46 --- /dev/null +++ b/test/exceptTest.ts @@ -0,0 +1,11 @@ +import * as assert from 'assert'; +import except from '../src/except'; + +describe('except()', () => { + it('should produces the set difference of two sequences', () => { + assert.deepEqual(Array.from(except.call([], [])), []); + assert.deepEqual(Array.from(except.call([1, 2, 3, 4, 5, 6], [2, 3, 4])), [1, 5, 6]); + assert.deepEqual(Array.from(except.call([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6], [2, 3, 4])), [1, 5, 6]); + assert.deepEqual(Array.from(except.call([2, 3, 4], [1, 2, 3, 4, 5, 6])), []); + }); +}); diff --git a/test/finallyTest.js b/test/finallyTest.ts similarity index 68% rename from test/finallyTest.js rename to test/finallyTest.ts index fe9e604..cc874e0 100644 --- a/test/finallyTest.js +++ b/test/finallyTest.ts @@ -1,12 +1,12 @@ -import _finally from '../dist/finally'; -import assert from 'assert'; -import sinon from 'sinon'; +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import _finally from '../src/finally'; describe('finally()', () => { it('should creates a sequence whose termination or disposal of an enumerator causes a finally action to be executed', () => { const source1 = [1, 2, 3]; const finallyAction1 = sinon.spy(); - assert.deepEqual(Array.from(source1::_finally(finallyAction1)), [1, 2, 3]); + assert.deepEqual(Array.from(_finally.call(source1, finallyAction1)), [1, 2, 3]); sinon.assert.called(finallyAction1); const source2 = { @@ -18,7 +18,7 @@ describe('finally()', () => { } }; const finallyAction2 = sinon.spy(); - assert.throws(() => Array.from(source2::_finally(finallyAction2))); + assert.throws(() => Array.from(_finally.call(source2, finallyAction2))); sinon.assert.called(finallyAction2); }); }); diff --git a/test/firstOrDefaultTest.js b/test/firstOrDefaultTest.js deleted file mode 100644 index 3b0ded5..0000000 --- a/test/firstOrDefaultTest.js +++ /dev/null @@ -1,28 +0,0 @@ -import assert from 'assert'; -import firstOrDefault from '../dist/firstOrDefault'; -import orderBy from '../dist/orderBy'; -import skip from '../dist/skip'; -import take from '../dist/take'; - -describe('firstOrDefault()', () => { - it('should returns the first element of a sequence', () => { - assert.strictEqual([1, 2, 3, 4]::firstOrDefault(), 1); - assert.strictEqual([1, 2, 3, 4]::firstOrDefault(x => x % 2 === 0), 2); - }); - - it('should returns a default value if the sequence contains no elements', () => { - assert.strictEqual([]::firstOrDefault(), null); - assert.strictEqual([]::firstOrDefault(null, 123), 123); - assert.strictEqual([1, 2, 3, 4]::firstOrDefault(x => x > 10), null); - assert.strictEqual([1, 2, 3, 4]::firstOrDefault(x => x > 10, 123), 123); - }); - - it('should works with orderBy()', () => { - const xs = [3, 2, 4, 1]; - assert.strictEqual(xs::orderBy()::firstOrDefault(), 1); - assert.strictEqual(xs::orderBy()::take(2)::firstOrDefault(), 1); - assert.strictEqual(xs::orderBy()::skip(2)::firstOrDefault(), 3); - assert.strictEqual(xs::orderBy()::take(2)::skip(1)::firstOrDefault(), 2); - assert.strictEqual(xs::orderBy()::skip(2)::take(1)::firstOrDefault(), 3); - }); -}); diff --git a/test/firstOrDefaultTest.ts b/test/firstOrDefaultTest.ts new file mode 100644 index 0000000..ab2ff2e --- /dev/null +++ b/test/firstOrDefaultTest.ts @@ -0,0 +1,26 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('firstOrDefault()', () => { + it('should returns the first element of a sequence', () => { + assert.strictEqual(new Enumerable([1, 2, 3, 4]).firstOrDefault(), 1); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).firstOrDefault(x => x % 2 === 0), 2); + }); + + it('should returns a default value if the sequence contains no elements', () => { + assert.strictEqual(new Enumerable([]).firstOrDefault(), null); + assert.strictEqual(new Enumerable([]).firstOrDefault(null, 123), 123); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).firstOrDefault(x => x > 10), null); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).firstOrDefault(x => x > 10, 123), 123); + }); + + it('should works with orderBy()', () => { + const xs = [3, 2, 4, 1]; + assert.strictEqual(new Enumerable(xs).orderBy().firstOrDefault(), 1); + assert.strictEqual(new Enumerable(xs).orderBy().firstOrDefault(x => x % 2 === 0), 2); + assert.strictEqual(new Enumerable(xs).orderBy().take(2).firstOrDefault(), 1); + assert.strictEqual(new Enumerable(xs).orderBy().skip(2).firstOrDefault(), 3); + assert.strictEqual(new Enumerable(xs).orderBy().take(2).skip(1).firstOrDefault(), 2); + assert.strictEqual(new Enumerable(xs).orderBy().skip(2).take(1).firstOrDefault(), 3); + }); +}); diff --git a/test/firstTest.js b/test/firstTest.js deleted file mode 100644 index f87b2b1..0000000 --- a/test/firstTest.js +++ /dev/null @@ -1,26 +0,0 @@ -import assert from 'assert'; -import first from '../dist/first'; -import orderBy from '../dist/orderBy'; -import skip from '../dist/skip'; -import take from '../dist/take'; - -describe('first()', () => { - it('should returns the first element of a sequence', () => { - assert.strictEqual([1, 2, 3, 4]::first(), 1); - assert.strictEqual([1, 2, 3, 4]::first(x => x % 2 === 0), 2); - }); - - it('should throws the exception if the sequence contains no elements', () => { - assert.throws(() => []::first()); - assert.throws(() => [1, 2, 3, 4]::first(x => x > 10)); - }); - - it('should works with orderBy()', () => { - const xs = [3, 2, 4, 1]; - assert.strictEqual(xs::orderBy()::first(), 1); - assert.strictEqual(xs::orderBy()::take(2)::first(), 1); - assert.strictEqual(xs::orderBy()::skip(2)::first(), 3); - assert.strictEqual(xs::orderBy()::take(2)::skip(1)::first(), 2); - assert.strictEqual(xs::orderBy()::skip(2)::take(1)::first(), 3); - }); -}); diff --git a/test/firstTest.ts b/test/firstTest.ts new file mode 100644 index 0000000..50581d5 --- /dev/null +++ b/test/firstTest.ts @@ -0,0 +1,24 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('first()', () => { + it('should returns the first element of a sequence', () => { + assert.strictEqual(new Enumerable([1, 2, 3, 4]).first(), 1); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).first(x => x % 2 === 0), 2); + }); + + it('should throws the exception if the sequence contains no elements', () => { + assert.throws(() => new Enumerable([]).first()); + assert.throws(() => new Enumerable([1, 2, 3, 4]).first(x => x > 10)); + }); + + it('should works with orderBy()', () => { + const xs = [3, 2, 4, 1]; + assert.strictEqual(new Enumerable(xs).orderBy().first(), 1); + assert.strictEqual(new Enumerable(xs).orderBy().first(x => x % 2 === 0), 2); + assert.strictEqual(new Enumerable(xs).orderBy().take(2).first(), 1); + assert.strictEqual(new Enumerable(xs).orderBy().skip(2).first(), 3); + assert.strictEqual(new Enumerable(xs).orderBy().take(2).skip(1).first(), 2); + assert.strictEqual(new Enumerable(xs).orderBy().skip(2).take(1).first(), 3); + }); +}); diff --git a/test/forEachTest.js b/test/forEachTest.js deleted file mode 100644 index d19cf4d..0000000 --- a/test/forEachTest.js +++ /dev/null @@ -1,15 +0,0 @@ -import forEach from '../dist/forEach'; -import sinon from 'sinon'; - -describe('forEach()', () => { - it('should enumerates the sequence and invokes the given action for each value in the sequence', () => { - const spy = sinon.spy(); - const xs = [1, 2, 3, 4]; - xs::forEach(e => spy(e)); - sinon.assert.callCount(spy, 4); - sinon.assert.calledWith(spy.getCall(0), 1); - sinon.assert.calledWith(spy.getCall(1), 2); - sinon.assert.calledWith(spy.getCall(2), 3); - sinon.assert.calledWith(spy.getCall(3), 4); - }); -}); diff --git a/test/forEachTest.ts b/test/forEachTest.ts new file mode 100644 index 0000000..8b825e5 --- /dev/null +++ b/test/forEachTest.ts @@ -0,0 +1,15 @@ +import * as sinon from 'sinon'; +import Enumerable from '../src'; + +describe('forEach()', () => { + it('should enumerates the sequence and invokes the given action for each value in the sequence', () => { + const spy = sinon.spy(); + const xs = [1, 2, 3, 4]; + new Enumerable(xs).forEach(x => spy(x)); + sinon.assert.callCount(spy, 4); + sinon.assert.calledWith(spy.getCall(0) as sinon.SinonSpy, 1); + sinon.assert.calledWith(spy.getCall(1) as sinon.SinonSpy, 2); + sinon.assert.calledWith(spy.getCall(2) as sinon.SinonSpy, 3); + sinon.assert.calledWith(spy.getCall(3) as sinon.SinonSpy, 4); + }); +}); diff --git a/test/groupByTest.js b/test/groupByTest.js deleted file mode 100644 index b2096ab..0000000 --- a/test/groupByTest.js +++ /dev/null @@ -1,15 +0,0 @@ -import assert from 'assert'; -import groupBy from '../dist/groupBy'; - -describe('groupBy()', () => { - it('should groups the elements of a sequence according to a specified key selector function', () => { - let result = [1, 2, 3, 4]::groupBy(x => x % 2 === 0 ? 'even' : 'odd'); - assert.deepEqual(Array.from(result), [['odd', [1, 3]], ['even', [2, 4]]]); - - result = [1, 2, 3, 4]::groupBy(x => x % 2 === 0 ? 'even' : 'odd', x => x * 2); - assert.deepEqual(Array.from(result), [['odd', [2, 6]], ['even', [4, 8]]]); - - result = [1, 2, 3, 4]::groupBy(x => x % 2 === 0 ? 'even' : 'odd', x => x * 2, (k, vs) => vs); - assert.deepEqual(Array.from(result), [[2, 6], [4, 8]]); - }); -}); diff --git a/test/groupByTest.ts b/test/groupByTest.ts new file mode 100644 index 0000000..b4936bc --- /dev/null +++ b/test/groupByTest.ts @@ -0,0 +1,15 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('groupBy()', () => { + it('should groups the elements of a sequence according to a specified key selector function', () => { + const result1 = new Enumerable([1, 2, 3, 4]).groupBy(x => x % 2 === 0 ? 'even' : 'odd'); + assert.deepEqual(Array.from(result1), [['odd', [1, 3]], ['even', [2, 4]]]); + + const result2 = new Enumerable([1, 2, 3, 4]).groupBy(x => x % 2 === 0 ? 'even' : 'odd', x => x * 2); + assert.deepEqual(Array.from(result2), [['odd', [2, 6]], ['even', [4, 8]]]); + + const result3 = new Enumerable([1, 2, 3, 4]).groupBy(x => x % 2 === 0 ? 'even' : 'odd', x => x * 2, (k, vs) => vs); + assert.deepEqual(Array.from(result3), [[2, 6], [4, 8]]); + }); +}); diff --git a/test/groupJoinTest.js b/test/groupJoinTest.ts similarity index 79% rename from test/groupJoinTest.js rename to test/groupJoinTest.ts index a9b51d9..ccd016c 100644 --- a/test/groupJoinTest.js +++ b/test/groupJoinTest.ts @@ -1,11 +1,11 @@ -import assert from 'assert'; -import groupJoin from '../dist/groupJoin'; +import * as assert from 'assert'; +import Enumerable from '../src'; describe('groupJoin()', () => { it('should correlates the elements of two sequences based on equality of keys and groups the results', () => { let xs = [0, 1, 2]; let ys = [4, 7, 6, 2, 3, 4, 8, 9]; - let result = xs::groupJoin( + let result = new Enumerable(xs).groupJoin( ys, x => x % 3, y => y % 3, @@ -15,7 +15,7 @@ describe('groupJoin()', () => { xs = [0, 1, 2]; ys = [3, 6, 4]; - result = xs::groupJoin( + result = new Enumerable(xs).groupJoin( ys, x => x % 3, y => y % 3, diff --git a/test/ignoreElementsTest.js b/test/ignoreElementsTest.ts similarity index 65% rename from test/ignoreElementsTest.js rename to test/ignoreElementsTest.ts index 88c5c98..0eab654 100644 --- a/test/ignoreElementsTest.js +++ b/test/ignoreElementsTest.ts @@ -1,6 +1,6 @@ -import assert from 'assert'; -import ignoreElements from '../dist/ignoreElements'; -import sinon from 'sinon'; +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import ignoreElements from '../src/ignoreElements'; describe('ignoreElements()', () => { it('should ignores all elements in the source sequence', () => { @@ -12,7 +12,7 @@ describe('ignoreElements()', () => { const source = { [Symbol.iterator]: iterator }; - assert.deepEqual(Array.from(source::ignoreElements()), []); + assert.deepEqual(Array.from(ignoreElements.call(source)), []); sinon.assert.notCalled(iterator); }); }); diff --git a/test/intersectTest.js b/test/intersectTest.ts similarity index 72% rename from test/intersectTest.js rename to test/intersectTest.ts index 7450cfd..09b585c 100644 --- a/test/intersectTest.js +++ b/test/intersectTest.ts @@ -1,11 +1,11 @@ -import assert from 'assert'; -import intersect from '../dist/intersect'; +import * as assert from 'assert'; +import intersect from '../src/intersect'; describe('intersect()', () => { it('should Produces the set intersection of two sequences by using the default equality comparer to compare values', () => { const xs = [44, 26, 92, 30, 71, 38]; const ys = [39, 59, 83, 47, 26, 4, 30]; - const result = xs::intersect(ys); + const result = intersect.call(xs, ys); assert.deepEqual(Array.from(result), [26, 30]); }); diff --git a/test/isEmptyTest.js b/test/isEmptyTest.js deleted file mode 100644 index c3fd92d..0000000 --- a/test/isEmptyTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import assert from 'assert'; -import isEmpty from '../dist/isEmpty'; - -describe('isEmpty()', () => { - it('should determines whether an enumerable sequence is empty', () => { - assert.strictEqual([]::isEmpty(), true); - assert.strictEqual([1, 2, 3]::isEmpty(), false); - }); -}); diff --git a/test/isEmptyTest.ts b/test/isEmptyTest.ts new file mode 100644 index 0000000..dde87a5 --- /dev/null +++ b/test/isEmptyTest.ts @@ -0,0 +1,9 @@ +import * as assert from 'assert'; +import isEmpty from '../src/isEmpty'; + +describe('isEmpty()', () => { + it('should determines whether an enumerable sequence is empty', () => { + assert.strictEqual(isEmpty.call([]), true); + assert.strictEqual(isEmpty.call([1, 2, 3]), false); + }); +}); diff --git a/test/joinTest.js b/test/joinTest.ts similarity index 79% rename from test/joinTest.js rename to test/joinTest.ts index 566395a..d800805 100644 --- a/test/joinTest.js +++ b/test/joinTest.ts @@ -1,11 +1,11 @@ -import assert from 'assert'; -import join from '../dist/join'; +import * as assert from 'assert'; +import Enumerable from '../src'; describe('join()', () => { it('should correlates the elements of two sequences based on matching keys', () => { let xs = [0, 1, 2]; let ys = [3, 6, 4]; - let result = xs::join( + let result = new Enumerable(xs).join( ys, x => x % 3, y => y % 3, @@ -15,7 +15,7 @@ describe('join()', () => { xs = [3, 6, 4]; ys = [0, 1, 2]; - result = xs::join( + result = new Enumerable(xs).join( ys, x => x % 3, y => y % 3, diff --git a/test/lastOrDefaultTest.js b/test/lastOrDefaultTest.js deleted file mode 100644 index af05dc0..0000000 --- a/test/lastOrDefaultTest.js +++ /dev/null @@ -1,29 +0,0 @@ -import assert from 'assert'; -import lastOrDefault from '../dist/lastOrDefault'; -import orderBy from '../dist/orderBy'; -import skip from '../dist/skip'; -import take from '../dist/take'; - -describe('lastOrDefault()', () => { - it('should returns the last element of a sequence', () => { - assert.strictEqual([1, 2, 3, 4]::lastOrDefault(), 4); - assert.strictEqual([1, 2, 3, 4]::lastOrDefault(x => x % 2 === 1), 3); - }); - - it('should returns null if the sequence contains no elements', () => { - assert.strictEqual([]::lastOrDefault(), null); - assert.strictEqual([]::lastOrDefault(null, 123), 123); - assert.strictEqual([1, 2, 3, 4]::lastOrDefault(x => x > 10), null); - assert.strictEqual([1, 2, 3, 4]::lastOrDefault(x => x > 10, 123), 123); - }); - - it('should works with orderBy()', () => { - const xs = [3, 2, 4, 1]; - - assert.strictEqual(xs::orderBy()::lastOrDefault(), 4); - assert.strictEqual(xs::orderBy()::take(2)::lastOrDefault(), 2); - assert.strictEqual(xs::orderBy()::skip(2)::lastOrDefault(), 4); - assert.strictEqual(xs::orderBy()::take(2)::skip(1)::lastOrDefault(), 2); - assert.strictEqual(xs::orderBy()::skip(2)::take(1)::lastOrDefault(), 3); - }); -}); diff --git a/test/lastOrDefaultTest.ts b/test/lastOrDefaultTest.ts new file mode 100644 index 0000000..46d7610 --- /dev/null +++ b/test/lastOrDefaultTest.ts @@ -0,0 +1,27 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('lastOrDefault()', () => { + it('should returns the last element of a sequence', () => { + assert.strictEqual(new Enumerable([1, 2, 3, 4]).lastOrDefault(), 4); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).lastOrDefault(x => x % 2 === 1), 3); + }); + + it('should returns null if the sequence contains no elements', () => { + assert.strictEqual(new Enumerable([]).lastOrDefault(), null); + assert.strictEqual(new Enumerable([]).lastOrDefault(null, 123), 123); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).lastOrDefault(x => x > 10), null); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).lastOrDefault(x => x > 10, 123), 123); + }); + + it('should works with orderBy()', () => { + const xs = [3, 2, 4, 1]; + + assert.strictEqual(new Enumerable(xs).orderBy().lastOrDefault(), 4); + assert.strictEqual(new Enumerable(xs).orderBy().lastOrDefault(x => x % 2 === 1), 3); + assert.strictEqual(new Enumerable(xs).orderBy().take(2).lastOrDefault(), 2); + assert.strictEqual(new Enumerable(xs).orderBy().skip(2).lastOrDefault(), 4); + assert.strictEqual(new Enumerable(xs).orderBy().take(2).skip(1).lastOrDefault(), 2); + assert.strictEqual(new Enumerable(xs).orderBy().skip(2).take(1).lastOrDefault(), 3); + }); +}); diff --git a/test/lastTest.js b/test/lastTest.js deleted file mode 100644 index 254b121..0000000 --- a/test/lastTest.js +++ /dev/null @@ -1,27 +0,0 @@ -import assert from 'assert'; -import last from '../dist/last'; -import orderBy from '../dist/orderBy'; -import skip from '../dist/skip'; -import take from '../dist/take'; - -describe('last()', () => { - it('should returns the last element of a sequence', () => { - assert.strictEqual([1, 2, 3, 4]::last(), 4); - assert.strictEqual([1, 2, 3, 4]::last(x => x % 2 === 1), 3); - }); - - it('should throws the exception if the sequence contains no elements', () => { - assert.throws(() => []::last()); - assert.throws(() => [1, 2, 3, 4]::last(x => x > 10)); - }); - - it('should works with orderBy()', () => { - const xs = [3, 2, 4, 1]; - - assert.strictEqual(xs::orderBy()::last(), 4); - assert.strictEqual(xs::orderBy()::take(2)::last(), 2); - assert.strictEqual(xs::orderBy()::skip(2)::last(), 4); - assert.strictEqual(xs::orderBy()::take(2)::skip(1)::last(), 2); - assert.strictEqual(xs::orderBy()::skip(2)::take(1)::last(), 3); - }); -}); diff --git a/test/lastTest.ts b/test/lastTest.ts new file mode 100644 index 0000000..69bcefe --- /dev/null +++ b/test/lastTest.ts @@ -0,0 +1,25 @@ +import * as assert from 'assert'; +import Enumerable from '../src' + +describe('last()', () => { + it('should returns the last element of a sequence', () => { + assert.strictEqual(new Enumerable([1, 2, 3, 4]).last(), 4); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).last(x => x % 2 === 1), 3); + }); + + it('should throws the exception if the sequence contains no elements', () => { + assert.throws(() => new Enumerable([]).last()); + assert.throws(() => new Enumerable([1, 2, 3, 4]).last(x => x > 10)); + }); + + it('should works with orderBy()', () => { + const xs = [3, 2, 4, 1]; + + assert.strictEqual(new Enumerable(xs).orderBy().last(), 4); + assert.strictEqual(new Enumerable(xs).orderBy().last(x => x % 2 === 1), 3); + assert.strictEqual(new Enumerable(xs).orderBy().take(2).last(), 2); + assert.strictEqual(new Enumerable(xs).orderBy().skip(2).last(), 4); + assert.strictEqual(new Enumerable(xs).orderBy().take(2).skip(1).last(), 2); + assert.strictEqual(new Enumerable(xs).orderBy().skip(2).take(1).last(), 3); + }); +}); diff --git a/test/maxByTest.js b/test/maxByTest.js deleted file mode 100644 index 0c6416b..0000000 --- a/test/maxByTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import maxBy from '../dist/maxBy'; - -describe('maxBy()', () => { - it('should returns the elements with the minimum key value by using the default comparer to compare key values', () => { - assert.deepEqual([]::maxBy(), []); - assert.deepEqual([3, 2, 1, 2, 3]::maxBy(x => x), [3, 3]); - assert.deepEqual(['abc', 'ab', 'a', 'ab', 'abc']::maxBy(s => s.length), ['abc', 'abc']); - }); -}); diff --git a/test/maxByTest.ts b/test/maxByTest.ts new file mode 100644 index 0000000..fbbb2c3 --- /dev/null +++ b/test/maxByTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('maxBy()', () => { + it('should returns the elements with the minimum key value by using the default comparer to compare key values', () => { + assert.deepEqual(new Enumerable([]).maxBy(), []); + assert.deepEqual(new Enumerable([3, 2, 1, 2, 3]).maxBy(x => x), [3, 3]); + assert.deepEqual(new Enumerable(['abc', 'ab', 'a', 'ab', 'abc']).maxBy(s => s.length), ['abc', 'abc']); + }); +}); diff --git a/test/maxTest.js b/test/maxTest.js deleted file mode 100644 index 84961aa..0000000 --- a/test/maxTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import max from '../dist/max'; - -describe('max()', () => { - it('should returns the maximum value in a sequence of values', () => { - assert([]::max(), -Infinity); - assert.strictEqual([1, 2, 3, 2, 1]::max(), 3); - assert.strictEqual(['a', 'ab', 'abc', 'ab', 'a']::max(s => s.length), 3); - }); -}); diff --git a/test/maxTest.ts b/test/maxTest.ts new file mode 100644 index 0000000..bf7b895 --- /dev/null +++ b/test/maxTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('max()', () => { + it('should returns the maximum value in a sequence of values', () => { + assert.strictEqual(new Enumerable([]).max(), -Infinity); + assert.strictEqual(new Enumerable([1, 2, 3, 2, 1]).max(), 3); + assert.strictEqual(new Enumerable(['a', 'ab', 'abc', 'ab', 'a']).max(s => s.length), 3); + }); +}); diff --git a/test/memoizeTest.js b/test/memoizeTest.ts similarity index 76% rename from test/memoizeTest.js rename to test/memoizeTest.ts index c3df2da..98d7d96 100644 --- a/test/memoizeTest.js +++ b/test/memoizeTest.ts @@ -1,5 +1,5 @@ -import assert from 'assert'; -import memoize from '../dist/memoize'; +import * as assert from 'assert'; +import Enumerable from '../src'; describe('memoize()', () => { it('should creates a buffer with a view over the source sequence', () => { @@ -7,7 +7,7 @@ describe('memoize()', () => { const iterator = (function* () { for (let i = 0; i < 10; i++) yield n++ })(); - const memoized = iterator::memoize(); + const memoized = new Enumerable(iterator).memoize(); const expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; assert.deepEqual(Array.from(memoized), expected); assert.deepEqual(Array.from(memoized), expected); diff --git a/test/minByTest.js b/test/minByTest.js deleted file mode 100644 index 7e9e778..0000000 --- a/test/minByTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import minBy from '../dist/minBy'; - -describe('minBy()', () => { - it('should returns the elements with the maximum key value by using the default comparer to compare key values', () => { - assert.deepEqual([]::minBy(), []); - assert.deepEqual([1, 2, 3, 2, 1]::minBy(x => x), [1, 1]); - assert.deepEqual(['a', 'ab', 'abc', 'ab', 'a']::minBy(s => s.length), ['a', 'a']); - }); -}); diff --git a/test/minByTest.ts b/test/minByTest.ts new file mode 100644 index 0000000..8bd78b9 --- /dev/null +++ b/test/minByTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('minBy()', () => { + it('should returns the elements with the maximum key value by using the default comparer to compare key values', () => { + assert.deepEqual(new Enumerable([]).minBy(), []); + assert.deepEqual(new Enumerable([1, 2, 3, 2, 1]).minBy(x => x), [1, 1]); + assert.deepEqual(new Enumerable(['a', 'ab', 'abc', 'ab', 'a']).minBy(s => s.length), ['a', 'a']); + }); +}); diff --git a/test/minTest.js b/test/minTest.js deleted file mode 100644 index 1cde89a..0000000 --- a/test/minTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import min from '../dist/min'; - -describe('min()', () => { - it('should returns the minimum value in a sequence of values', () => { - assert.strictEqual([]::min(), Infinity); - assert.strictEqual([1, 2, 3, 2, 1]::min(), 1); - assert.strictEqual(['a', 'ab', 'abc', 'ab', 'a']::min(s => s.length), 1); - }); -}); diff --git a/test/minTest.ts b/test/minTest.ts new file mode 100644 index 0000000..87bdf96 --- /dev/null +++ b/test/minTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('min()', () => { + it('should returns the minimum value in a sequence of values', () => { + assert.strictEqual(new Enumerable([]).min(), Infinity); + assert.strictEqual(new Enumerable([1, 2, 3, 2, 1]).min(), 1); + assert.strictEqual(new Enumerable(['a', 'ab', 'abc', 'ab', 'a']).min(s => s.length), 1); + }); +}); diff --git a/test/onErrorResumeNextTest.js b/test/onErrorResumeNextTest.ts similarity index 63% rename from test/onErrorResumeNextTest.js rename to test/onErrorResumeNextTest.ts index 717e1d1..6cc2e68 100644 --- a/test/onErrorResumeNextTest.js +++ b/test/onErrorResumeNextTest.ts @@ -1,6 +1,5 @@ -import assert from 'assert'; -import onErrorResumeNext from '../dist/onErrorResumeNext'; -import sinon from 'sinon'; +import * as assert from 'assert'; +import onErrorResumeNext from '../src/onErrorResumeNext'; describe('onErrorResumeNext()', () => { it('should creates a sequence that concatenates both given sequences', () => { @@ -12,6 +11,6 @@ describe('onErrorResumeNext()', () => { } }; const ys = [3, 4]; - assert.deepEqual(Array.from(xs::onErrorResumeNext(ys)), [1, 2, 3, 4]); + assert.deepEqual(Array.from(onErrorResumeNext.call(xs, ys)), [1, 2, 3, 4]); }); }); diff --git a/test/orderByDescendingTest.js b/test/orderByDescendingTest.js deleted file mode 100644 index 599de2d..0000000 --- a/test/orderByDescendingTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import assert from 'assert'; -import orderByDescending from '../dist/orderByDescending'; - -describe('orderByDescending()', () => { - it('should sorts the elements of a sequence in ascending order according to a key', () => { - assert.deepEqual(Array.from([3, 2, 4, 1]::orderByDescending()), [4, 3, 2, 1]); - assert.deepEqual(Array.from([3, 2, 4, 1]::orderByDescending(n => n % 2).thenByDescending(n => n)), [3, 1, 4, 2]); - }); -}); diff --git a/test/orderByDescendingTest.ts b/test/orderByDescendingTest.ts new file mode 100644 index 0000000..6b651c8 --- /dev/null +++ b/test/orderByDescendingTest.ts @@ -0,0 +1,9 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('orderByDescending()', () => { + it('should sorts the elements of a sequence in ascending order according to a key', () => { + assert.deepEqual(new Enumerable([3, 2, 4, 1]).orderByDescending().toArray(), [4, 3, 2, 1]); + assert.deepEqual(new Enumerable([3, 2, 4, 1]).orderByDescending(n => n % 2).thenByDescending(n => n).toArray(), [3, 1, 4, 2]); + }); +}); diff --git a/test/orderByTest.js b/test/orderByTest.js deleted file mode 100644 index a84edd4..0000000 --- a/test/orderByTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import assert from 'assert'; -import orderBy from '../dist/orderBy'; - -describe('orderBy()', () => { - it('should sorts the elements of a sequence in ascending order according to a key', () => { - assert.deepEqual(Array.from([3, 2, 4, 1]::orderBy()), [1, 2, 3, 4]); - assert.deepEqual(Array.from([3, 2, 4, 1]::orderBy(n => n % 2).thenBy(n => n)), [2, 4, 1, 3]); - }); -}); diff --git a/test/orderByTest.ts b/test/orderByTest.ts new file mode 100644 index 0000000..52dc9af --- /dev/null +++ b/test/orderByTest.ts @@ -0,0 +1,9 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('orderBy()', () => { + it('should sorts the elements of a sequence in ascending order according to a key', () => { + assert.deepEqual(new Enumerable([3, 2, 4, 1]).orderBy().toArray(), [1, 2, 3, 4]); + assert.deepEqual(new Enumerable([3, 2, 4, 1]).orderBy(n => n % 2).thenBy(n => n).toArray(), [2, 4, 1, 3]); + }); +}); diff --git a/test/repeatTest.js b/test/repeatTest.ts similarity index 84% rename from test/repeatTest.js rename to test/repeatTest.ts index ff06bef..f4aaaee 100644 --- a/test/repeatTest.js +++ b/test/repeatTest.ts @@ -1,9 +1,9 @@ -import assert from 'assert'; -import repeat from '../dist/repeat'; +import * as assert from 'assert'; +import repeat from '../src/repeat'; describe('repeat()', () => { it('should generates a sequence by repeating the given value.', () => { - const iterator1 = [1, 2, 3]::repeat()[Symbol.iterator](); + const iterator1 = repeat.call([1, 2, 3])[Symbol.iterator](); assert.deepEqual(iterator1.next(), { done: false, value: 1 }); assert.deepEqual(iterator1.next(), { done: false, value: 2 }); assert.deepEqual(iterator1.next(), { done: false, value: 3 }); @@ -12,7 +12,7 @@ describe('repeat()', () => { assert.deepEqual(iterator1.next(), { done: false, value: 3 }); assert.deepEqual(iterator1.next(), { done: false, value: 1 }); - const iterator2 = [1, 2, 3]::repeat(2)[Symbol.iterator](); + const iterator2 = repeat.call([1, 2, 3], 2)[Symbol.iterator](); assert.deepEqual(iterator2.next(), { done: false, value: 1 }); assert.deepEqual(iterator2.next(), { done: false, value: 2 }); assert.deepEqual(iterator2.next(), { done: false, value: 3 }); diff --git a/test/retryTest.js b/test/retryTest.ts similarity index 74% rename from test/retryTest.js rename to test/retryTest.ts index fb32ee3..f0406e3 100644 --- a/test/retryTest.js +++ b/test/retryTest.ts @@ -1,10 +1,10 @@ -import assert from 'assert'; -import retry from '../dist/retry'; +import * as assert from 'assert'; +import retry from '../src/retry'; describe('retry()', () => { it('should creates a sequence that retries enumerating the source sequence as long as an error occurs.', () => { - assert.deepEqual(Array.from([1, 2, 3]::retry()), [1, 2, 3]); - assert.deepEqual(Array.from([1, 2, 3]::retry(2)), [1, 2, 3]); + assert.deepEqual(Array.from(retry.call([1, 2, 3])), [1, 2, 3]); + assert.deepEqual(Array.from(retry.call([1, 2, 3], 2)), [1, 2, 3]); const xs = { [Symbol.iterator]: function*() { @@ -14,7 +14,7 @@ describe('retry()', () => { throw new Error() } }; - const iterator = xs::retry(2)[Symbol.iterator](); + const iterator = retry.call(xs, 2)[Symbol.iterator](); assert.deepEqual(iterator.next(), { done: false, value: 1 }); assert.deepEqual(iterator.next(), { done: false, value: 2 }); assert.deepEqual(iterator.next(), { done: false, value: 3 }); diff --git a/test/reverseTest.js b/test/reverseTest.js deleted file mode 100644 index ca28518..0000000 --- a/test/reverseTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import assert from 'assert'; -import reverse from '../dist/reverse'; - -describe('reverse()', () => { - it('should inverts the order of the elements in a sequence', () => { - assert.deepEqual(Array.from([]::reverse()), []); - assert.deepEqual(Array.from([3, 2, 4, 1]::reverse(x => x)), [1, 4, 2, 3]); - }); -}); diff --git a/test/reverseTest.ts b/test/reverseTest.ts new file mode 100644 index 0000000..6052205 --- /dev/null +++ b/test/reverseTest.ts @@ -0,0 +1,9 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('reverse()', () => { + it('should inverts the order of the elements in a sequence', () => { + assert.deepEqual(new Enumerable([]).reverse().toArray(), []); + assert.deepEqual(new Enumerable([3, 2, 4, 1]).reverse().toArray(), [1, 4, 2, 3]); + }); +}); diff --git a/test/scanTest.js b/test/scanTest.js deleted file mode 100644 index a5c9ecf..0000000 --- a/test/scanTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import assert from 'assert'; -import scan from '../dist/scan'; - -describe('scan()', () => { - it('should generates a sequence of accumulated values', () => { - assert.deepEqual(Array.from([1, 2, 3, 4, 5]::scan(0, (total, n) => total + n)), [1, 3, 6, 10, 15]); - assert.deepEqual(Array.from([]::scan(0, (total, n) => total + n)), []); - }); -}); diff --git a/test/scanTest.ts b/test/scanTest.ts new file mode 100644 index 0000000..6085212 --- /dev/null +++ b/test/scanTest.ts @@ -0,0 +1,9 @@ +import * as assert from 'assert'; +import Enumerable from '../src/Enumerable'; + +describe('scan()', () => { + it('should generates a sequence of accumulated values', () => { + assert.deepEqual(new Enumerable([1, 2, 3, 4, 5]).scan(0, (total, n) => total + n).toArray(), [1, 3, 6, 10, 15]); + assert.deepEqual(new Enumerable([]).scan(0, (total, n) => total + n).toArray(), []); + }); +}); diff --git a/test/selectManyTest.js b/test/selectManyTest.js deleted file mode 100644 index 386735c..0000000 --- a/test/selectManyTest.js +++ /dev/null @@ -1,8 +0,0 @@ -import assert from 'assert'; -import selectMany from '../dist/selectMany'; - -describe('selectMany()', () => { - it('should projects each element of a sequence to a Iterable', () => { - assert.deepEqual(Array.from([1, 2, 3, 4]::selectMany(x => [x, x * 2])), [1, 2, 2, 4, 3, 6, 4, 8]); - }); -}); diff --git a/test/selectManyTest.ts b/test/selectManyTest.ts new file mode 100644 index 0000000..ca1b57e --- /dev/null +++ b/test/selectManyTest.ts @@ -0,0 +1,8 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('selectMany()', () => { + it('should projects each element of a sequence to a Iterable', () => { + assert.deepEqual(new Enumerable([1, 2, 3, 4]).selectMany(x => [x, x * 2]).toArray(), [1, 2, 2, 4, 3, 6, 4, 8]); + }); +}); diff --git a/test/selectTest.js b/test/selectTest.js deleted file mode 100644 index 7b507b7..0000000 --- a/test/selectTest.js +++ /dev/null @@ -1,8 +0,0 @@ -import assert from 'assert'; -import select from '../dist/select'; - -describe('select()', () => { - it('should projects each element of a sequence', () => { - assert.deepEqual(Array.from([1, 2, 3, 4]::select(x => x * 2)), [2, 4, 6, 8]); - }); -}); diff --git a/test/selectTest.ts b/test/selectTest.ts new file mode 100644 index 0000000..aa97138 --- /dev/null +++ b/test/selectTest.ts @@ -0,0 +1,8 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('select()', () => { + it('should projects each element of a sequence', () => { + assert.deepEqual(new Enumerable([1, 2, 3, 4]).select(x => x * 2).toArray(), [2, 4, 6, 8]); + }); +}); diff --git a/test/singleOrDefaultTest.js b/test/singleOrDefaultTest.js deleted file mode 100644 index e3374aa..0000000 --- a/test/singleOrDefaultTest.js +++ /dev/null @@ -1,20 +0,0 @@ -import assert from 'assert'; -import singleOrDefault from '../dist/singleOrDefault'; - -describe('singleOrDefault()', () => { - it('should returns the only element of a sequence', () => { - assert.strictEqual([1]::singleOrDefault(), 1); - assert.strictEqual([1, 2]::singleOrDefault(x => x % 2 === 0), 2); - }); - - it('should returns a default value if the sequence is empty', () => { - assert.strictEqual([]::singleOrDefault(null, 123), 123); - assert.strictEqual([1, 2]::singleOrDefault(null, 123), 123); - assert.strictEqual([1, 2, 3, 4]::singleOrDefault(x => x > 0, 123), 123); - assert.strictEqual([1, 2, 3, 4]::singleOrDefault(x => x > 10, 123), 123); - assert.strictEqual([]::singleOrDefault(), null); - assert.strictEqual([1, 2]::singleOrDefault(), null); - assert.strictEqual([1, 2, 3, 4]::singleOrDefault(x => x > 0), null); - assert.strictEqual([1, 2, 3, 4]::singleOrDefault(x => x > 10), null); - }); -}); diff --git a/test/singleOrDefaultTest.ts b/test/singleOrDefaultTest.ts new file mode 100644 index 0000000..b6b5612 --- /dev/null +++ b/test/singleOrDefaultTest.ts @@ -0,0 +1,20 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('singleOrDefault()', () => { + it('should returns the only element of a sequence', () => { + assert.strictEqual(new Enumerable([1]).singleOrDefault(), 1); + assert.strictEqual(new Enumerable([1, 2]).singleOrDefault(x => x % 2 === 0), 2); + }); + + it('should returns a default value if the sequence is empty', () => { + assert.strictEqual(new Enumerable([]).singleOrDefault(null, 123), 123); + assert.strictEqual(new Enumerable([1, 2]).singleOrDefault(null, 123), 123); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).singleOrDefault(x => x > 0, 123), 123); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).singleOrDefault(x => x > 10, 123), 123); + assert.strictEqual(new Enumerable([]).singleOrDefault(), null); + assert.strictEqual(new Enumerable([1, 2]).singleOrDefault(), null); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).singleOrDefault(x => x > 0), null); + assert.strictEqual(new Enumerable([1, 2, 3, 4]).singleOrDefault(x => x > 10), null); + }); +}); diff --git a/test/singleTest.js b/test/singleTest.js deleted file mode 100644 index 00db3d6..0000000 --- a/test/singleTest.js +++ /dev/null @@ -1,16 +0,0 @@ -import single from '../dist/single'; -import assert from 'assert'; - -describe('single()', () => { - it('should returns the only element of a sequence', () => { - assert.strictEqual([1]::single(), 1); - assert.strictEqual([1, 2]::single(x => x % 2 === 0), 2); - }); - - it('should throws an exception if there is not exactly one element in the sequence', () => { - assert.throws(() => []::single()); - assert.throws(() => [1, 2]::single()); - assert.throws(() => [1, 2, 3, 4]::single(x => x > 0)); - assert.throws(() => [1, 2, 3, 4]::single(x => x > 10)); - }); -}); diff --git a/test/singleTest.ts b/test/singleTest.ts new file mode 100644 index 0000000..2a1a4d4 --- /dev/null +++ b/test/singleTest.ts @@ -0,0 +1,16 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('single()', () => { + it('should returns the only element of a sequence', () => { + assert.strictEqual(new Enumerable([1]).single(), 1); + assert.strictEqual(new Enumerable([1, 2]).single(x => x % 2 === 0), 2); + }); + + it('should throws an exception if there is not exactly one element in the sequence', () => { + assert.throws(() => new Enumerable([]).single()); + assert.throws(() => new Enumerable([1, 2]).single()); + assert.throws(() => new Enumerable([1, 2, 3, 4]).single(x => x > 0)); + assert.throws(() => new Enumerable([1, 2, 3, 4]).single(x => x > 10)); + }); +}); diff --git a/test/skipLastTest.js b/test/skipLastTest.js deleted file mode 100644 index cb8a234..0000000 --- a/test/skipLastTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import skipLast from '../dist/skipLast'; - -describe('skipLast()', () => { - it('should bypasses a specified number of contiguous elements from the end of the sequence', () => { - assert.deepEqual(Array.from([]::skipLast(1)), []); - assert.deepEqual(Array.from([0, 1, 2, 3, 4]::skipLast(0)), [0, 1, 2, 3, 4]); - assert.deepEqual(Array.from([0, 1, 2, 3, 4]::skipLast(3)), [0, 1]); - }); -}); diff --git a/test/skipLastTest.ts b/test/skipLastTest.ts new file mode 100644 index 0000000..cb1e815 --- /dev/null +++ b/test/skipLastTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import skipLast from '../src/skipLast'; + +describe('skipLast()', () => { + it('should bypasses a specified number of contiguous elements from the end of the sequence', () => { + assert.deepEqual(Array.from(skipLast.call([], 1)), []); + assert.deepEqual(Array.from(skipLast.call([0, 1, 2, 3, 4], 0)), [0, 1, 2, 3, 4]); + assert.deepEqual(Array.from(skipLast.call([0, 1, 2, 3, 4], 3)), [0, 1]); + }); +}); diff --git a/test/skipTest.js b/test/skipTest.js deleted file mode 100644 index 3037515..0000000 --- a/test/skipTest.js +++ /dev/null @@ -1,11 +0,0 @@ -import assert from 'assert'; -import skip from '../dist/skip'; - -describe('skip()', () => { - it('should bypasses a specified number of elements in a sequence and then returns the remaining elements', () => { - assert.deepEqual(Array.from([]::skip(1)), []); - assert.deepEqual(Array.from([0, 1, 2, 3, 4]::skip(0)), [0, 1, 2, 3, 4]); - assert.deepEqual(Array.from([0, 1, 2, 3, 4]::skip(3)), [3, 4]); - assert.deepEqual(Array.from([0, 1, 2, 3, 4][Symbol.iterator]()::skip(3)), [3, 4]); - }); -}); diff --git a/test/skipTest.ts b/test/skipTest.ts new file mode 100644 index 0000000..7c55af0 --- /dev/null +++ b/test/skipTest.ts @@ -0,0 +1,11 @@ +import * as assert from 'assert'; +import skip from '../src/skip'; + +describe('skip()', () => { + it('should bypasses a specified number of elements in a sequence and then returns the remaining elements', () => { + assert.deepEqual(Array.from(skip.call([], 1)), []); + assert.deepEqual(Array.from(skip.call([0, 1, 2, 3, 4], 0)), [0, 1, 2, 3, 4]); + assert.deepEqual(Array.from(skip.call([0, 1, 2, 3, 4], 3)), [3, 4]); + assert.deepEqual(Array.from(skip.call([0, 1, 2, 3, 4][Symbol.iterator](), 3)), [3, 4]); + }); +}); diff --git a/test/skipWhileTest.js b/test/skipWhileTest.js deleted file mode 100644 index 227e018..0000000 --- a/test/skipWhileTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import assert from 'assert'; -import skipWhile from '../dist/skipWhile'; - -describe('skipWhile()', () => { - it('should skip elements in a sequence while a condition is true', () => { - assert.deepEqual(Array.from([1, 2, 3, 4]::skipWhile(x => x % 2 === 0)), [1, 2, 3, 4]); - assert.deepEqual(Array.from([4, 3, 2, 1]::skipWhile(x => x % 2 === 0)), [3, 2, 1]); - }); -}); diff --git a/test/skipWhileTest.ts b/test/skipWhileTest.ts new file mode 100644 index 0000000..d2d5a51 --- /dev/null +++ b/test/skipWhileTest.ts @@ -0,0 +1,9 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('skipWhile()', () => { + it('should skip elements in a sequence while a condition is true', () => { + assert.deepEqual(new Enumerable([1, 2, 3, 4]).skipWhile(x => x % 2 === 0).toArray(), [1, 2, 3, 4]); + assert.deepEqual(new Enumerable([4, 3, 2, 1]).skipWhile(x => x % 2 === 0).toArray(), [3, 2, 1]); + }); +}); diff --git a/test/startWithTest.js b/test/startWithTest.js deleted file mode 100644 index a15aa53..0000000 --- a/test/startWithTest.js +++ /dev/null @@ -1,8 +0,0 @@ -import assert from 'assert'; -import startWith from '../dist/startWith'; - -describe('startWith()', () => { - it('should returns the source sequence prefixed with the specified value', () => { - assert.deepEqual(Array.from([0, 1, 2, 3, 4]::startWith(0)), [0, 0, 1, 2, 3, 4]); - }); -}); diff --git a/test/startWithTest.ts b/test/startWithTest.ts new file mode 100644 index 0000000..e72bdd9 --- /dev/null +++ b/test/startWithTest.ts @@ -0,0 +1,8 @@ +import * as assert from 'assert'; +import startWith from '../src/startWith'; + +describe('startWith()', () => { + it('should returns the source sequence prefixed with the specified value', () => { + assert.deepEqual(Array.from(startWith.call([0, 1, 2, 3, 4], 0)), [0, 0, 1, 2, 3, 4]); + }); +}); diff --git a/test/static/catchTest.js b/test/static/catchTest.ts similarity index 52% rename from test/static/catchTest.js rename to test/static/catchTest.ts index bc28676..930ee2e 100644 --- a/test/static/catchTest.js +++ b/test/static/catchTest.ts @@ -1,30 +1,30 @@ -import _catch from '../../dist/static/catch'; -import assert from 'assert'; +import Enumerable from '../../src'; +import * as assert from 'assert'; describe('catch()', () => { it('should creates a sequence by concatenating source sequences until a source sequence completes successfully', () => { const xs = { [Symbol.iterator]: function*() { - yield 1 - yield 2 - yield 3 - throw new Error() + yield 1; + yield 2; + yield 3; + throw new Error(); } }; const ys = [4, 5, 6]; const zs = [7, 8, 9]; - assert.deepEqual(Array.from(_catch(xs, ys, zs)), [1, 2, 3, 4, 5, 6]); + assert.deepEqual(Enumerable._catch(xs, ys, zs).toArray(), [1, 2, 3, 4, 5, 6]); }); it('should throw the exception if an error continues to occur', () => { const xs = { [Symbol.iterator]: function*() { - yield 1 - yield 2 - yield 3 - throw new Error() + yield 1; + yield 2; + yield 3; + throw new Error(); } }; - assert.throws(() => Array.from(_catch(xs, xs))); + assert.throws(() => Enumerable._catch(xs, xs).toArray()); }); }); diff --git a/test/static/concatTest.js b/test/static/concatTest.js deleted file mode 100644 index 018c007..0000000 --- a/test/static/concatTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import concat from '../../dist/static/concat'; - -describe('concat()', () => { - it('should concatenates two sequences', () => { - assert.deepEqual(Array.from(concat([], [])), []); - assert.deepEqual(Array.from(concat([1, 2, 3], [4, 5, 6])), [1, 2, 3, 4, 5, 6]); - assert.deepEqual(Array.from(concat([1, 2, 3], [4, 5, 6], [7, 8, 9])), [1, 2, 3, 4, 5, 6, 7, 8, 9]); - }); -}); diff --git a/test/static/concatTest.ts b/test/static/concatTest.ts new file mode 100644 index 0000000..2b92af0 --- /dev/null +++ b/test/static/concatTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import Enumerable from '../../src'; + +describe('concat()', () => { + it('should concatenates two sequences', () => { + assert.deepEqual(Enumerable.concat([], []).toArray(), []); + assert.deepEqual(Enumerable.concat([1, 2, 3], [4, 5, 6]).toArray(), [1, 2, 3, 4, 5, 6]); + assert.deepEqual(Enumerable.concat([1, 2, 3], [4, 5, 6], [7, 8, 9]).toArray(), [1, 2, 3, 4, 5, 6, 7, 8, 9]); + }); +}); diff --git a/test/static/deferTest.js b/test/static/deferTest.js deleted file mode 100644 index b4ef296..0000000 --- a/test/static/deferTest.js +++ /dev/null @@ -1,19 +0,0 @@ -import assert from 'assert'; -import defer from '../../dist/static/defer'; - -describe('defer()', () => { - it('should creates an enumerable sequence based on an enumerable factory function', () => { - let n = 5; - - const xs = { - [Symbol.iterator]: function*() { - while (n > 0) { - yield n-- - } - } - }; - - assert.deepEqual(Array.from(xs), [5, 4, 3, 2, 1]); - assert.deepEqual(Array.from(xs), []); - }); -}); diff --git a/test/static/deferTest.ts b/test/static/deferTest.ts new file mode 100644 index 0000000..bd93c0a --- /dev/null +++ b/test/static/deferTest.ts @@ -0,0 +1,19 @@ +import * as assert from 'assert'; +import Enumerable from '../../src'; + +describe('defer()', () => { + it('should creates an enumerable sequence based on an enumerable factory function', () => { + let n = 0; + + const xs = Enumerable.defer(function*() { + n++; + for (let i = 0; i < n; i++) { + yield i; + } + }); + + assert.deepEqual(xs.toArray(), [0]); + assert.deepEqual(xs.toArray(), [0, 1]); + assert.deepEqual(xs.toArray(), [0, 1, 2]); + }); +}); diff --git a/test/static/generateTest.js b/test/static/generateTest.js deleted file mode 100644 index 0b4b7bc..0000000 --- a/test/static/generateTest.js +++ /dev/null @@ -1,8 +0,0 @@ -import assert from 'assert'; -import generate from '../../dist/static/generate'; - -describe('generate()', () => { - it('should generates a sequence by mimicking a for loop', () => { - assert.deepEqual(Array.from(generate(0, x => x < 5, x => x + 1, x => x * x)), [0, 1, 4, 9, 16]); - }); -}); diff --git a/test/static/generateTest.ts b/test/static/generateTest.ts new file mode 100644 index 0000000..1deeaf5 --- /dev/null +++ b/test/static/generateTest.ts @@ -0,0 +1,8 @@ +import * as assert from 'assert'; +import Enumerable from '../../src'; + +describe('generate()', () => { + it('should generates a sequence by mimicking a for loop', () => { + assert.deepEqual(Enumerable.generate(0, x => x < 5, x => x + 1, x => x * x).toArray(), [0, 1, 4, 9, 16]); + }); +}); diff --git a/test/static/ifTest.js b/test/static/ifTest.js deleted file mode 100644 index f0ef7b5..0000000 --- a/test/static/ifTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import _if from '../../dist/static/if'; -import assert from 'assert'; - -describe('if()', () => { - it('should returns an enumerable sequence based on the evaluation result of the given condition', () => { - assert.deepEqual(Array.from(_if(() => true, [1, 2, 3], [4, 5, 6])), [1, 2, 3]); - assert.deepEqual(Array.from(_if(() => false, [1, 2, 3], [4, 5, 6])), [4, 5, 6]); - }); -}); diff --git a/test/static/ifTest.ts b/test/static/ifTest.ts new file mode 100644 index 0000000..7ab48c4 --- /dev/null +++ b/test/static/ifTest.ts @@ -0,0 +1,9 @@ +import Enumerable from '../../src'; +import * as assert from 'assert'; + +describe('if()', () => { + it('should returns an enumerable sequence based on the evaluation result of the given condition', () => { + assert.deepEqual(Enumerable._if(() => true, [1, 2, 3], [4, 5, 6]).toArray(), [1, 2, 3]); + assert.deepEqual(Enumerable._if(() => false, [1, 2, 3], [4, 5, 6]).toArray(), [4, 5, 6]); + }); +}); diff --git a/test/static/rangeTest.js b/test/static/rangeTest.js deleted file mode 100644 index 9bed6cb..0000000 --- a/test/static/rangeTest.js +++ /dev/null @@ -1,33 +0,0 @@ -import assert from 'assert'; -import elementAt from '../../dist/elementAt'; -import elementAtOrDefault from '../../dist/elementAtOrDefault'; -import first from '../../dist/first'; -import firstOrDefault from '../../dist/firstOrDefault'; -import last from '../../dist/last'; -import lastOrDefault from '../../dist/lastOrDefault'; -import range from '../../dist/static/range'; -import skip from '../../dist/skip'; -import take from '../../dist/take'; - -describe('range()', () => { - it('should generates a sequence of integral numbers within a specified range', () => { - assert.deepEqual(Array.from(range(1, 10)), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); - assert.deepEqual(Array.from(range(1, 10)::take(5)), [1, 2, 3, 4, 5]); - assert.deepEqual(Array.from(range(1, 10)::skip(5)), [6, 7, 8, 9, 10]); - assert.strictEqual(range(1, 10).length, 10); - assert.strictEqual(range(1, 10)::take(5)::first(), 1); - assert.strictEqual(range(1, 10)::take(5)::last(), 5); - assert.strictEqual(range(1, 10)::take(5)::first(x => x % 2 === 0), 2); - assert.strictEqual(range(1, 10)::take(5)::last(x => x % 2 === 0), 4); - assert.strictEqual(range(1, 10)::skip(5)::first(), 6); - assert.strictEqual(range(1, 10)::skip(5)::last(), 10); - assert.strictEqual(range(1, 10)::skip(10)::firstOrDefault(null, 123), 123); - assert.strictEqual(range(1, 10)::skip(10)::lastOrDefault(null, 123), 123); - assert.strictEqual(range(1, 10)::elementAt(5), 6); - assert.strictEqual(range(1, 10)::elementAtOrDefault(999, 123), 123); - - assert.throws(() => range(1, 10)::skip(10)::first()); - assert.throws(() => range(1, 10)::skip(10)::last()); - assert.throws(() => range(1, 10)::elementAt(999)); - }); -}); diff --git a/test/static/rangeTest.ts b/test/static/rangeTest.ts new file mode 100644 index 0000000..e78bd3a --- /dev/null +++ b/test/static/rangeTest.ts @@ -0,0 +1,24 @@ +import * as assert from 'assert'; +import Enumerable from '../../src'; + +describe('range()', () => { + it('should generates a sequence of integral numbers within a specified range', () => { + assert.deepEqual(Enumerable.range(1, 10).toArray(), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + assert.deepEqual(Enumerable.range(1, 10).take(5).toArray(), [1, 2, 3, 4, 5]); + assert.deepEqual(Enumerable.range(1, 10).skip(5).toArray(), [6, 7, 8, 9, 10]); + assert.strictEqual(Enumerable.range(1, 10).take(5).first(), 1); + assert.strictEqual(Enumerable.range(1, 10).take(5).last(), 5); + assert.strictEqual(Enumerable.range(1, 10).take(5).first(x => x % 2 === 0), 2); + assert.strictEqual(Enumerable.range(1, 10).take(5).last(x => x % 2 === 0), 4); + assert.strictEqual(Enumerable.range(1, 10).skip(5).first(), 6); + assert.strictEqual(Enumerable.range(1, 10).skip(5).last(), 10); + assert.strictEqual(Enumerable.range(1, 10).skip(10).firstOrDefault(null, 123), 123); + assert.strictEqual(Enumerable.range(1, 10).skip(10).lastOrDefault(null, 123), 123); + assert.strictEqual(Enumerable.range(1, 10).elementAt(5), 6); + assert.strictEqual(Enumerable.range(1, 10).elementAtOrDefault(999, 123), 123); + + assert.throws(() => Enumerable.range(1, 10).skip(10).first()); + assert.throws(() => Enumerable.range(1, 10).skip(10).last()); + assert.throws(() => Enumerable.range(1, 10).elementAt(999)); + }); +}); diff --git a/test/static/repeatTest.js b/test/static/repeatTest.ts similarity index 77% rename from test/static/repeatTest.js rename to test/static/repeatTest.ts index a347b9d..d6b8518 100644 --- a/test/static/repeatTest.js +++ b/test/static/repeatTest.ts @@ -1,15 +1,15 @@ -import assert from 'assert'; -import repeat from '../../dist/static/repeat'; +import * as assert from 'assert'; +import Enumerable from '../../src'; describe('repeat()', () => { it('should generates a sequence by repeating the given value', () => { - const iterator1 = repeat(123)[Symbol.iterator](); + const iterator1 = Enumerable.repeat(123)[Symbol.iterator](); assert.deepEqual(iterator1.next(), { done: false, value: 123 }); assert.deepEqual(iterator1.next(), { done: false, value: 123 }); assert.deepEqual(iterator1.next(), { done: false, value: 123 }); assert.deepEqual(iterator1.next(), { done: false, value: 123 }); - const iterator2 = repeat(123, 3)[Symbol.iterator](); + const iterator2 = Enumerable.repeat(123, 3)[Symbol.iterator](); assert.deepEqual(iterator2.next(), { done: false, value: 123 }); assert.deepEqual(iterator2.next(), { done: false, value: 123 }); assert.deepEqual(iterator2.next(), { done: false, value: 123 }); diff --git a/test/static/returnTest.js b/test/static/returnTest.js deleted file mode 100644 index 5cf5fbf..0000000 --- a/test/static/returnTest.js +++ /dev/null @@ -1,8 +0,0 @@ -import _return from '../../dist/static/return'; -import assert from 'assert'; - -describe('return()', () => { - it('should returns a sequence with a single element.', () => { - assert.deepEqual(Array.from(_return(123)), [123]); - }); -}); diff --git a/test/static/returnTest.ts b/test/static/returnTest.ts new file mode 100644 index 0000000..b3c6b2c --- /dev/null +++ b/test/static/returnTest.ts @@ -0,0 +1,8 @@ +import Enumerable from '../../src'; +import * as assert from 'assert'; + +describe('return()', () => { + it('should returns a sequence with a single element.', () => { + assert.deepEqual(Enumerable._return(123).toArray(), [123]); + }); +}); diff --git a/test/static/zipTest.js b/test/static/zipTest.js deleted file mode 100644 index 3cf66d7..0000000 --- a/test/static/zipTest.js +++ /dev/null @@ -1,11 +0,0 @@ -import assert from 'assert'; -import zip from '../../dist/static/zip'; - -describe('zip()', () => { - it('should applies a specified function to the corresponding elements of two sequences', () => { - assert.deepEqual(Array.from(zip([1, 3, 5, 7], [2, 4, 6, 8], (x, y) => [x, y])), [[1, 2], [3, 4], [5, 6], [7, 8]]); - assert.deepEqual(Array.from(zip([1, 3, 5, 7, 9], [2, 4, 6, 8], (x, y) => [x, y])), [[1, 2], [3, 4], [5, 6], [7, 8]]); - assert.deepEqual(Array.from(zip([1, 3, 5, 7], [2, 4, 6, 8, 10], (x, y) => [x, y])), [[1, 2], [3, 4], [5, 6], [7, 8]]); - assert.deepEqual(Array.from(zip([], [], (x, y) => [x, y])), []); - }); -}); diff --git a/test/static/zipTest.ts b/test/static/zipTest.ts new file mode 100644 index 0000000..0861feb --- /dev/null +++ b/test/static/zipTest.ts @@ -0,0 +1,11 @@ +import * as assert from 'assert'; +import Enumerable from '../../src'; + +describe('zip()', () => { + it('should applies a specified function to the corresponding elements of two sequences', () => { + assert.deepEqual(Enumerable.zip([1, 3, 5, 7], [2, 4, 6, 8], (x, y) => [x, y]).toArray(), [[1, 2], [3, 4], [5, 6], [7, 8]]); + assert.deepEqual(Enumerable.zip([1, 3, 5, 7, 9], [2, 4, 6, 8], (x, y) => [x, y]).toArray(), [[1, 2], [3, 4], [5, 6], [7, 8]]); + assert.deepEqual(Enumerable.zip([1, 3, 5, 7], [2, 4, 6, 8, 10], (x, y) => [x, y]).toArray(), [[1, 2], [3, 4], [5, 6], [7, 8]]); + assert.deepEqual(Enumerable.zip([], [], (x, y) => [x, y]).toArray(), []); + }); +}); diff --git a/test/sumTest.js b/test/sumTest.js deleted file mode 100644 index 27d943a..0000000 --- a/test/sumTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import assert from 'assert'; -import sum from '../dist/sum'; - -describe('sum()', () => { - it('should computes the sum of a sequence of numeric values', () => { - assert.strictEqual([1, 2, 3]::sum(), 6); - assert.strictEqual([1, 2, 3]::sum(x => x * 2), 12); - }); -}); diff --git a/test/sumTest.ts b/test/sumTest.ts new file mode 100644 index 0000000..b775d35 --- /dev/null +++ b/test/sumTest.ts @@ -0,0 +1,9 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('sum()', () => { + it('should computes the sum of a sequence of numeric values', () => { + assert.strictEqual(new Enumerable([1, 2, 3]).sum(), 6); + assert.strictEqual(new Enumerable([1, 2, 3]).sum(x => x * 2), 12); + }); +}); diff --git a/test/takeLastTest.js b/test/takeLastTest.js deleted file mode 100644 index c0683f7..0000000 --- a/test/takeLastTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import takeLast from '../dist/takeLast'; - -describe('takeLast()', () => { - it('should returns a specified number of contiguous elements from the end of the sequence', () => { - assert.deepEqual(Array.from([1, 2, 3, 4, 5]::takeLast(0)), []); - assert.deepEqual(Array.from([1, 2, 3, 4, 5]::takeLast(5)), [1, 2, 3, 4, 5]); - assert.deepEqual(Array.from([1, 2, 3, 4, 5]::takeLast(3)), [3, 4, 5]); - }); -}); diff --git a/test/takeLastTest.ts b/test/takeLastTest.ts new file mode 100644 index 0000000..f3fe704 --- /dev/null +++ b/test/takeLastTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import takeLast from '../src/takeLast'; + +describe('takeLast()', () => { + it('should returns a specified number of contiguous elements from the end of the sequence', () => { + assert.deepEqual(Array.from(takeLast.call([1, 2, 3, 4, 5], 0)), []); + assert.deepEqual(Array.from(takeLast.call([1, 2, 3, 4, 5], 5)), [1, 2, 3, 4, 5]); + assert.deepEqual(Array.from(takeLast.call([1, 2, 3, 4, 5], 3)), [3, 4, 5]); + }); +}); diff --git a/test/takeTest.js b/test/takeTest.js deleted file mode 100644 index 4f0907a..0000000 --- a/test/takeTest.js +++ /dev/null @@ -1,16 +0,0 @@ -import assert from 'assert'; -import orderBy from '../dist/orderBy'; -import skip from '../dist/skip'; -import take from '../dist/take'; - -describe('take()', () => { - it('should returns a specified number of contiguous elements from the start of a sequence', () => { - assert.deepEqual(Array.from([1, 2, 3, 4]::take(2)), [1, 2]); - assert.deepEqual(Array.from([1, 2, 3, 4]::take(0)), []); - }); - - it('should works with orderBy()', () => { - assert.deepEqual(Array.from([3, 2, 4, 1]::orderBy()::take(2)), [1, 2]); - assert.deepEqual(Array.from([3, 2, 4, 1]::orderBy()::skip(2)), [3, 4]); - }); -}); diff --git a/test/takeTest.ts b/test/takeTest.ts new file mode 100644 index 0000000..d6497d7 --- /dev/null +++ b/test/takeTest.ts @@ -0,0 +1,16 @@ +import * as assert from 'assert'; +import orderBy from '../src/orderBy'; +import skip from '../src/skip'; +import take from '../src/take'; + +describe('take()', () => { + it('should returns a specified number of contiguous elements from the start of a sequence', () => { + assert.deepEqual(Array.from(take.call([1, 2, 3, 4], 2)), [1, 2]); + assert.deepEqual(Array.from(take.call([1, 2, 3, 4], 0)), []); + }); + + it('should works with orderBy()', () => { + assert.deepEqual(Array.from(take.call(orderBy.call([3, 2, 4, 1]), 2)), [1, 2]); + assert.deepEqual(Array.from(take.call(orderBy.call([]), 2)), []); + }); +}); diff --git a/test/takeWhileTest.js b/test/takeWhileTest.js deleted file mode 100644 index c59fe8c..0000000 --- a/test/takeWhileTest.js +++ /dev/null @@ -1,9 +0,0 @@ -import assert from 'assert'; -import takeWhile from '../dist/takeWhile'; - -describe('takeWhile()', () => { - it('should yield elements from a sequence while a condition is true', () => { - assert.deepEqual(Array.from([1, 2, 3, 4]::takeWhile(x => x % 2 === 0)), []); - assert.deepEqual(Array.from([4, 3, 2, 1]::takeWhile(x => x % 2 === 0)), [4]); - }); -}); diff --git a/test/takeWhileTest.ts b/test/takeWhileTest.ts new file mode 100644 index 0000000..1580608 --- /dev/null +++ b/test/takeWhileTest.ts @@ -0,0 +1,9 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('takeWhile()', () => { + it('should yield elements from a sequence while a condition is true', () => { + assert.deepEqual(new Enumerable([1, 2, 3, 4]).takeWhile(x => x % 2 === 0).toArray(), []); + assert.deepEqual(new Enumerable([4, 3, 2, 1]).takeWhile(x => x % 2 === 0).toArray(), [4]); + }); +}); diff --git a/test/toArrayTest.js b/test/toArrayTest.js deleted file mode 100644 index efd9b2e..0000000 --- a/test/toArrayTest.js +++ /dev/null @@ -1,10 +0,0 @@ -import assert from 'assert'; -import toArray from '../dist/toArray'; - -describe('toArray()', () => { - it('should creates a Array from Iterable', () => { - assert.deepEqual([]::toArray(), []); - assert.deepEqual([1, 2, 3]::toArray(), [1, 2, 3]); - assert.deepEqual([1, 2, 3][Symbol.iterator]()::toArray(), [1, 2, 3]); - }); -}); diff --git a/test/toArrayTest.ts b/test/toArrayTest.ts new file mode 100644 index 0000000..fae0280 --- /dev/null +++ b/test/toArrayTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('toArray()', () => { + it('should creates a Array from Iterable', () => { + assert.deepEqual(new Enumerable([]).toArray(), []); + assert.deepEqual(new Enumerable([1, 2, 3]).toArray(), [1, 2, 3]); + assert.deepEqual(new Enumerable([1, 2, 3][Symbol.iterator]()).toArray(), [1, 2, 3]); + }); +}); diff --git a/test/toDictionaryTest.js b/test/toDictionaryTest.ts similarity index 59% rename from test/toDictionaryTest.js rename to test/toDictionaryTest.ts index 3af2faf..6b36928 100644 --- a/test/toDictionaryTest.js +++ b/test/toDictionaryTest.ts @@ -1,17 +1,17 @@ -import assert from 'assert'; -import toDictionary from '../dist/toDictionary'; +import * as assert from 'assert'; +import Enumerable from '../src'; describe('toDictionary()', () => { it('should creates a Map from Iterable', () => { - let result = []::toDictionary(); + let result = new Enumerable([]).toDictionary(); assert(result instanceof Map); assert.deepEqual(Array.from(result), []); - result = ['a', 'bc', 'def', 'gh', 'i']::toDictionary(x => x.length); + result = new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toDictionary(x => x.length); assert(result instanceof Map); assert.deepEqual(Array.from(result), [[1, 'i'], [2, 'gh'], [3, 'def']]); - result = ['a', 'bc', 'def', 'gh', 'i']::toDictionary(x => x, x => x.length); + result = new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toDictionary(x => x, x => x.length); assert(result instanceof Map); assert.deepEqual(Array.from(result), [['a', 1], ['bc', 2], ['def', 3], ['gh', 2], ['i', 1]]); }); diff --git a/test/toLookupTest.js b/test/toLookupTest.js deleted file mode 100644 index d55da2e..0000000 --- a/test/toLookupTest.js +++ /dev/null @@ -1,18 +0,0 @@ -import assert from 'assert'; -import toLookup from '../dist/toLookup'; - -describe('toLookup()', () => { - it('should creates a generic Map from an Iterable', () => { - let result = []::toLookup(); - assert(result instanceof Map); - assert.deepEqual(Array.from(result), []); - - result = ['a', 'bc', 'def', 'gh', 'i']::toLookup(x => x.length); - assert(result instanceof Map); - assert.deepEqual(Array.from(result), [[1, ['a', 'i']], [2, ['bc', 'gh']], [3, ['def']]]); - - result = ['a', 'bc', 'def', 'gh', 'i']::toLookup(x => x.length, x => x + x); - assert(result instanceof Map); - assert.deepEqual(Array.from(result), [[1, ['aa', 'ii']], [2, ['bcbc', 'ghgh']], [3, ['defdef']]]); - }); -}); diff --git a/test/toLookupTest.ts b/test/toLookupTest.ts new file mode 100644 index 0000000..97bb13d --- /dev/null +++ b/test/toLookupTest.ts @@ -0,0 +1,10 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('toLookup()', () => { + it('should creates a generic Map from an Iterable', () => { + assert.deepEqual(Array.from(new Enumerable([]).toLookup()), []); + assert.deepEqual(Array.from(new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toLookup(x => x.length)), [[1, ['a', 'i']], [2, ['bc', 'gh']], [3, ['def']]]); + assert.deepEqual(Array.from(new Enumerable(['a', 'bc', 'def', 'gh', 'i']).toLookup(x => x.length, x => x + x)), [[1, ['aa', 'ii']], [2, ['bcbc', 'ghgh']], [3, ['defdef']]]); + }); +}); diff --git a/test/unionTest.js b/test/unionTest.js deleted file mode 100644 index ef114af..0000000 --- a/test/unionTest.js +++ /dev/null @@ -1,11 +0,0 @@ -import assert from 'assert'; -import union from '../dist/union'; - -describe('union()', () => { - it('should produces the set union of two sequences', () => { - assert.deepEqual(Array.from([1, 2, 3]::union([2, 3, 4])), [2, 3]); - assert.deepEqual(Array.from([1, 2, 3]::union([])), []); - assert.deepEqual(Array.from([]::union([2, 3, 4])), []); - assert.deepEqual(Array.from([]::union([])), []); - }); -}); diff --git a/test/unionTest.ts b/test/unionTest.ts new file mode 100644 index 0000000..aa90655 --- /dev/null +++ b/test/unionTest.ts @@ -0,0 +1,11 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('union()', () => { + it('should produces the set union of two sequences', () => { + assert.deepEqual(new Enumerable([1, 2, 3]).union([2, 3, 4]).toArray(), [2, 3]); + assert.deepEqual(new Enumerable([1, 2, 3]).union([]).toArray(), []); + assert.deepEqual(new Enumerable([]).union([2, 3, 4]).toArray(), []); + assert.deepEqual(new Enumerable([]).union([]).toArray(), []); + }); +}); diff --git a/test/whereTest.js b/test/whereTest.js deleted file mode 100644 index e6aba58..0000000 --- a/test/whereTest.js +++ /dev/null @@ -1,8 +0,0 @@ -import assert from 'assert'; -import where from '../dist/where'; - -describe('where()', () => { - it('should filters a sequence of values based on a predicate', () => { - assert.deepEqual(Array.from([1, 2, 3, 4]::where(x => x % 2 == 0)), [2, 4]); - }); -}); diff --git a/test/whereTest.ts b/test/whereTest.ts new file mode 100644 index 0000000..4cd867b --- /dev/null +++ b/test/whereTest.ts @@ -0,0 +1,8 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('where()', () => { + it('should filters a sequence of values based on a predicate', () => { + assert.deepEqual(new Enumerable([1, 2, 3, 4]).where(x => x % 2 == 0).toArray(), [2, 4]); + }); +}); diff --git a/test/whileTest.js b/test/whileTest.ts similarity index 65% rename from test/whileTest.js rename to test/whileTest.ts index 9ce00fd..b2e74e0 100644 --- a/test/whileTest.js +++ b/test/whileTest.ts @@ -1,5 +1,5 @@ -import _while from '../dist/while'; -import assert from 'assert'; +import Enumerable from '../src'; +import * as assert from 'assert'; describe('while()', () => { it('should generates an enumerable sequence by repeating a source sequence as long as the given loop condition holds', () => { @@ -9,6 +9,6 @@ describe('while()', () => { yield x-- } }; - assert.deepEqual(Array.from(xs::_while(() => x > 0)), [5, 4, 3, 2, 1]); + assert.deepEqual(new Enumerable(xs).while(() => x > 0).toArray(), [5, 4, 3, 2, 1]); }); }); diff --git a/test/zipTest.js b/test/zipTest.js deleted file mode 100644 index 80a4006..0000000 --- a/test/zipTest.js +++ /dev/null @@ -1,11 +0,0 @@ -import assert from 'assert'; -import zip from '../dist/zip'; - -describe('zip()', () => { - it('should applies a specified function to the corresponding elements of two sequences', () => { - assert.deepEqual(Array.from([1, 3, 5, 7]::zip([2, 4, 6, 8], (x, y) => [x, y])), [[1, 2], [3, 4], [5, 6], [7, 8]]); - assert.deepEqual(Array.from([1, 3, 5, 7, 9]::zip([2, 4, 6, 8], (x, y) => [x, y])), [[1, 2], [3, 4], [5, 6], [7, 8]]); - assert.deepEqual(Array.from([1, 3, 5, 7]::zip([2, 4, 6, 8, 10], (x, y) => [x, y])), [[1, 2], [3, 4], [5, 6], [7, 8]]); - assert.deepEqual(Array.from([]::zip([], (x, y) => [x, y])), []); - }); -}); diff --git a/test/zipTest.ts b/test/zipTest.ts new file mode 100644 index 0000000..0970b23 --- /dev/null +++ b/test/zipTest.ts @@ -0,0 +1,11 @@ +import * as assert from 'assert'; +import Enumerable from '../src'; + +describe('zip()', () => { + it('should applies a specified function to the corresponding elements of two sequences', () => { + assert.deepEqual(new Enumerable([1, 3, 5, 7]).zip([2, 4, 6, 8], (x, y) => [x, y]).toArray(), [[1, 2], [3, 4], [5, 6], [7, 8]]); + assert.deepEqual(new Enumerable([1, 3, 5, 7, 9]).zip([2, 4, 6, 8], (x, y) => [x, y]).toArray(), [[1, 2], [3, 4], [5, 6], [7, 8]]); + assert.deepEqual(new Enumerable([1, 3, 5, 7]).zip([2, 4, 6, 8, 10], (x, y) => [x, y]).toArray(), [[1, 2], [3, 4], [5, 6], [7, 8]]); + assert.deepEqual(new Enumerable([]).zip([], (x, y) => [x, y]).toArray(), []); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 36f46d6..d4a728d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,5 +11,12 @@ }, "exclude": [ "node_modules" + ], + "include": [ + "src/**/*.ts", + "test/**/*.ts" + ], + "files": [ + "typings/index.d.ts" ] } diff --git a/typings.json b/typings.json new file mode 100644 index 0000000..9adcf26 --- /dev/null +++ b/typings.json @@ -0,0 +1,9 @@ +{ + "globalDevDependencies": { + "mocha": "registry:dt/mocha#2.2.5+20160720003353", + "node": "registry:env/node#6.0.0+20160918225031" + }, + "dependencies": { + "sinon": "registry:npm/sinon#1.16.0+20160723033700" + } +}