Skip to content

Commit

Permalink
Start using native ArrayIterators which use native array functions
Browse files Browse the repository at this point in the history
- switch to babel.config.cjs instead of .babelrc
- update dependencies
  • Loading branch information
Indomitable committed Dec 24, 2019
1 parent b36f53d commit acb1a20
Show file tree
Hide file tree
Showing 21 changed files with 1,289 additions and 1,328 deletions.
16 changes: 0 additions & 16 deletions .babelrc

This file was deleted.

23 changes: 23 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = function (api) {
api.cache(true);
api.retainLines = true;
const presets = [ '@babel/preset-env' ];
const plugins = [
[
"module-resolver",
{
"alias": {
"modern-linq": "./index.esm.js"
}
}
],
[
"@babel/plugin-proposal-private-methods",
{ "loose": true }
]
];
return {
presets,
plugins
}
};
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@
"LICENSE"
],
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/node": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/register": "^7.5.5",
"@types/chai": "^4.2.0",
"@babel/cli": "^7.7.7",
"@babel/core": "^7.7.7",
"@babel/plugin-proposal-private-methods": "^7.7.4",
"@babel/preset-env": "^7.7.7",
"@babel/register": "^7.7.7",
"@types/chai": "^4.2.7",
"@types/mocha": "^5.2.7",
"babel-plugin-module-resolver": "^3.2.0",
"babel-plugin-module-resolver": "^4.0.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"microtime": "^3.0.0",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"rollup": "^1.17.0",
"mocha": "^6.2.2",
"nyc": "^15.0.0",
"rollup": "^1.27.14",
"rollup-plugin-babel": "^4.3.3",
"typescript": "^3.5.3"
"typescript": "^3.7.4"
}
}
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default {
},
plugins: [
babel({
configFile: './babel.config.cjs',
exclude: 'node_modules/**'
})
]
Expand Down
86 changes: 7 additions & 79 deletions src/creation.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,12 @@
import { RangeIterable } from "./generators/range";
import { BaseLinqIterable } from "./base-linq-iterable";
import { RepeatIterable } from "./generators/repeat";
import { getIterator, doneValue, iteratorResultCreator } from "./utils";

export class LinqIterable extends BaseLinqIterable {
constructor(source) {
super(source);
}

get() {
return this.source;
}

[Symbol.iterator]() {
return getIterator(this.source);
}
}

export class ArrayLikeIterable extends BaseLinqIterable {
constructor(source) {
super(source);
}

get() {
return Array.isArray(this.source) ? this.source : this;
}

[Symbol.iterator]() {
if (Array.isArray(this.source)) {
return getIterator(this.source);
}
const length = this.source.length;
const source = this.source;
let current = 0;
return {
next() {
if (current < length) {
const value = source[current];
current++;
return iteratorResultCreator(value);
} else {
return doneValue();
}
}
};
}
}

export class ObjectIterable extends BaseLinqIterable {
constructor(source, resultCreator) {
super(source);
this.resultCreator = typeof resultCreator === 'undefined' ? ObjectIterable.__defaultResultCreator : resultCreator;
}

static __defaultResultCreator(key, value) {
return { key, value };
}

[Symbol.iterator]() {
const obj = this.source;
const resultCreator = this.resultCreator;
const keys = Object.keys(obj);
let index = 0;
return {
next() {
if (index < keys.length) {
const key = keys[index];
const value = obj[key];
index++;
return iteratorResultCreator(resultCreator(key, value));
} else {
return doneValue();
}
}
};
}
}
import RangeIterable from './generators/range';
import RepeatIterable from './generators/repeat';
import LinqIterable from './iterables/initial/linq';
import ArrayIterable from './iterables/initial/array';
import ArrayLikeIterable from './iterables/initial/array-like';
import ObjectIterable from './iterables/initial/object';

export function fromIterable(source) {
return new LinqIterable(source);
return Array.isArray(source) ? new ArrayIterable(source) : new LinqIterable(source);
}

export function fromObject(obj, resultCreator) {
Expand Down
15 changes: 13 additions & 2 deletions src/finalizers/to-array.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { ArrayFilterIterable } from "../iterables/where";

export class ToArrayFinalizer {
static get(source, map) {
if (!map) {
Expand All @@ -10,3 +8,16 @@ export class ToArrayFinalizer {
}
}
}

/**
* Return it is toArray finalizer which is used when source.get is 100% an array.
* Used in native array iterators
*/
export class ToArrayArrayFinalizer {
static get(source, mapper) {
if (!mapper) {
return source.get();
}
return source.map(mapper).toArray();
}
}
9 changes: 5 additions & 4 deletions src/generators/range.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/**
* Generates range of numbers [from, to)
*/
import { BaseLinqIterable } from "../base-linq-iterable";

export class RangeIterable extends BaseLinqIterable {
export default class RangeIterable {
/**
* The range is [from, to)
* @param {number} from
* @param {number} to
*/
constructor(from, to) {
super([]);
this.from = from;
this.to = to;
}
Expand Down Expand Up @@ -56,4 +53,8 @@ export class RangeIterable extends BaseLinqIterable {
}
};
}

get() {
return this;
}
}
8 changes: 5 additions & 3 deletions src/generators/repeat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { BaseLinqIterable } from "../base-linq-iterable";
export class RepeatIterable extends BaseLinqIterable {
export default class RepeatIterable {
constructor(value, times) {
super([]);
this.value = value;
this.times = times;
}
Expand All @@ -21,4 +19,8 @@ export class RepeatIterable extends BaseLinqIterable {
}
};
}

get() {
return this;
}
}
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { applyMixin } from './utils';
import { ArrayLikeIterable, LinqIterable, ObjectIterable } from './creation';

import LinqIterable from './iterables/initial/linq';
import ArrayIterable from './iterables/initial/array';
import ArrayLikeIterable from './iterables/initial/array-like';
import ObjectIterable from './iterables/initial/object';

import { linqMixin } from './linq-mixin';
import { ArrayFilterIterable, WhereIterable } from './iterables/where';
import { SelectIterable } from './iterables/select';
import { SelectManyIterable } from './iterables/select-many';
import { TakeIterable } from './iterables/take';
import { SkipIterable } from './iterables/skip';
import { RangeIterable } from './generators/range';
import RangeIterable from './generators/range';
import { DistinctIterable } from './iterables/distinct';
import { Grouping, GroupIterable } from './iterables/group';
import { OrderIterable, OrderIterableDescending } from "./iterables/order";
import { ConcatIterable } from "./iterables/concat";
import { UnionIterable } from "./iterables/union";
import { GroupJoinIterable } from "./iterables/group-join";
import { JoinIterable } from "./iterables/join";
import { RepeatIterable } from "./generators/repeat";
import RepeatIterable from './generators/repeat';
import { PageIterable } from "./iterables/page";
import { ReverseIterable } from "./iterables/reverse";
import { TakeWhileIterable } from "./iterables/take-while";
Expand All @@ -26,6 +31,7 @@ import { IntersectIterable } from "./iterables/intersect";
// note: if using class as output we can just apply the mixin to BaseLinqIterable.
applyMixin(linqMixin, [
LinqIterable,
ArrayIterable,
ArrayLikeIterable,
ObjectIterable,
WhereIterable,
Expand Down
23 changes: 10 additions & 13 deletions src/iterables/distinct.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,26 @@
import { BaseLinqIterable } from "../base-linq-iterable";
import {getIterator} from "../utils";

/**
* Returns distinct values
*/
export class DistinctIterable extends BaseLinqIterable {
export class DistinctIterable {
/**
*
* @param {Iterable} source
* @param {Function} comparer comparer function. if not provider use native Set.
*/
constructor(source, comparer) {
super(source);
this.source = source;
this.comparer = comparer;
}

get() {
if (!this.comparer) {
return new Set(this._getSource());
}
return this;
}

[Symbol.iterator]() {
const source = this._getSource();
const source = this.source.get();
if (!this.comparer) {
const set = new Set(source);
return this._getIterator(set);
return getIterator(set);
}
const iterator = this._getIterator(source);
const iterator = getIterator(source);
const itemChecker = new DistinctItemChecker(this.comparer);
return {
next() {
Expand All @@ -46,6 +39,10 @@ export class DistinctIterable extends BaseLinqIterable {
}
};
}

get() {
return this;
}
}

class DistinctItemChecker {
Expand Down
31 changes: 31 additions & 0 deletions src/iterables/initial/array-like.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {doneValue, getIterator, iteratorResultCreator} from "../../utils";

export default class ArrayLikeIterable {
constructor(source) {
this.source = source;
}

get() {
return Array.isArray(this.source) ? this.source : this;
}

[Symbol.iterator]() {
if (Array.isArray(this.source)) {
return getIterator(this.source);
}
const length = this.source.length;
const source = this.source;
let current = 0;
return {
next() {
if (current < length) {
const value = source[current];
current++;
return iteratorResultCreator(value);
} else {
return doneValue();
}
}
};
}
}
25 changes: 25 additions & 0 deletions src/iterables/initial/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {getIterator} from "../../utils";
import {ArrayFilterIterable} from "../where";
import {ToArrayArrayFinalizer} from "../../finalizers/to-array";

export default class ArrayIterable {
constructor(array) {
this.array = array;
}

[Symbol.iterator]() {
return getIterator(this.array);
}

get() {
return this.array;
}

where(predicate) {
return new ArrayFilterIterable(this.array, predicate);
}

toArray(mapper) {
return ToArrayArrayFinalizer.get(this, mapper);
}
}
Loading

0 comments on commit acb1a20

Please sign in to comment.