temp-helper-linqjs 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/linq-module.js +194 -19
- package/package.json +1 -1
package/linq-module.js
CHANGED
|
@@ -1,32 +1,77 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Array 프로토타입 확장 - LINQ 스타일 메서드
|
|
3
|
+
* C# LINQ와 유사한 쿼리 메서드를 JavaScript Array에 추가합니다.
|
|
3
4
|
* @module helper-linqjs
|
|
5
|
+
* @version 0.2.1
|
|
6
|
+
* @author WooYoung
|
|
7
|
+
* @license MIT
|
|
4
8
|
*/
|
|
5
9
|
|
|
6
10
|
(function() {
|
|
7
11
|
'use strict';
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
/**
|
|
14
|
+
* 조건을 만족하는 요소를 필터링합니다.
|
|
15
|
+
* @function Where
|
|
16
|
+
* @memberof Array.prototype
|
|
17
|
+
* @param {Function} predicate - 필터링 조건 함수 (true를 반환하는 요소만 포함)
|
|
18
|
+
* @returns {Array} 필터링된 새로운 배열
|
|
19
|
+
* @example
|
|
20
|
+
* [1, 2, 3, 4].Where(x => x > 2) // [3, 4]
|
|
21
|
+
*/
|
|
10
22
|
Array.prototype.Where = function(predicate) {
|
|
11
23
|
return this.filter(predicate);
|
|
12
24
|
};
|
|
13
25
|
|
|
14
|
-
|
|
26
|
+
/**
|
|
27
|
+
* 배열의 각 요소를 변환합니다.
|
|
28
|
+
* @function Select
|
|
29
|
+
* @memberof Array.prototype
|
|
30
|
+
* @param {Function} selector - 변환 함수
|
|
31
|
+
* @returns {Array} 변환된 새로운 배열
|
|
32
|
+
* @example
|
|
33
|
+
* [1, 2, 3].Select(x => x * 2) // [2, 4, 6]
|
|
34
|
+
*/
|
|
15
35
|
Array.prototype.Select = function(selector) {
|
|
16
36
|
return this.map(selector);
|
|
17
37
|
};
|
|
18
38
|
|
|
19
|
-
|
|
39
|
+
/**
|
|
40
|
+
* 배열의 처음 N개 요소를 추출합니다.
|
|
41
|
+
* @function Take
|
|
42
|
+
* @memberof Array.prototype
|
|
43
|
+
* @param {number} count - 추출할 요소의 개수
|
|
44
|
+
* @returns {Array} 처음 count개의 요소로 구성된 배열
|
|
45
|
+
* @example
|
|
46
|
+
* [1, 2, 3, 4, 5].Take(3) // [1, 2, 3]
|
|
47
|
+
*/
|
|
20
48
|
Array.prototype.Take = function(count) {
|
|
21
49
|
return this.slice(0, count);
|
|
22
50
|
};
|
|
23
51
|
|
|
24
|
-
|
|
52
|
+
/**
|
|
53
|
+
* 배열의 처음 N개 요소를 제외합니다.
|
|
54
|
+
* @function Skip
|
|
55
|
+
* @memberof Array.prototype
|
|
56
|
+
* @param {number} count - 제외할 요소의 개수
|
|
57
|
+
* @returns {Array} count개 이후의 요소로 구성된 배열
|
|
58
|
+
* @example
|
|
59
|
+
* [1, 2, 3, 4, 5].Skip(2) // [3, 4, 5]
|
|
60
|
+
*/
|
|
25
61
|
Array.prototype.Skip = function(count) {
|
|
26
62
|
return this.slice(count);
|
|
27
63
|
};
|
|
28
64
|
|
|
29
|
-
|
|
65
|
+
/**
|
|
66
|
+
* 배열을 오름차순으로 정렬합니다.
|
|
67
|
+
* @function OrderBy
|
|
68
|
+
* @memberof Array.prototype
|
|
69
|
+
* @param {Function} selector - 정렬 기준을 결정하는 함수
|
|
70
|
+
* @returns {Array} 정렬된 배열
|
|
71
|
+
* @example
|
|
72
|
+
* [3, 1, 2].OrderBy(x => x) // [1, 2, 3]
|
|
73
|
+
* [{val: 3}, {val: 1}].OrderBy(x => x.val) // [{val: 1}, {val: 3}]
|
|
74
|
+
*/
|
|
30
75
|
Array.prototype.OrderBy = function(selector) {
|
|
31
76
|
return this.sort((a, b) => {
|
|
32
77
|
const valA = selector(a);
|
|
@@ -37,7 +82,15 @@
|
|
|
37
82
|
});
|
|
38
83
|
};
|
|
39
84
|
|
|
40
|
-
|
|
85
|
+
/**
|
|
86
|
+
* 배열을 내림차순으로 정렬합니다.
|
|
87
|
+
* @function OrderByDescending
|
|
88
|
+
* @memberof Array.prototype
|
|
89
|
+
* @param {Function} selector - 정렬 기준을 결정하는 함수
|
|
90
|
+
* @returns {Array} 내림차순으로 정렬된 배열
|
|
91
|
+
* @example
|
|
92
|
+
* [1, 2, 3].OrderByDescending(x => x) // [3, 2, 1]
|
|
93
|
+
*/
|
|
41
94
|
Array.prototype.OrderByDescending = function(selector) {
|
|
42
95
|
return this.sort((a, b) => {
|
|
43
96
|
const valA = selector(a);
|
|
@@ -48,7 +101,16 @@
|
|
|
48
101
|
});
|
|
49
102
|
};
|
|
50
103
|
|
|
51
|
-
|
|
104
|
+
/**
|
|
105
|
+
* 중복된 요소를 제거합니다.
|
|
106
|
+
* @function Distinct
|
|
107
|
+
* @memberof Array.prototype
|
|
108
|
+
* @param {Function} [selector=x => x] - 중복 판별 기준 함수 (기본값: 요소 자체)
|
|
109
|
+
* @returns {Array} 중복이 제거된 배열
|
|
110
|
+
* @example
|
|
111
|
+
* [1, 1, 2, 2, 3].Distinct() // [1, 2, 3]
|
|
112
|
+
* [{id: 1}, {id: 1}, {id: 2}].Distinct(x => x.id) // [{id: 1}, {id: 2}]
|
|
113
|
+
*/
|
|
52
114
|
Array.prototype.Distinct = function(selector = x => x) {
|
|
53
115
|
const seen = new Set();
|
|
54
116
|
return this.filter(item => {
|
|
@@ -59,7 +121,16 @@
|
|
|
59
121
|
});
|
|
60
122
|
};
|
|
61
123
|
|
|
62
|
-
|
|
124
|
+
/**
|
|
125
|
+
* 배열을 지정된 키로 그룹화합니다.
|
|
126
|
+
* @function GroupBy
|
|
127
|
+
* @memberof Array.prototype
|
|
128
|
+
* @param {Function} selector - 그룹화 키를 결정하는 함수
|
|
129
|
+
* @returns {Array<Object>} { key, values } 객체 배열
|
|
130
|
+
* @example
|
|
131
|
+
* [1, 2, 3, 4].GroupBy(x => x % 2)
|
|
132
|
+
* // [{ key: 1, values: [1, 3] }, { key: 0, values: [2, 4] }]
|
|
133
|
+
*/
|
|
63
134
|
Array.prototype.GroupBy = function(selector) {
|
|
64
135
|
const groups = new Map();
|
|
65
136
|
this.forEach(item => {
|
|
@@ -72,7 +143,18 @@
|
|
|
72
143
|
return Array.from(groups, ([key, values]) => ({ key, values }));
|
|
73
144
|
};
|
|
74
145
|
|
|
75
|
-
|
|
146
|
+
/**
|
|
147
|
+
* 두 배열을 조인합니다. (SQL INNER JOIN과 유사)
|
|
148
|
+
* @function Join
|
|
149
|
+
* @memberof Array.prototype
|
|
150
|
+
* @param {Array} other - 조인할 배열
|
|
151
|
+
* @param {Function} outerKey - 현재 배열의 키 선택 함수
|
|
152
|
+
* @param {Function} innerKey - 다른 배열의 키 선택 함수
|
|
153
|
+
* @param {Function} selector - 조인 결과를 구성하는 함수
|
|
154
|
+
* @returns {Array} 조인된 결과 배열
|
|
155
|
+
* @example
|
|
156
|
+
* [1, 2].Join([2, 3], x => x, y => y, (x, y) => x + y) // [4]
|
|
157
|
+
*/
|
|
76
158
|
Array.prototype.Join = function(other, outerKey, innerKey, selector) {
|
|
77
159
|
const innerMap = new Map();
|
|
78
160
|
other.forEach(item => {
|
|
@@ -90,7 +172,17 @@
|
|
|
90
172
|
});
|
|
91
173
|
};
|
|
92
174
|
|
|
93
|
-
|
|
175
|
+
/**
|
|
176
|
+
* 첫 번째 요소를 반환합니다.
|
|
177
|
+
* @function First
|
|
178
|
+
* @memberof Array.prototype
|
|
179
|
+
* @param {Function} [predicate=null] - 조건 함수 (선택사항)
|
|
180
|
+
* @returns {*} 첫 번째 요소 (또는 조건을 만족하는 첫 요소)
|
|
181
|
+
* @throws {Error} 배열이 비어있거나 조건을 만족하는 요소가 없을 경우
|
|
182
|
+
* @example
|
|
183
|
+
* [1, 2, 3].First() // 1
|
|
184
|
+
* [1, 2, 3].First(x => x > 2) // 3
|
|
185
|
+
*/
|
|
94
186
|
Array.prototype.First = function(predicate = null) {
|
|
95
187
|
if (predicate) {
|
|
96
188
|
return this.find(predicate);
|
|
@@ -98,7 +190,17 @@
|
|
|
98
190
|
return this[0];
|
|
99
191
|
};
|
|
100
192
|
|
|
101
|
-
|
|
193
|
+
/**
|
|
194
|
+
* 마지막 요소를 반환합니다.
|
|
195
|
+
* @function Last
|
|
196
|
+
* @memberof Array.prototype
|
|
197
|
+
* @param {Function} [predicate=null] - 조건 함수 (선택사항)
|
|
198
|
+
* @returns {*} 마지막 요소 (또는 조건을 만족하는 마지막 요소)
|
|
199
|
+
* @throws {Error} 배열이 비어있거나 조건을 만족하는 요소가 없을 경우
|
|
200
|
+
* @example
|
|
201
|
+
* [1, 2, 3].Last() // 3
|
|
202
|
+
* [1, 2, 3].Last(x => x < 3) // 2
|
|
203
|
+
*/
|
|
102
204
|
Array.prototype.Last = function(predicate = null) {
|
|
103
205
|
if (predicate) {
|
|
104
206
|
const arr = this.filter(predicate);
|
|
@@ -107,7 +209,16 @@
|
|
|
107
209
|
return this[this.length - 1];
|
|
108
210
|
};
|
|
109
211
|
|
|
110
|
-
|
|
212
|
+
/**
|
|
213
|
+
* 배열의 요소 개수를 반환합니다.
|
|
214
|
+
* @function Count
|
|
215
|
+
* @memberof Array.prototype
|
|
216
|
+
* @param {Function} [predicate=null] - 조건 함수 (선택사항)
|
|
217
|
+
* @returns {number} 요소 개수 (또는 조건을 만족하는 요소 개수)
|
|
218
|
+
* @example
|
|
219
|
+
* [1, 2, 3, 4].Count() // 4
|
|
220
|
+
* [1, 2, 3, 4].Count(x => x > 2) // 2
|
|
221
|
+
*/
|
|
111
222
|
Array.prototype.Count = function(predicate = null) {
|
|
112
223
|
if (predicate) {
|
|
113
224
|
return this.filter(predicate).length;
|
|
@@ -115,7 +226,17 @@
|
|
|
115
226
|
return this.length;
|
|
116
227
|
};
|
|
117
228
|
|
|
118
|
-
|
|
229
|
+
/**
|
|
230
|
+
* 조건을 만족하는 요소가 있는지 확인합니다.
|
|
231
|
+
* @function Any
|
|
232
|
+
* @memberof Array.prototype
|
|
233
|
+
* @param {Function} [predicate=null] - 조건 함수 (선택사항)
|
|
234
|
+
* @returns {boolean} 조건을 만족하는 요소가 있으면 true
|
|
235
|
+
* @example
|
|
236
|
+
* [1, 2, 3].Any() // true
|
|
237
|
+
* [1, 2, 3].Any(x => x > 2) // true
|
|
238
|
+
* [1, 2, 3].Any(x => x > 10) // false
|
|
239
|
+
*/
|
|
119
240
|
Array.prototype.Any = function(predicate = null) {
|
|
120
241
|
if (predicate) {
|
|
121
242
|
return this.some(predicate);
|
|
@@ -123,33 +244,87 @@
|
|
|
123
244
|
return this.length > 0;
|
|
124
245
|
};
|
|
125
246
|
|
|
126
|
-
|
|
247
|
+
/**
|
|
248
|
+
* 모든 요소가 조건을 만족하는지 확인합니다.
|
|
249
|
+
* @function All
|
|
250
|
+
* @memberof Array.prototype
|
|
251
|
+
* @param {Function} predicate - 조건 함수
|
|
252
|
+
* @returns {boolean} 모든 요소가 조건을 만족하면 true
|
|
253
|
+
* @example
|
|
254
|
+
* [1, 2, 3].All(x => x > 0) // true
|
|
255
|
+
* [1, 2, 3].All(x => x > 2) // false
|
|
256
|
+
*/
|
|
127
257
|
Array.prototype.All = function(predicate) {
|
|
128
258
|
return this.every(predicate);
|
|
129
259
|
};
|
|
130
260
|
|
|
131
|
-
|
|
261
|
+
/**
|
|
262
|
+
* 배열 요소의 합계를 계산합니다.
|
|
263
|
+
* @function Sum
|
|
264
|
+
* @memberof Array.prototype
|
|
265
|
+
* @param {Function} [selector=x => x] - 요소 선택 함수 (기본값: 요소 자체)
|
|
266
|
+
* @returns {number} 합계
|
|
267
|
+
* @example
|
|
268
|
+
* [1, 2, 3, 4].Sum() // 10
|
|
269
|
+
* [{val: 1}, {val: 2}].Sum(x => x.val) // 3
|
|
270
|
+
*/
|
|
132
271
|
Array.prototype.Sum = function(selector = x => x) {
|
|
133
272
|
return this.reduce((acc, item) => acc + selector(item), 0);
|
|
134
273
|
};
|
|
135
274
|
|
|
136
|
-
|
|
275
|
+
/**
|
|
276
|
+
* 배열 요소의 평균을 계산합니다.
|
|
277
|
+
* @function Average
|
|
278
|
+
* @memberof Array.prototype
|
|
279
|
+
* @param {Function} [selector=x => x] - 요소 선택 함수 (기본값: 요소 자체)
|
|
280
|
+
* @returns {number} 평균 (빈 배열은 0 반환)
|
|
281
|
+
* @example
|
|
282
|
+
* [1, 2, 3, 4].Average() // 2.5
|
|
283
|
+
* [{val: 1}, {val: 3}].Average(x => x.val) // 2
|
|
284
|
+
*/
|
|
137
285
|
Array.prototype.Average = function(selector = x => x) {
|
|
138
286
|
if (this.length === 0) return 0;
|
|
139
287
|
return this.Sum(selector) / this.length;
|
|
140
288
|
};
|
|
141
289
|
|
|
142
|
-
|
|
290
|
+
/**
|
|
291
|
+
* 배열에서 최댓값을 반환합니다.
|
|
292
|
+
* @function Max
|
|
293
|
+
* @memberof Array.prototype
|
|
294
|
+
* @param {Function} [selector=x => x] - 요소 선택 함수 (기본값: 요소 자체)
|
|
295
|
+
* @returns {number} 최댓값
|
|
296
|
+
* @example
|
|
297
|
+
* [1, 2, 3, 4].Max() // 4
|
|
298
|
+
* [{val: 1}, {val: 5}].Max(x => x.val) // 5
|
|
299
|
+
*/
|
|
143
300
|
Array.prototype.Max = function(selector = x => x) {
|
|
144
301
|
return Math.max(...this.map(selector));
|
|
145
302
|
};
|
|
146
303
|
|
|
147
|
-
|
|
304
|
+
/**
|
|
305
|
+
* 배열에서 최솟값을 반환합니다.
|
|
306
|
+
* @function Min
|
|
307
|
+
* @memberof Array.prototype
|
|
308
|
+
* @param {Function} [selector=x => x] - 요소 선택 함수 (기본값: 요소 자체)
|
|
309
|
+
* @returns {number} 최솟값
|
|
310
|
+
* @example
|
|
311
|
+
* [1, 2, 3, 4].Min() // 1
|
|
312
|
+
* [{val: 5}, {val: 2}].Min(x => x.val) // 2
|
|
313
|
+
*/
|
|
148
314
|
Array.prototype.Min = function(selector = x => x) {
|
|
149
315
|
return Math.min(...this.map(selector));
|
|
150
316
|
};
|
|
151
317
|
|
|
152
|
-
|
|
318
|
+
/**
|
|
319
|
+
* 배열을 조건에 따라 두 그룹으로 분할합니다.
|
|
320
|
+
* @function Partition
|
|
321
|
+
* @memberof Array.prototype
|
|
322
|
+
* @param {Function} predicate - 분할 조건 함수
|
|
323
|
+
* @returns {Array<Array>} [조건을 만족하는 요소 배열, 만족하지 않는 요소 배열]
|
|
324
|
+
* @example
|
|
325
|
+
* [1, 2, 3, 4].Partition(x => x % 2 === 0)
|
|
326
|
+
* // [[2, 4], [1, 3]]
|
|
327
|
+
*/
|
|
153
328
|
Array.prototype.Partition = function(predicate) {
|
|
154
329
|
const trueArr = [];
|
|
155
330
|
const falseArr = [];
|