Coder Social home page Coder Social logo

isc30 / linq-collections Goto Github PK

View Code? Open in Web Editor NEW
118.0 9.0 13.0 555 KB

Strongly typed Linq and Collections implementation for Javascript and TypeScript (ECMAScript 5)

Home Page: https://www.npmjs.com/package/linq-collections

License: MIT License

TypeScript 50.68% JavaScript 49.17% HTML 0.15%
linq typescript strongly-typed performance statically-typed collections javascript

linq-collections's Introduction

Linq-Collections: (IEnumerable, ...) + (List, Dictionary, ...)

npm version npm downloads build status coverage

Strongly typed Linq implementation for Javascript and TypeScript (ES5, ES6, +)
Includes collections (+ readonly versions): List, Dictionary, Stack, ...

Current Stable Version

https://github.com/isc30/linq-collections
This project was developed by Ivan Sanz (isc30)

The project is already finished, yet some features are missing. If you want to contribute with any of these, please check the Development status and missing features list
I will be happy to accept pull requests :D

Intellisense friendly

Every single method has complete type definitions available.
If you use TypeScript, its purely is based in generics.

[Insert motivational GIF with intellisense in action]

Browser compatibility: 100%

Using ES5, it has 100% compatibility with nodejs and all main browsers (+mobile)
Check your browser now if you don't believe it ->

Performance

Linq-Collections uses custom iterators and deferred execution mechanisms that ensure BLAZING FAST operations, outperforming any other popular library. Its also optimized to work with minimal CPU and RAM usage.

Why use it?

If previous reasons aren't enought, here are few more:

  • Javascript && TypeScript compatible - You can use it with JS or TypeScript (contains .d.ts definitions)
  • No dependencies - Pure and lightweight
  • 100% browser/nodejs support - Stop caring about compatibility, it works everywhere!
  • Strongly typed - Developed in TypeScript, it uses no 'any' or dirty code. Everything is based in generics and strongly typed
  • Best performance - Deferred execution with custom iterators make the difference. Currently the fastest library.
  • Works out of the box - 'npm install linq-collections' is the hardest thing you'll need to do
  • Collections - Provides many type of collections (list, dictionary, ... + readonly) with linq integrated inside. As in C#
  • Strict standard - Strictly implementing microsoft's official linq definition (you can check it for exceptions, behavior, etc)
  • Deeply tested - Each new version is passing tons of quality tests before being released

Using the package

Interfaces for this library are already designed. New versions won't break any old code. We strongly recommend using * for version selector

dependencies {
    "linq-collections": "1.*"
}

Features

Complete Linq to Objects implementation (deferred execution)

toArray, toList, toDictionary, toLookup, aggregate, all, any, average, concat, contains, count, defaultIfEmpty, distinct, elementAt, elementAtOrDefault, except, first, firstOrDefault, forEach, groupBy, groupJoin, intersect, join, last, lastOrDefault, longCount, max, min, orderBy, orderByDescending, reverse, select, selectMany, sequenceEquals, single, single, singleOrDefault, skip, skipWhile, sum, take, takeWhile, union, where, zip, ...

Collections (+ readonly versions)

List, Dictionary, Stack, Queue, ...

All Collections are Queryable

const list = new List<string>([
    "Hello",
    "Bye",
    "Thanks",
]);

const notHello = list.where(e => e !== "Hello");

How to run tests

This library uses mocha with custom assertion helper for testing.
Use nyc mocha to run the tests and coverage.

Hall of fame

linq-collections's People

Contributors

isc30 avatar nikolalukovic avatar tholdrim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

linq-collections's Issues

SkipWhile

SkipWhile will only skip those elements in list until predicate is true and after that, it will stop filtering.

Proposed signature:

export interface IQueryable<TOut>
{
    skipWhile(predicate: Predicate<TOut>): IEnumerable<TOut>;
}

Using deferred execution

Intersects

Produces the set intersection of two sequences (deferred execution)

Proposed signature:

export interface IQueryable<TOut>
{
    intersect(other: IQueryable<TOut>): IEnumerable<TOut>;
}

Roadmap/missing features

Hi, Any chance you could publish a roadmap, or other method for people to understand what's meant by "Please expect some missing features until it's finished."?

Otherwise looking excellent.

I cloned the source and a error has occur.

I'm using tsc 3.2.2

after call npm install

src/Collections.ts:1004:13 - error TS2322: Type '"string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"' is not assignable to type 'Type'.
Type '"bigint"' is not assignable to type 'Type'.

1004 this.keyType = typeof key;
~~~~~~~~~~~~

warning when building

‼ Could not load source file "../../src/Linq.ts" in source map of "../node_modules/linq-collections/build/src/Linq.js".
‼ Could not load source file "../../src/Enumerables.ts" in source map of "../node_modules/linq-collections/build/src/Enumerables.js".
‼ Could not load source file "../../src/Iterators.ts" in source map of "../node_modules/linq-collections/build/src/Iterators.js".
‼ Could not load source file "../../src/Comparers.ts" in source map of "../node_modules/linq-collections/build/src/Comparers.js".
‼ Could not load source file "../../src/Collections.ts" in source map of "../node_modules/linq-collections/build/src/Collections.js".
‼ Could not load source file "../../src/Utils.ts" in source map of "../node_modules/linq-collections/build/src/Utils.js".

ToLookup

A lookup is a 1:many inmutable map (multi-map; each key is mapped to an IQueryable<> of the values with that key).

Proposed definition:

toLookup<TKey extends Indexer, TValue>(
        keySelector: Selector<TOut, TKey>,
        valueSelector: Selector<TOut, TValue>)
        : IDictionary<TKey, IQueryable<TValue>>;

Can be implemented in a very similar way to GroupBy from List class

Zip

Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.

Proposed definition:

zip<TOther, TSelector>(
    other: TOther[] | IQueryable<TOther>,
    selector: (TOut, TOther) => TSelector): IEnumerable<TSelector>;

Uses deferred execution

ToLookup

A lookup is a 1:many inmutable map (multi-map; each key is mapped to an IQueryable<> of the values with that key).

Proposed definition:

toLookup<TKey extends Indexer, TValue>(
        keySelector: Selector<TOut, TKey>,
        valueSelector: Selector<TOut, TValue>)
        : IDictionary<TKey, IQueryable<TValue>>;

Can be implemented in a very similar way to GroupBy from List class

Benchmarks (linq.ts vs linq-es2015 vs linq-collections)

Currently benchmarking versus the more popular libraries: linq.ts and linq-es2015

Distinct (100k elements)

#5fba7d linq-collections: 116 ops/s
#d0d0d0 linq-es2015: 17.7 ops/s
#f03c15 linq.ts: 0.18 ops/s

OrderBy (500k elements)

#5fba7d linq.ts: 3.81 ops/s
#d0d0d0 linq-collections: 3.74 ops/s
#f03c15 linq-es2015: 2.26 ops/s

Join

Correlates the elements of two sequences based on matching keys

Proposed definition:

join<TInner, TKey, TResult>(
    inner: IQueryable<TInner>,
    pkSelector: Selector<TOut, TKey>,
    fkSelector: Selector<TInner, TKey>,
    resultSelector: ((outer: TOut, inner: TInner) => TResult)): TResult;

Using deferred execution

SkipWhile

SkipWhile will only skip those elements in list until predicate is true and after that, it will stop filtering.

Proposed signature:

export interface IQueryable<TOut>
{
    skipWhile(predicate: Predicate<TOut>): IEnumerable<TOut>;
}

Using deferred execution

Missing license

This library looks very promising, however it is missing a license file and license in package.json. This might throw off potential users.

SequenceEqual

Determines whether two sequences are equal by comparing the elements

Proposed signature:

export interface IQueryable<TOut>
{
    sequenceEqual(other: IQueryable<TOut>): boolean;
}

`readonly RowAccessor[]` is not assignable to parameter of type `RowAccessor[]` ?

This interface:

export declare interface IListViewCommandSetExecuteEventParameters {
    /**
     * The unique identifier for the command.  This is specified as ICommandDefinition.commandId
     * in the component manifest.
     */
    readonly itemId: string;
    /**
     * The currently selected ListView rows, at the time when the event occurred.
     */
    readonly selectedRows: ReadonlyArray<RowAccessor>;
}

Cannot be used in new List(...) or Enumerable :
image

Hash and Equality

To properly create a dictionary/set etc two things are needed in .net

getHashcode method to get the hash of the object to know in which bucket it must be placed
an equals method to know which object to pick up on the bucket if the hash collides

I don't see any implementation of those in the collections, how are they handled by this library?

SequenceEqual

Determines whether two sequences are equal by comparing the elements

Proposed signature:

export interface IQueryable<TOut>
{
    sequenceEqual(other: IQueryable<TOut>): boolean;
}

DefaultIfEmpty

Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty

defaultIfEmpty(): IEnumerable<TOut | undefined>;
defaultIfEmpty(defaultValue: TOut): IEnumerable<TOut>;

Uses streamed deferred execution

Readonly collections?

Hey...

I'm still getting busy trying to add contributions, don't think I've forgotten 😄

In the meantime, I just wondered if you've thought about adding 'readonly' collections? I know it's never going to be exactly perfect in the typescript/javascript world, but it's a nice addition to imply usage to users of exposed properties.

Thanks.

Firebase Javascript Objects?

Hi,

I am trying to use your library with Google Firebase Realtime Database JavaScript objects. I am having an issue well trying to create a new Dictionary<string, Object> which doesn't seem to be working well with JavaScript objects. The object I am trying to parse looks like this.

var objdict: Dictionary<string, Object> = new Dictionary<string, Object>(event[0]);


->Object {-L-j7Z4jxiQiSAKBGxSD: Object, -L-j7Z4lKxlERKPCtKLk: Object}
---> -L-j7Z4jxiQiSAKBGxSD:Object {ExpirationDate: "9999-12-31T00:00:00.000Z", Name: "Object 1", StartDate: "2017-12-07T04:00:58.990Z"}
---> -L-j7Z4lKxlERKPCtKLk:Object {ExpirationDate: "2017-12-08T04:00:58.990Z", Name: "Object 2", StartDate: "2017-12-07T04:00:58.990Z"}

I was stepping into the code to try and figure out what was the issue, because I am pretty new in JavaScript and noticed. in the Collections.js compiled version It is trying to find the length of the keyValuePairs.length which doesn't seem to work with JavaScriptObjects? If i do Object.keys(keyValuePairs).length it will find the correct length for the total amount of keys within that object.

Am I using dictionaries worry or is there anyway I can use your current linq-collection with javascript objects and or is there a way I can fix this issue so I can use linq-collection?

Thanks for your help.

Proper iterator testing

Add new iterables to iterator test factory:

  • DefaultIfEmptyEnumerable
  • TakeWhileEnumerable
  • SkipWhileEnumerable

How to group by multiple keys and select those keys?

Doing a single key looks straight-forward.

var list = new List(myObjectArray)
  var result = list
    .groupBy(p => p.page)
    .select(g => ({
      page: g.key,
      count: g.value.count(),
    }))
    .toArray()

But how do I do multiple keys?

var list = new List(myObjectArray)
  var result = list
    .groupBy(p => ({ page: p.page, date: moment(p.dateTime).format('YYYY-MM-DD')}))
    .select(g => ({
      page: g.key.page, <-- ???
      date: g.key.date, <-- ???
      count: g.value.count(),
    }))
    .toArray()

Intersect

Produces the set intersection of two sequences (deferred execution)

Proposed signature:

export interface IQueryable<TOut>
{
    intersect(other: IQueryable<TOut>): IEnumerable<TOut>;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.