eniks / linq Goto Github PK
View Code? Open in Web Editor NEWTypeScript Implementation of Language-Integrated Query (LINQ) (ECMAScript 2015)
License: Apache License 2.0
TypeScript Implementation of Language-Integrated Query (LINQ) (ECMAScript 2015)
License: Apache License 2.0
If the type of source is an Array, index is used to obtain the element at the specified index. Otherwise, this method iterates the specified element.
When importing, I get the message "Error TS2306 File 'D:/XXX/node_modules/linq-es2015/dist/linq.d.ts' is not a module." I use the import like this: "import {asEnumerable, Range} from 'linq-es2015";"
Furthermore I have a list of messages like "Cannot find name 'iterable'".
I also tried to load the sources with "import {asEnumerable, Range} from "linq-es2015/src/linq.ts";". But then I get a "JsErrorScriptException (0x30001)" without being related to some specific file.
Error found in Visual Studio 2015
Before I put a a repo app on plunkr I would like to know wether this is a bug or my excpectations are just wrong.
Thats the code I run
let pageCount = 11;
let resultLength = result.length;
let gradingKeys = Enumerable.From(result)
.Select((item, index) => {
return { grade: item.grade, scores: item.scores, index: Math.floor(item.index / pageCount) }
// Unexpected behavior: as the result array has a length of 11 the ChunkBy returns an empty array?
}
)
.ChunkBy(g => g.index)
.ToArray();
For all 11 objects in the result array I do a Select function where each anonym object 'index' property is calculated to 0.
My expectation how it should work
That means when when the ChunkBy on the index property is done I expected a flat list of 11 items.
How it really worked
After the ChunkBy is done I got an array of length 0
My assumption
Incorrect behavior
Correct behavior
Would you please be that kind and tell me wether the ChunkBy has bug or I need to fix things on my side. Also I must say I would prefer on your side so everyone else has not to do it, but lets discuss it ;-)
If no predicate is specified and the type of source implements Count or Length, that implementation is used to obtain the count of elements. Otherwise, this method determines the count.
A lot of functions pass undefined
as their _target
when creating a new EnumerableImpl
, and a lot of other functions are then using this _target
and will of course crash.
Here's a demo (in this specific demo, Where
creates an undefined
which then crashes Any
):
https://jsfiddle.net/rdw5uetf/
Hi, first of all thanks for the package. After a few checks on linq packages on npm. This is the only one that worked for me and still being maintained.
There is a small problem, node.js gives me
error TS2304: Cannot find name 'Iterable'.
upon build. That's because *.d.ts files reference Iterable<T>
but there is no definition for it.
Adding
interface Iterable<T>{
}
to bothlinq.d.ts
and enumerable.d.ts
and
export interface Map<TKey, TElement>{
}
toenumerable.d.ts
helped me get rid of the errors.
I Use angular-cli to create an angular project, and import linq-es2015.
When I typed:
ng build --prod --aot
there was an error:
8ef7c4cbb4dc50.chunk.js from UglifyJs Unexpected token: name (CSharpEnumerator) [0.4cff4e8ef7c4cbb4dc50.chunk.js:872,6]
Here is a DEMO that can reappearance the problem.
How do I use this with Webpack/Angular 2?
I quite like the look of this project; but it looks like it needs updating.
For example, with the latest TypeScript you can let the compiler deal with generating the definitions.
You can also use the "typings" field in package.json
(point it to the typings definition), that way when people install your module via npm, typescript and IDE's can automatically pick up the typings without fanfare.
Should also have a dist folder containing the typescript compiled, at least, for system and commonjs module formats (with package.json pointing to the commonjs one. That way you can support node.js apps and browser-based apps (that use module loaders).
Throw an exception when ThenBy() is called on anything but OrderBy().
Range(start: number, count: number): Enumerable;
Branch | Build failing π¨ |
---|---|
Dependency | typescript |
Current Version | 2.4.0 |
Type | devDependency |
This version is covered by your current version range and after updating it in your project the build failed.
As typescript is βonlyβ a devDependency of this project it might not break production or downstream projects, but βonlyβ your build or test tools β preventing new deploys or publishes.
I recommend you give this issue a high priority. Iβm sure you can resolve this πͺ
The new version differs by 141 commits.
8b2fe13
Update LKG.
14d95ed
Test:Block-scoped definition of Promise works
a8846bf
Skip block scope check with no error location
44f2336
Merge pull request #16633 from Microsoft/release-2.4_fixIncrementalParsing
4875a27
Add tests
15ef20d
Set the structureReused to be safemoudles when dynamic import change
6d33083
Add tests
11b9f6e
Wip-fix incremental parsing
2721fd4
In TypeScript code, never bind JSDoc normally, just set parent pointers (#16555) (#16561)
0968ed9
Revert string enum changes (#16569)
096f8cc
Update LKG
9241175
Allow running in strict mode (#16557)
f49b007
Update LKG
f1b0f59
Update version to 2.4.1
ed9cde9
Update LKG
There are 141 commits in total.
See the full diff
There is a collection of frequently asked questions and of course you may always ask my humans.
Your Greenkeeper Bot π΄
import { asEnumerable as linq } from 'linq-es2015';
var b = linq("Volvo Black jejtyj 2005").Contains("Volvo");
console.log(b); // false
Hi,
I tried to run the follwing code:
let t :any[] = [{name:'royi'}, {name:'david'}, {name:'barak'}, {name:'moshe'}];
let o = asEnumerable(t).OrderBy(s=> s.name).ToArray();
console.log(o);
It print in the original order...
I also try to define name string propety in type script class and orderby this property also do nothing..
Thanks,
Royi
I am using the following libraries:
Angular 2.0.1
Angular-CLI 1.0.0-Beta.16 (using webpack)
linq-es2015 2.4.1
TypeScript 2.0.3
With npm I did:
npm install linq-es2015
npm install @types/linq-es2015
I want to import your linq library in MyComponent.ts:
import * as Enumerable from 'linq-es2015';
In the angularcli configuration file I modify the scripts array:
"scripts": ["../node_modules/linq-es2015/dist/linq.js"],
Then I run my application and in the browser console I get that error:
Can not find module "." in your linq.js. line 16
The first thing I realized is that in my project under node_modules/@types/ there exist no linq-es2015 folder at all.
But I do not think thats the problem why I get that error during runtime.
I have still added a screenshot from chrome browser:
Hi,
please see this thread: http://stackoverflow.com/questions/419019/split-list-into-sublists-with-linq
Microsoft also have an implementation of the ChunkBy extension method:
https://msdn.microsoft.com/de-de/library/cc138361.aspx
They just forgot... it to put it into the .net framework ;-)
You see any chance that this gets added to your cool library?
See also this table as sample: 15 items per table
When two sequences are the same length, there is one more iteration than there should be (compared to C#). It matters when using a custom comparer, unless it checks for undefined. See
https://jsfiddle.net/ralphwaldo29/xj5aox7k/
Created enumerable could be used only once.
Produces the set intersection of two sequences by using the specified key selector to compare elements.
Aggregate(Func<TSource, TSource, TSource>) Applies an accumulator function over a sequence.
Not sure if this method makes sense in JavaScript
import { asEnumerable as linq } from 'linq-es5';
const yx = [
{ id: '1', batchNumber: 'ZKFM1', value: 'zzz' },
{ id: '2', batchNumber: 'ZKFM', value: 'xxx' },
{ id: '1', batchNumber: 'ZKFM1', value: 'yyy' }
];
const xs = linq(yx).GroupBy(d => ({ id: d.id, b: d.batchNumber })).Select(d => {
const t = linq(d as { id: string, batchNumber: string, value: string }[]).Select(dx => dx);
return {
cnt: t.Count(),
array: t.ToArray(),
};
}).ToArray();
console.log(JSON.stringify(xs));
the answer as blow:
[
{
"cnt": 1,
"array": [
{
"id": "1",
"batchNumber": "ZKFM1",
"value": "zzz"
}
]
},
{
"cnt": 1,
"array": [
{
"id": "2",
"batchNumber": "ZKFM",
"value": "xxx"
}
]
},
{
"cnt": 1,
"array": [
{
"id": "1",
"batchNumber": "ZKFM1",
"value": "yyy"
}
]
}
]
i hope it will be:
[
{
"cnt": 2,
"array": [
{
"id": "1",
"batchNumber": "ZKFM1",
"value": "zzz"
},
{
"id": "1",
"batchNumber": "ZKFM1",
"value": "yyy"
}
]
},
{
"cnt": 1,
"array": [
{
"id": "2",
"batchNumber": "ZKFM",
"value": "xxx"
}
]
}
]
Hi !
I've this error:
linq.js:16 Uncaught Error: Cannot find module "."
Why ?
Right now SelectMany
is declared like this:
SelectMany<S, V>(selector?: (x: T, index: number) => Iterable<S>, result?: (x: T, s: S) => V): Enumerable<V>;
The second parameter, result
, is optional, but it's also the only one that would allow for inferring V
. As a result, when the second parameter is omitted, V
cannot be inferred and ends up as {}
. For example:
var xs = Enumerable.asEnumerable([1,2]).SelectMany( i => [i,i] );
The type of xs
will be inferred as Enumerable<{}>
.
To fix this, an overload of SelectMany
with one generic parameter needs to be explicitly present, and in order to make it distinct from the first overload, the result
parameter needs to be made not optional, which would also mean that selector
can't be optional either:
SelectMany<S, V>(selector: (x: T, index: number) => Iterable<S>, result: (x: T, s: S) => V): Enumerable<V>;
SelectMany<S>(selector?: (x: T, index: number) => Iterable<S>): Enumerable<S>;
P.S. I also don't quite get why selector
was declared optional in the first place.
import { asEnumerable as linq } from 'linq-es5';
const xs = [{ x: 'x', b: true }, { x: 'y', b: false }, { x: 'z', b: true }];
const lq = linq(xs).Where(f => f.b).Concat(linq(xs).Where(f => !f.b));
console.log(lq.ToArray());
/*
{x: "x", b: true}
{x: "y", b: false}
{x: "z", b: true}
{x: "y", b: false}
*/
The same code in C#
var xs = new[] { new { x = 'x', b = true }, new { x = 'y', b = false }, new { x = 'z', b = true } };
var lq = xs.Where(f => f.b).Concat(xs.Where(f => !f.b));
foreach (var item in lq)
{
Console.WriteLine($"{item.x} {item.b}");
}
/*
x True
z True
y False
*/
BUT below is ok in ts
const xs = [{ x: 'x', b: true }, { x: 'y', b: false }, { x: 'z', b: true }];
console.log(linq(xs).Where(f => f.b).ToArray());
/*
{x: "x", b: true}
{x: "z", b: true}
*/
console.log(linq(xs).Where(f => !f.b).ToArray())
/*
{x: "y", b: false}
*/
linq.js:4396 Uncaught TypeError: Cannot read property 'Symbol(Symbol.iterator)' of null
Hi, I'm using [email protected] and [email protected]
I want to use linq-es2015 with typescript, so I follow this tutorial
I copyed the app.ts to compile but failed.
qpan@qpan-vm:~/Desktop/$ tsc
node_modules/linq-es2015/lib/enumerable.d.ts(1,40): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(70,115): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(76,23): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(150,22): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(178,91): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(200,31): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(200,132): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(210,25): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(230,26): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(365,58): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(374,26): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(452,95): error TS2304: Cannot find name 'Map'.
node_modules/linq-es2015/lib/enumerable.d.ts(461,102): error TS2304: Cannot find name 'Map'.
node_modules/linq-es2015/lib/enumerable.d.ts(480,22): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/enumerable.d.ts(507,23): error TS2304: Cannot find name 'Iterable'.
node_modules/linq-es2015/lib/linq.d.ts(6,44): error TS2304: Cannot find name 'Iterable'.
routes/ts/attend/attend.ts(8,50): error TS2345: Argument of type '(longest: string, next: {}) => {}' is not assignable to parameter of type '(aggr: string, x: {}) => string'.
Type '{}' is not assignable to type 'string'.
routes/ts/attend/attend.ts(8,74): error TS2339: Property 'length' does not exist on type '{}'.
routes/ts/attend/attend.ts(10,39): error TS7006: Parameter 'fruit' implicitly has an 'any' type.
can you help me ?
Add running index to the method so position in the sequence could be used for grouping
import { asEnumerable as linq } from 'linq-es5';
const yx = [
{ id: '1', batchNumber: 'ZKFM1' },
{ id: '2', batchNumber: 'ZKFM' },
{ id: '3', batchNumber: 'ZKFM1' }
];
const zx = [
{ id: '1', value: 'zzz' },
{ id: '2', value: 'xxx' },
];
// LEFT JOIN
const yyy = linq(yx).GroupJoin(zx, a => a.id, b => b.id, (a, temp) => ({ a, temp }))
.SelectMany((t1: any) => linq(t1.temp).DefaultIfEmpty(), (t1: any, t: any) =>
({
id: t1.a.id,
batchNumber: t1.a.batchNumber,
value: t == null ? null : t.value,
}))
.ToArray();
console.log(JSON.stringify(yyy));
the answer as blow:
[
{
"id": "1",
"batchNumber": "ZKFM1",
"value": "zzz"
},
{
"id": "2",
"batchNumber": "ZKFM",
"value": "xxx"
}
]
i hope it will be:
[
{
"id": "1",
"batchNumber": "ZKFM1",
"value": "zzz"
},
{
"id": "2",
"batchNumber": "ZKFM",
"value": "xxx"
},
{
"id": "3",
"batchNumber": "ZKFM1",
"value": null
}
]
The Enumerable.Concat
method is defined like this:
Concat<V>(second: Iterable<T>): Enumerable<V>;
This makes it impossible or TS to infer the type V
, because it is not used in parameters, so type V
always ends up being {}
.
The correct definition should be this:
Concat<V>(second: Iterable<V>): Enumerable<V&T>;
Usage:
Three simple commands: Open your terminal, and
npm install -g greenkeeper
greenkeeper login
Your browser will open and ask you to log in to GitHub. Please do that.
Then, inside the repo you want to keep up to date, do
greenkeeper enable
and youβre done! Your first greenkeeper PR will arrive shortly!
Checkout my fork from this project to see how this works:
https://github.com/StefH/LINQ/pull/1
I have an easy christmas question for you :-)
In C# linq it seems easy but I don`t get it with TypeScript.
How can I group by 2 parameters like schoolclassNumber AND SubjectName?
Normally I would ask that on SO, but there is no linq-es2015 tag existing ;-)
UPDATE
I was confused and wanted actually something different. Of course I could just make a getter for the schoolclassNumber and subjectName together.
Thus my original question is how can I pass the TestType.id property as property of the returned item object?
export class TestType {
constructor(obj)
{
this.id = obj.id;
this.name = obj.name;
this.schoolclassNumber = obj.schoolclassNumber;
this.subjectName = obj.subjectName
}
id: number;
name: string;
schoolclassNumber: string;
subjectName: string;
}
let groupedTestTypes = Enumerable.from(testTypes)
.GroupBy(g => g.schoolclassNumber, g => g.name, (key, names) => { return { key: key, names: names } })
.Select((item, index) => new TestTypePair(item.key, Enumerable.from(item.names).ToArray()))
.ToArray();
for (var x of groupedTestTypes) {
console.log(x.key);
console.log("------------------");
for (var z of x.names) {
console.log(z);
}
}
```
in package.json
"dependencies": {
...
"linq-es2015": "2.4.4",
...
in systemjs.config.js
var map = {
...
'linq-es2015': 'node_modules/linq-es2015'
...
var packages = {
...
'linq-es2015': { main: 'index.js', defaultExtension: 'js' }
...
in gulpfile.js
gulp.src('./node_modules/linq-es2015/*/.js').pipe(gulp.dest('./wwwroot/node_modules/linq-es2015'));
then in xxx.ts file:
import * as Enumerable from "linq-es2015";
ngOnInit() {
var count = Enumerable.asEnumerable([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
.Where(a => a % 2 == 1)
.Count();
alert(count);
...
but when run in chrome ,
Error: XHR error (404 Not Found) ...
Error loading http://localhost:5000/node_modules/linq-es2015/index.js as "linq-es2015" from http://localhost:5000/app/xxxx.js
can anyone help me?
why error happied?
I have a lot of 'implicit any' errors in TS compilation. Looks like enumerable.ts
has invalid signatures for lambdas. For example,
Count(predicate?: (T) => Boolean): number;
Here T
is the name of a variable according to TypeScript syntax, but not its type. The type is any
. The correct syntax for lambdas should be
Count(predicate?: (x: T) => Boolean): number;
I'm using linq-es2-15 2.4.9 and Angular2 2.0.0. Linq works fine in Chrome and Firefox, but it won't even load in IE11. Any idea what the problem might be?
Thanks,
John
From the IE console:
Error: SyntaxError: Expected identifier
at ZoneDelegate.prototype.invoke (http://localhost:54040/node_modules/zone.js/dist/zone.js:190:13)
at Zone.prototype.run (http://localhost:54040/node_modules/zone.js/dist/zone.js:85:17)
at Anonymous function (http://localhost:54040/node_modules/zone.js/dist/zone.js:451:17)
at ZoneDelegate.prototype.invokeTask (http://localhost:54040/node_modules/zone.js/dist/zone.js:223:17)
at Zone.prototype.runTask (http://localhost:54040/node_modules/zone.js/dist/zone.js:125:21)
at drainMicroTaskQueue (http://localhost:54040/node_modules/zone.js/dist/zone.js:357:25)
Evaluating http://localhost:54040/node_modules/linq-es2015/dist/linq.js
Error loading http://localhost:54040/node_modules/linq-es2015/dist/linq.js as "linq-es2015" from http://localhost:54040/app/fc/fc.component.js
In both TheyBy<T>
and ThenByDescending<T>
upcast compliation fails
var superEqual = (<OrderedLinq<T>>this).equal;
Since this
is type of Linq<T>
typescript (1.7.5) complier throws
Type
Linq<T>
is not assignable to typeOrderedLinq<T>
. Property 'equal' is missing in typeLinq<T>
.
To do a work around assigning this
to var self:any = this
is sensible..?
I am consuming the package as follows:
import {asEnumerable, Range} from "linq-es2015";
it builds without errors and works on my desktop firefox. However it does not work on iOS or Mac Safari. Because my output js file contains functions like:
function* Forward(target) {
yield* target;
}
exports.Forward = Forward;
function* Reverse(target) {
for (let i = target.length - 1; i >= 0; i--) {
yield target[i];
}
}
exports.Reverse = Reverse;
function* Select(target, transform) {
let index = 0;
for (let value of target) {
yield transform(value, index++);
}
}
what does function* mean? And why does linq-es2015 produces it?
/**
Allows enumerating over a COM collection, which may not have indexed item access.
/
interface Enumerator {
/*
/**
/**
/**
Probably suggestion for typings in #4 not implemented yet (or at least npm repository is not up to date).
Does it support the possibility to work with providers? Or it only works on collections? For example, could it be possible to create an odata provider?
Can't understand how to work with it in Visual Studio without npm.
Get two types of errors during the compilation:
Iterable<I>
This was my first attempt to use EcmaScript6 (and shimps) so I'm definitely doing something wrong.
I have downloaded linq-ts.ts
, linq-ts.d.ts
from you; es6-shim.min.js
, es6-shim.map
from "es6-shim" and es6-shim.d.ts
from "definetely typed" projects.. But it seems the es6-shim.d.ts
doesn't contain Symbol
, when as I see es6-shim.js
contains it...
Where you get your es6-shim.d.ts
with Symbol defined? Or what else d.ts U
I also need generic advice: may be your project is not suitable to be compiled to EcmaScript5, and you strongly recommend to use only EcmaScript6?
const test = [
{ isControlled: true, no: 'C01' },
{ isControlled: true, no: 'C02' },
{ isControlled: false, no: 'C01' },
{ isControlled: false, no: 'C02' },
];
const after = linq(test).OrderByDescending(x => x.isControlled).ThenBy(x => x.no).ToArray();
console.log(JSON.stringify(after));
[{"isControlled":true,"no":"C02"},{"isControlled":true,"no":"C01"},{"isControlled":false,"no":"C02"},{"isControlled":false,"no":"C01"}]
the result is not as c# code, and the result of c# is what I want.
var test = new[]
{
new { isControlled = true, no = "C01" },
new { isControlled = true, no = "C02" },
new { isControlled = false, no = "C01" },
new { isControlled = false, no = "C02" },
};
var after = test.OrderByDescending(x => x.isControlled).ThenBy(x => x.no).ToArray();
Console.WriteLine(JsonConvert.SerializeObject(after));
[{"isControlled":true,"no":"C01"},{"isControlled":true,"no":"C02"},{"isControlled":false,"no":"C01"},{"isControlled":false,"no":"C02"}]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.