mihaifm / linq Goto Github PK
View Code? Open in Web Editor NEWlinq.js - LINQ for JavaScript
License: MIT License
linq.js - LINQ for JavaScript
License: MIT License
About the linq .js. Do you have some plans to develop linq to sql for javascript like C# linq to sql?
Hi,
First, thanks for this, it's a really cool lib :-)
While I was using it, I noticed that in the typings, the return type for groupBy
is odd:
Line 121 in 27a8af6
// truly, return type is IEnumerable<IGrouping<TKey, T>> but Visual Studio + TypeScript Compiler can't compile.
groupBy<TKey>(keySelector: (element: T) => TKey): IEnumerable<IGrouping<TKey, any>>;
Like the comment says, the true return type should be using T
instead of any
.
But I have the impression that the reason it invokes for not returning the right return type is moot.
I don't really know how this Visual Studio + TS compiler works but in the typescript project I'm working on, I just went and corrected it in my 'node_modules` folder directly and my editor started giving correct intellisense.
So I was wondering if you could just correct this.
If you want I can do a pull request.
Thanks!
It would be nice to have the typings (https://linqjs.codeplex.com/SourceControl/latest#typescript/) in the npm package.
Can we have mininifed js file?
Just wondering what the origin of this project is, to determine if I should replace the version in my app.
Thanks!
Hi, I`am migrating from bower to npm, I looking for the same repository in npm, but I found severals with other tag version.
Is this repo in npm?
if yes, Why is the url?
Thanks for your help.
Such as Enumerable besides Enumerable ?
💭 We should have the package version match the linq version.
Hello,
Just curious, this is an interesting JavaScript extension. But I wonder, why have anything to do with IEnumerable or Enumerator? Why not embrace the goodness of the JavaScript Array
, but with LINQ feel to it?
For instance,
if (Array.prototype.any === undefined) {
Array.prototype.any = function (match) {
match = match || function (x) { return true; }
for (let x in this) {
if (match(x)) { return true; }
}
return false;
}
}
Or, similarly for first
:
if (Array.prototype.first === undefined) {
Array.prototype.first = function (match) {
match = match || function (x) { return true; }
for (let x in this) {
if (match(x)) { return x; }
}
return null;
}
}
Of course, being careful to mesh nicely with the JavaScript API.
How can I select a group of specific values with their keys?
I want to able to perform a query similar to "select id, name, ... from table"
meaning I want the result to be an array only containing the fields I chose.
Thanks
IEnumerable<T>.zip() accepts U[] (or IEnumerable<U>) as the first argument.
Enumerable.from([1, 2, 3])
.zip(["a", "b", "c"], (first, second) => `${first}${second}`)
.toArray();
// => ["1a", "2b", "3c"]
So, definitions should be like this:
zip<U, TResult>(second: IEnumerable<U>, resultSelector: (first: T, second: U, index: number) => TResult): IEnumerable<TResult>;
zip<U, TResult>(second: { length: number;[x: number]: U; }, resultSelector: (first: T, second: U, index: number) => TResult): IEnumerable<TResult>;
zip<U, TResult>(second: U[], resultSelector: (first: T, second: U, index: number) => TResult): IEnumerable<TResult>;
I'm trying to find minified and not minified versions of linq that I can use in a browser. Ideally I could just grab this off of a cdn, but it seems like the latest (https://cdnjs.com/libraries/linq.js
) is 2.2.0.2 and you're on version 3 now.
I couldn't find anything in the readme about using this in the browser, but I saw in another issue that "bower should work". I've never heard of bower, but I gave it a shot. When I run
$ bower install linq
I get
fatal: repository 'https://github.com/mihaifm/linq/blob/master/linq.js.git/' not found
Is there any way to easily use the latest version in my browser?
Version in bower.json needs to be bumped to 3.0.5
{
"name": "linqjs",
"version": "1.0.0", //Change to 3.0.5
"main": "linq.js",
"ignore": [
"*/.",
"extensions",
"test",
"_.md",
"CHANGELOG",
"package.json"
],
"homepage": "https://github.com/mihaifm/linq",
"_release": "6b76ff6f5f",
"_resolution": {
"type": "branch",
"branch": "master",
"commit": "6b76ff6f5f77d3a4f7a7812bb9a979536f7e5e78"
},
"_source": "git://github.com/mihaifm/linq.git",
"target": "",
"_originalSource": "linqjs",
"_direct": true
}
Like doing this
Enumerable.from(dataVals).where("$.indexOf("" + parameterName + "") !== -1").singleOrDefault("", "$").replace(/+/g, " ").split("=")[1];
instead of doing
return Enumerable.from(dataVals).where(p=> p.indexOf(parameterName) !== -1).singleOrDefault().replace(/+/g, " ").split("=")[1];
for most cases I can rewrite but I have a few specific that it is easier to use the string selector
Thanks
Hello
I tried to use your library with objects with this structure:
const vehicle = {
weight: 1000,
length: 5,
color: "Blue",
...
}
and wanted to filter some values using .where
method
Enumerable.from(vehicle)
.where(i => excludedFields.indexOf(i.Key) == -1)
.toObject(i => i.Key, i => i.Value);
I expected that .from
function would convert my object to KeyValuePair. Unfortunately this method considered that input is array and didn't convert object to KeyValue
I checked the source code and found this statement on the row 300
// array or array like object
if (typeof obj.length == Types.Number) {
return new ArrayEnumerable(obj);
}
It looks like any object with length property will be converted to ArrayEnumerable independent of another properties.
Is it possible to check for Arrays by another way (something like instanceof call)?
Current typing is
groupBy<TKey, TElement, TResult, TCompare>(
keySelector: (element: T) => TKey,
elementSelector: (element: T) => TElement,
resultSelector: (key: TKey, element: IEnumerable<TElement>) => TResult,
compareSelector: (element: T) => TCompare
): IEnumerable<TResult>;
But the compareSelector
actually accepts parameter of type TKey
, not T
(which is more intuitive than T
, BTW).
This probably happens because it is passed as a dictionary comparer inside of toLookup
method, but I haven't investigated this further.
e.g. #20 for a bug/fix
I also wanted to add iterator support so I could use for..of directly instead of using toArray first.
I only ask as this is a copy of codeplex's version but with a few npm fixes, right?
This repo seems to be the most used with 5000 monthly installs, and I personally use it a lot (well, I use a 'fork' (not actually a fork in the git sense) with iterator support), so I'd like to pull some changes in
Your example is in uppercase, but it is in lowercase when used. I feel that your example should be adjusted to lowercase.
For example, your example is (Enumerable.Range(1, 10).Where(delegate(int i) { return i % 3 == 0; }))
But I actually use (Enumerable.from(array).where("x=>x.name=='删除'").toArray();)
Otherwise, report wrong.
I am not entirely sure where this should go, as really this is just a republish of the codeplex one, but given codeplex is pretty much dead I am not sure where to put it.
Anyway the docs on the side show SingleOrDefault(default, predicate)
however the code in here is singleOrDefault(predicate, default)
.
So is there any sort of idea if this is now going to become the maintained build or should this be raised on codeplex?
We're getting some really weird behavior when using orderBy
on arrays with null
in them.
Below is a test case running linq 3.2.1 on node 10.15.3.
const Enumerable = require('linq');
const data = ['2019-10-01', null, '2019-09-12', '2019-09-15', null];
const result = Enumerable.from(data).orderBy(x => x).toArray();
result.forEach(x => console.log(x));
Expected output:
null
null
2019-09-12
2019-09-15
2019-10-01
Actual output:
2019-10-01
null
2019-09-12
2019-09-15
null
Thanks in advance for any help you can provide.
I am trying to use this library within a web application, and am having a hard time sorting out how to get Enumerable
to be defined globally as the require()
bit is a Node thing.
How can I go about doing this?
i can't understan how connect your linq to vue project.
RT,@3.0.4,FirstOrDefault Method Always Return First Data,ignore Where condition。
FirstOrDefault doesn't appear to work.
Enumerable.from([]).firstOrDefault(15) // null
Enumerable.prototype.firstOrDefault = function (predicate, defaultValue) {
if (predicate) {
if (typeof predicate === Types.Function || typeof Utils.createLambda(predicate) === Types.Function)
return this.where(predicate).firstOrDefault(null, defaultValue);
defaultValue = predicate;
}
defaultValue = defaultValue || null; // THIS LINE
...
Maybe we should remove this line from firstOrDefault
to allow undefined as default value?
It is specially important because the user might pass in undefined
explicitly for the parameter defaultValue
(which is the conventional missing value in JS), and this undefined value will get overloaded with null.
Even the type definition doesnt indicate that the return type might be T or null. In fact, type definition doesn't allow passing in null
explicitly as defaultValue (which would make things more explicit and understandable for users).
The user has no way of knowing this unless they actually look into the code.
How do I get a javascript Iterable into your library? Should be simple.
function* words() : Iterable<string> {
yield "abc";
yield "def";
}
let x = Linq.from(words()); // does not work
I cannot manage to convert an array to Enumerable.
Enumerable.from() keeps returning an empty object {}
This is the output I get from sample.js:
first step of Lambda Expression
Scope of lambda expression
from(Object) -> convert to keyvaluePair
foo:a
bar:100
foobar:true
forEach (continue and break)
1:foo
3:foo
5:foo
Grouping and ref/value comparen
false
false
------
------
Regular Expression matches
0 : abcd
1 : c
index : 0
input : abcdefgABzDefabgdg
toString() : abcd,c
---
0 : ABzD
1 : z
index : 7
input : abcdefgABzDefabgdg
toString() : ABzD,z
---
0 : abgd
1 : g
index : 13
input : abcdefgABzDefabgdg
toString() : abgd,g
---
LazyEvaluation and InfinityList
57
Dictionary
zzz
huga
a:zzz
b:huga
Nondeterministic Programs
Please also add a reference output and/or a unit test.
As you can see from this part:
Grouping and ref/value comparen
false
false
------
------
The Enumerable.from() is not working
As a former C# developer I like your library. Just want to know, any plans to use ES6 Iterable instead of custom Enumerable? If you want to support old browsers, would it be possible to have Enumerables to use Iterables under the hood for modern browsers? Also relates to #51
Thanks
orderBy doesn't take a compare function as parameter, similar to C# LINQ orderBy
If you call firstOrDefault with a default value that equates to false then the return value will be null if the enumerable is empty.
testing.firstOrDefault(null, 0)
yields null
testing.firstOrDefault(null, false)
yiels null
This is because line at 1869 defaultValue = defaultValue || null
shouldn't it be defaultValue = (typeof(defaultValue) === 'undefined') ? null : defaultValue
That way passing in null
yields null
, passing in an undefined value yields null
everything else yields the supplied defaultValue
when the enumerable is empty
Maybe I'm missing something here, but when you have an enumerable you can to toDictionary
but you can't do toMap
to create a regular javascript map. Very confused. I think ENikS/LINQ allows you to do that quite simply.
i use typing and linq js ver 3.0.4-beta5
On TS, when i write codes, i have no issue, on client, when i launch on browser, i get this error:
angular.min.js:118 ReferenceError: Enumerable is not defined
and when i traced the linq.js file: it goes to the first if:
this one:
// module export
if (typeof define === Types.Function && define.amd) { // AMD
define("linqjs", [], function () { return Enumerable; });
}
Would you fix it?
has someone else seen this. i have large amount of data on which i am doing group by operations..like (1500 rows of data) and when i do group by it does not produce the correct result. it adds the numeric column correctly but puts the amount in wrong key. i tried doing it and matching wit excel pivot table and my results are not tying. has anyone else seen it?
The package on the NPM have some issues so please tell me that when will the latest package be released on the NPM?
If I import it,
import 'linq';
I get intellisense, but it doesn't compile:
'Enumerable' refers to a UMD global, but the current file is a module. Consider adding an import instead.
Used with: Webpack, Angular 4, Typescript
when using yeoman, it doesn't install linq.js as a vendor. This is only because the 'bower.json' file is missing 'main'... I solved it by changing it to this...
{
"name": "linqjs",
"homepage": "https://github.com/mihaifm/linq",
"_release": "6b76ff6f5f",
"_resolution": {
"type": "branch",
"branch": "master",
"commit": "6b76ff6f5f77d3a4f7a7812bb9a979536f7e5e78"
},
"_source": "git://github.com/mihaifm/linq.git",
"_target": "*",
"_originalSource": "linqjs",
"_direct": true,
"main": "./linq.js"
}
Hello, let's say I have the casual array:
var values = [1, 2, 3];
I want to select from that array and work with the results, right?
var selected = Enumerable.from(values).where(x => x%2 !== 0);
console.log("number of odd values: " + selected.length); // Expecting 2
However, what is returned from Where appears to be an Linq object wrapper of some sort.
How do I extract the actual values from the expression?
Otherwise, cannot give enough 👍 for this type of library package.
Thanks!
Thanks for building this repository.
I want to use linq.js via https://rails-assets.org/
It requires released version of the package.
can i add a new comparator in the query ?
I see that your package is in npm , but there are no links to the repo, can you please share it?
I found that doing OrderBy() then ThenBy() on IE 11 returns a different result set than on Chrome or Firefox.
Created jsfiddle to reproduce:
https://jsfiddle.net/screaney/gj0gwxfo/4/
It appears on IE the ThenBy places them in reverse instead of asc/asc.
In all join
and groupJoin
definitions the resultSelector
is defined as resultSelector: (outer: T, inner: TKey) => TResult
.
Whereas the correct generic argument of inner
is TInner
.
The console returns unexpected "." on line 68 in linq.js if I use linq like this:
Enumerable.from(a).join(b, "l1 => l1.ArticleId", "l2 => l2.Id", "(l1, l2) => { l1.Article = l2; return l1; }").toArray();
To allow assigning variables during the join the line 68 in linq.js:
f = new Function(expr[1], "return " + expr[2]);
must be changed to this:
f = new Function(expr[1], (expr[2].includes("return") ? expr[2] : "return " + expr[2]));
The C# equivalent as follows:
a.Join(b,
l1 => l1.ArticleId, l2 => l2.Id, (l1, l2) =>
{
l1.Article = l2;
return l1;
}).ToArray();
Currently LICENSE file states:
Copyright (c) < year > < copyright holders >
You should provide neuecc and yourself as copyright holders, otherwise I have a problem considering it a valid license. Also, in README please provide a link to this file, not opensource.org
Hi @mihaifm,
Can you please publish master to npm?
I'm using linq in my TypeScript project with noImplicitAny
enabled and currently the compiler errors on the typings for linq 3.0.7.
node_modules/linq/linq.d.ts(29,75): error TS7006: Parameter 'element' implicitly has an 'any' type.
node_modules/linq/linq.d.ts(37,5): error TS7010: 'constructor', which lacks return-type annotation, implicitly has an 'any' return type.
This is actually fixed in master but it hasn't been published to npm yet. Could you please publish to npm?
Thanks
Hi everyone
I contacted the original author of this library and he mentioned that he is no longer maintaining it.
I initially created this repository to add his library to npm, but since then a small number of improvements have been added.
The code is stable, but it could use some enhancements like using the new ecmascript lambdas and all that good stuff.
Unfortunatelly I don't have the time to further maintain this as well, so if anyone wants to contribute let me know here.
thanks
Enumerable.prototype.find = function (compareSelector) {
compareSelector = Utils.createLambda(compareSelector);
var result = null;
this.forEach(function(item) {
if (compareSelector(item)) {
result = item;
return;
}
});
return result;
};
Usage:
var result = Enumerable.from(list).find("x => x.Id == " + somevariable.Id);
It works fine and I hope you like it. It could use some optimization of code quality though.
I don't like the "x => x.Id == " + somevariable.Id part, because the somevariable.Id is outside of the quotation marks. Maybe you got a better solution for this problem.
C# Equivalent would be:
public static T Find<T>(this IList<T> source, Func<T, bool> condition)
{
foreach (var item in source)
{
if (condition(item))
{
return item;
}
}
return default(T);
}
Usage:
var result = list.Find(x => x.Id == somevariable.Id);
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.