Skip to content

Commit

Permalink
Documentation improvements and additional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaifm committed Sep 29, 2021
1 parent 9adbe63 commit 6e79333
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 82 deletions.
133 changes: 111 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
## linq
This is a javascript implementation of the .NET [LINQ](https://msdn.microsoft.com/en-us/library/bb308959.aspx) library.
# linq

It contains all the original .NET methods plus a few additions.

### Installation
This is a JavaScript implementation of the .NET [LINQ](https://msdn.microsoft.com/en-us/library/bb308959.aspx) library.

With npm:

npm install linq
It contains all the original .NET methods plus a few additions.

CDN availability:
Written in pure JavaScript with no dependencies.

| CDN | URL |
|-----------:|:-------------------------------------------|
| `unpkg` | <https://unpkg.com/linq/> |
| `jsDelivr` | <https://jsdelivr.com/package/npm/linq> |
| `packd` | <https://bundle.run/linq@latest?name=linq> |
## Examples

### Examples
```js
// C# LINQ - delegate
Enumerable.Range(1, 10)
Expand All @@ -32,26 +22,125 @@ Enumerable.range(1, 10)

```js
// C# LINQ - lambda
Enumerable.Range(1, 10).Where(i => i % 3 == 0).Select(i => i * 10);
Enumerable.Range(1, 10).Where((i) => i % 3 == 0).Select((i) => i * 10);

// linq.js - arrow function
Enumerable.range(1, 10).where(i => i % 3 == 0).select(i => i * 10);
Enumerable.range(1, 10).where((i) => i % 3 == 0).select((i) => i * 10);
```

```js
// C# LINQ - anonymous type
array.Select((val, i) => new { Value = val, Index = i });
array.Select((val, i) => new { Value: val, Index: i }());

// linq.js - object literal
Enumerable.from(array).select((val, i) => ({ value: val, index: i}));
Enumerable.from(array).select((val, i) => ({ value: val, index: i }));
```

See [sample/tutorial.js](https://github.com/mihaifm/linq/blob/master/sample/tutorial.js) and the [test](https://github.com/mihaifm/linq/tree/master/test) folder for more examples.

# Usage

## Node.js (ES modules)

Install the latest version of the library with npm:

npm install linq

Load it in your code with the `import` syntax:

```js
import Enumerable from 'linq'

let result = Enumerable.range(1, 10).where(i => i % 3 == 0).select(i => i * 10)
console.log(result.toArray()) // [ 30, 60, 90 ]
```

Because the library is an ES module, this code will only work if your project is also configured as an ES module. Add the following line in your `package.json` to make it an ES module:

```json
"type": "module"
```

If you're not planning to use ES modules, check the CommonJS section below.

## Node.js (CommonJS modules)

Install version 3 of this library:

npm install linq@3

Load it with the `require` syntax:

```js
const Enumerable = require('linq')

let count = Enumerable.range(1, 10).count(i => i < 5)
console.log(count) // 4
```

The [cjs](https://github.com/mihaifm/linq/tree/cjs) branch contains the source code for the CommonJS version of the library.

## TypeScript

Install the latest version of the library with npm.

Configure your compiler options in `tsconfig.json`

```json
"compilerOptions": {
"target": "ES2020",
"moduleResolution": "node"
}
```

The library comes with a `d.ts` file containing type definitions for all the objects and methods, feel free to use them in your code:

```ts
import Enumerable from 'linq';

type tnum = Enumerable.IEnumerable<number>;
let x: tnum = Enumerable.from([1, 2, 3]);
```

See [sample/tutorial.js](https://github.com/mihaifm/linq/blob/master/sample/tutorial.js) for more examples.
## Deno

Install the latest version of the library with npm.

Use the full file path when importing the library. Also use the `@deno-types` annotation to load type definitions:

```ts
// @deno-types="./node_modules/linq/linq.d.ts"
import Enumerable from './node_modules/linq/linq.js'

let radius = Enumerable.toInfinity(1).where(r => r * r * Math.PI > 10000).first()
```

## Browser

The minified version of the library is available in the [release](https://github.com/mihaifm/linq/releases/latest) archive.

Load it via `<script type="module">`:

```html
<script type="module" src="./linq.min.js"></script>
<script type="module">
import Enumerable from './linq.min.js'
Enumerable.from([1, 2, 3]).forEach(x => console.log(x))
</script>
```

You can also load the library via a CDN:

| CDN | URL |
| ---------: | :----------------------------------------- |
| unpkg | <https://unpkg.com/linq/> |
| jsDelivr | <https://jsdelivr.com/package/npm/linq> |
| packd | <https://bundle.run/linq@latest?name=linq> |

### People
# Credits

[Yoshifumi Kawai](https://github.com/neuecc) developed the [original version](http://linqjs.codeplex.com/) of this library, currently no longer maintained.

### License
# License

[MIT License](https://github.com/mihaifm/linq/blob/master/LICENSE)
10 changes: 3 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@
"author": "Mihai Ciuraru <mihaixc@gmail.com>",
"description": "linq.js - LINQ for JavaScript",
"type": "module",
"version": "3.2.4",
"version": "4.0.0",
"license": "MIT",
"homepage": "https://github.com/mihaifm/linq",
"repository": {
"type": "git",
"url": "https://github.com/mihaifm/linq.git"
},
"scripts": {
"test": "node test/testrunner.js",
"minify": "uglifyjs linq.js -c -m -o linq.min.js"
"test": "node test/testrunner.js"
},
"preferGlobal": false,
"keywords": [
"linq"
],
"dependencies": {},
"devDependencies": {
"uglify-js": "3.10.3"
},
"engines": {
"node": "*"
},
"main": "./linq",
"main": "./linq.js",
"exports": "./linq.js",
"types": "./linq.d.ts"
}
2 changes: 1 addition & 1 deletion sample/tutorial.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Enumerable = require('../linq');
import Enumerable from '../linq.js'

/////////////////////////////
// Simple Lambda Expressions
Expand Down
52 changes: 0 additions & 52 deletions util/gpg_keys/mihaifm.asc

This file was deleted.

0 comments on commit 6e79333

Please sign in to comment.