temp-helper-linqjs
v0.3.2
Published
Array 프로토타입 확장 - LINQ 스타일 메서드
Downloads
23
Readme
helper-linqjs
JavaScript Array에 C# LINQ 스타일의 쿼리 메서드를 추가하는 라이브러리입니다.
설치
npm install helper-linqjs사용
require('helper-linqjs');
[1, 2, 3, 4, 5]
.Where(x => x > 2)
.Select(x => x * 2)
// [6, 8, 10]지원되는 메서드
| 메서드 | 설명 | 반환값 |
|--------|------|--------|
| Where(predicate) | 조건을 만족하는 요소 필터링 | Array |
| Select(selector) | 배열 변환 | Array |
| Take(count) | 처음 N개 요소 추출 | Array |
| Skip(count) | 처음 N개 요소 제외 | Array |
| OrderBy(selector) | 오름차순 정렬 | Array |
| OrderByDescending(selector) | 내림차순 정렬 | Array |
| Distinct(selector) | 중복 제거 | Array |
| GroupBy(selector) | 키로 그룹화 | Array |
| Join(other, outerKey, innerKey, selector) | 배열 조인 | Array |
| First(predicate) | 첫 번째 요소 | Any |
| Last(predicate) | 마지막 요소 | Any |
| Count(predicate) | 개수 | Number |
| Any(predicate) | 조건을 만족하는 요소 있는지 확인 | Boolean |
| All(predicate) | 모든 요소가 조건을 만족하는지 확인 | Boolean |
| Sum(selector) | 합계 | Number |
| Average(selector) | 평균 | Number |
| Max(selector) | 최댓값 | Number |
| Min(selector) | 최솟값 | Number |
| Partition(predicate) | 조건에 따라 분할 | [Array, Array] |
메서드 상세 설명
Where
조건을 만족하는 요소 필터링
[1, 2, 3, 4].Where(x => x > 2) // [3, 4]Select
배열 변환
[1, 2, 3].Select(x => x * 2) // [2, 4, 6]Take
처음 N개 요소 추출
[1, 2, 3, 4, 5].Take(3) // [1, 2, 3]Skip
처음 N개 요소 제외
[1, 2, 3, 4, 5].Skip(2) // [3, 4, 5]OrderBy
오름차순 정렬
[3, 1, 2].OrderBy(x => x) // [1, 2, 3]OrderByDescending
내림차순 정렬
[1, 2, 3].OrderByDescending(x => x) // [3, 2, 1]Distinct
중복 제거
[1, 1, 2, 2, 3].Distinct() // [1, 2, 3]GroupBy
키로 그룹화
[1, 2, 3, 4].GroupBy(x => x % 2)
// [{ key: 1, values: [1, 3] }, { key: 0, values: [2, 4] }]Join
배열 조인
[1, 2].Join([2, 3], x => x, y => y, (x, y) => x + y)
// [4]First
첫 번째 요소
[1, 2, 3].First() // 1
[1, 2, 3].First(x => x > 2) // 3Last
마지막 요소
[1, 2, 3].Last() // 3
[1, 2, 3].Last(x => x < 3) // 2Count
개수
[1, 2, 3, 4].Count() // 4
[1, 2, 3, 4].Count(x => x > 2) // 2Any
조건을 만족하는 요소 있는지 확인
[1, 2, 3].Any(x => x > 2) // trueAll
모든 요소가 조건을 만족하는지 확인
[1, 2, 3].All(x => x > 0) // trueSum
합계
[1, 2, 3, 4].Sum() // 10Average
평균
[1, 2, 3, 4].Average() // 2.5Max
최댓값
[1, 2, 3, 4].Max() // 4Min
최솟값
[1, 2, 3, 4].Min() // 1Partition
조건에 따라 분할
[1, 2, 3, 4].Partition(x => x % 2 === 0)
// [[2, 4], [1, 3]]예제
require('helper-linqjs');
const products = [
{ name: 'Laptop', price: 1000 },
{ name: 'Mouse', price: 25 },
{ name: 'Desk', price: 300 }
];
// 가격 100 이상인 상품을 가격순으로 정렬
const result = products
.Where(p => p.price > 100)
.OrderBy(p => p.price)
.Select(p => p.name);
console.log(result); // ['Desk', 'Laptop']npm deprecate [email protected] "라이센스 변경으로 더이상 사용 안함"
