3 Star 22 Fork 7

散漫的水元素 / linqjs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

LinqJs

GitHub release npm npm Travis (.com) Codecov npm bundle size NPM GitHub search hit counter GitHub search hit counter GitHub search hit counter

Since 2.1.0, I rewrite all to use new features of ES6. The performance be better, memory used less and using deferred execution.

对 JavaScript 中原生数组、对象进行扩展, 提供了一些对数据的操作方法. 包括对数组结构、树形结构、对象结构等数据进行 查询,排序,连接,合并,分组,分段,转换,遍历 等一系列功能. 可以用精炼和易懂的代码实现比较复杂的操作.

一开始,在 ES5 的时代,我觉得 js 对数组的操作实在太繁琐了. 可能正是这种繁琐才有了像 underscore 这样优秀的 js 库的出现. 我本不是做前端出身的,我觉得其他语言对于数组或列表的操作要比 js 好用很多,提供了大量的方法和功能. 于是最早参考的 C#中的功能实现了一些方法, 就有了1.0版本. 后来,ES6 发布并普及开来,js 原生数组也增加了很多好用的功能,但是这还远远不够. 于是我又翻出了我的代码用 ES6 重写, 有了2.1版本, 并发布至npm上. 这一次我又借鉴了另外一些语言中的部分特性, 比如 php.我想我后面可能再会添加一些其他语言或框架类似的功能,比如 Java,Python 等. 后来我在 stackoverflow 中看到很多人对于树形结构的操作提了很多的问题,我想我的工具包应该要可以为他们提供帮助.于是在最新的几个版本中我加入了对树形结构的一些操作.

帮助文档(完善中) :+1:

Documentation(not completed) :+1:

Usage 用法

1. Import 引入

Use NodeJs 使用NodeJs

$ npm install --save linq-js
const Enumerable = require('linq-js');
  • description:This module require ES6. I suggest you to use this with ES6. The following examples is already use ES6.
  • 说明:本module依赖于ES6。建议项目在中使用ES6。以下案例中将均使用ES6写法。

2. Get IEnumerable instance 获取IEnumerable对象

interface IEnumerable { };

function asEnumerable():IEnumerable;
//You can use the asEnumerable methods of every object's to get an IEnumerable object;
//任何对象都有asEnumerable方法用来获取IEnumerable对象

e.g. 案例

'abc'.asEnumerable();
[1,2,3].asEnumerable();
({a:1,b:2}).asEnumerable();

3. Use IEnumerable instance 使用IEnumerable对象

e.g. 案例 简单的判断数组中的数据

let pets = [ { name: "Barley", age: 8, vaccinated: true }, { name: "Boots", age: 4, vaccinated: false }, { name: "Whiskers", age: 1, vaccinated: false } ];
let unvaccinated = pets.asEnumerable().any(p => p.age > 1 && p.vaccinated === false);
console.log(`There ${ unvaccinated ? "are" : "are not any" } unvaccinated animals over age one.`);
// This code produces the following output: 这段代码输出以下内容:
//  There are unvaccinated animals over age one.

e.g. 案例 两个数组进行内连接查询

let magnus = { id: 1, name: "Hedlund, Magnus" }, terry = { id: 2, name: "Adams, Terry" }, charlotte = { id: 3, name: "Weiss, Charlotte" };
let barley = { name: "Barley", owner: 2 }, boots = { name: "Boots", owner: 2 }, whiskers = { name: "Whiskers", owner: 3 }, daisy = { name: "Daisy", owner: 1 };
let people = [ magnus, terry, charlotte ];
let pets = [ barley, boots, whiskers, daisy ];
let query = people.asEnumerable().join(pets,
    (person, pet) => ({ ownerName: person.name, pet: pet.name }),
    person => person.id,
    pet => pet.owner);
for (let obj of query) {
    console.log(`${ obj.ownerName } - ${ obj.pet }`);
}
/*
 This code produces the following output: 这段代码输出以下内容:
 Hedlund, Magnus - Daisy
 Adams, Terry - Barley
 Adams, Terry - Boots
 Weiss, Charlotte - Whiskers
 */

:see mdn(Array) :参考 MDN(Array)

:see msdn(IEnumerable) :参考 MSDN(IEnumerable)

:see msdn(Enumerable) :参考 MSDN(Enumerable)

MIT License Copyright (c) 2018 Miao Wang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个方便对数组、字典、树形数据进行操作、筛选等操作的工具库 展开 收起
取消

发行版 (9)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/wm123450405/linqjs.git
git@gitee.com:wm123450405/linqjs.git
wm123450405
linqjs
linqjs
master

搜索帮助