@anil-labs/collection-js 0.1.0
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/LICENSE +21 -0
- package/README.md +251 -0
- package/dist/index.es.js +3211 -0
- package/dist/index.umd.cjs +8 -0
- package/dist/src/async/AsyncCollection.d.ts +57 -0
- package/dist/src/async/concurrent.d.ts +6 -0
- package/dist/src/async/index.d.ts +3 -0
- package/dist/src/collection/Collection.d.ts +274 -0
- package/dist/src/collection/HigherOrderProxy.d.ts +19 -0
- package/dist/src/collection/LazyCollection.d.ts +173 -0
- package/dist/src/collection/index.d.ts +5 -0
- package/dist/src/contracts/Arrayable.d.ts +4 -0
- package/dist/src/contracts/Enumerable.d.ts +17 -0
- package/dist/src/contracts/Jsonable.d.ts +3 -0
- package/dist/src/contracts/Macroable.d.ts +6 -0
- package/dist/src/contracts/index.d.ts +5 -0
- package/dist/src/exceptions/CollectionException.d.ts +3 -0
- package/dist/src/exceptions/ItemNotFoundException.d.ts +4 -0
- package/dist/src/exceptions/MultipleItemsFoundException.d.ts +5 -0
- package/dist/src/exceptions/UnexpectedValueException.d.ts +4 -0
- package/dist/src/exceptions/index.d.ts +4 -0
- package/dist/src/helpers/collect.d.ts +3 -0
- package/dist/src/helpers/index.d.ts +2 -0
- package/dist/src/helpers/lazy.d.ts +3 -0
- package/dist/src/index.d.ts +31 -0
- package/dist/src/io/csv.d.ts +30 -0
- package/dist/src/io/index.d.ts +5 -0
- package/dist/src/io/jsonl.d.ts +9 -0
- package/dist/src/io/streams.d.ts +27 -0
- package/dist/src/macros/Macroable.d.ts +20 -0
- package/dist/src/macros/index.d.ts +1 -0
- package/dist/src/operations/accessors.d.ts +19 -0
- package/dist/src/operations/aggregations.d.ts +9 -0
- package/dist/src/operations/chunking.d.ts +13 -0
- package/dist/src/operations/combine.d.ts +4 -0
- package/dist/src/operations/compare.d.ts +29 -0
- package/dist/src/operations/conditionals.d.ts +7 -0
- package/dist/src/operations/filters.d.ts +27 -0
- package/dist/src/operations/grouping.d.ts +5 -0
- package/dist/src/operations/index.d.ts +22 -0
- package/dist/src/operations/itertools.d.ts +37 -0
- package/dist/src/operations/joins.d.ts +17 -0
- package/dist/src/operations/mutations.d.ts +40 -0
- package/dist/src/operations/objectOps.d.ts +10 -0
- package/dist/src/operations/random.d.ts +2 -0
- package/dist/src/operations/reducers.d.ts +9 -0
- package/dist/src/operations/sequence.d.ts +4 -0
- package/dist/src/operations/setOps.d.ts +14 -0
- package/dist/src/operations/sliceOps.d.ts +8 -0
- package/dist/src/operations/sorting.d.ts +11 -0
- package/dist/src/operations/stats.d.ts +30 -0
- package/dist/src/operations/strings.d.ts +2 -0
- package/dist/src/operations/transformations.d.ts +9 -0
- package/dist/src/operations/uniqueness.d.ts +3 -0
- package/dist/src/support/arrayWrap.d.ts +6 -0
- package/dist/src/support/dataGet.d.ts +8 -0
- package/dist/src/support/deepClone.d.ts +1 -0
- package/dist/src/support/deepEqual.d.ts +11 -0
- package/dist/src/support/index.d.ts +8 -0
- package/dist/src/support/isObject.d.ts +4 -0
- package/dist/src/support/operatorForWhere.d.ts +4 -0
- package/dist/src/support/types.d.ts +15 -0
- package/dist/src/support/valueRetriever.d.ts +7 -0
- package/package.json +93 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Anil Kumar Thakur
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# @anil-labs/collection-js
|
|
2
|
+
|
|
3
|
+
A fluent, Laravel-inspired Collection library for JavaScript and TypeScript. Full parity with the **Laravel 13.x Collections** API — plus statistics, SQL-style joins, combinatorics, async streams, and CSV/JSONL I/O that go beyond it.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@anil-labs/collection-js)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
- **Strict TypeScript** — written in strict mode with deep type inference; no `any` in the public surface.
|
|
9
|
+
- **Immutable by default** — methods return new collections; the handful of mutators mirror Laravel exactly.
|
|
10
|
+
- **Three flavours** — eager `Collection`, generator-backed `LazyCollection`, and `AsyncCollection` for `AsyncIterable` sources.
|
|
11
|
+
- **Tree-shakable** — every operation is also a standalone pure function you can import directly.
|
|
12
|
+
- **Zero runtime dependencies.**
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @anil-labs/collection-js
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import collect from '@anil-labs/collection-js'
|
|
24
|
+
|
|
25
|
+
collect([1, 2, 3, 4, 5])
|
|
26
|
+
.filter((item) => item > 2)
|
|
27
|
+
.map((item) => item * 10)
|
|
28
|
+
.all()
|
|
29
|
+
// => [30, 40, 50]
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Working with Objects
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
const users = collect([
|
|
36
|
+
{ id: 1, name: 'Alice', role: 'admin', score: 95 },
|
|
37
|
+
{ id: 2, name: 'Bob', role: 'user', score: 80 },
|
|
38
|
+
{ id: 3, name: 'Charlie', role: 'admin', score: 92 },
|
|
39
|
+
{ id: 4, name: 'Diana', role: 'user', score: 88 }
|
|
40
|
+
])
|
|
41
|
+
|
|
42
|
+
users.where('role', 'admin').sortByDesc('score').pluck('name').all()
|
|
43
|
+
// => ['Alice', 'Charlie']
|
|
44
|
+
|
|
45
|
+
users.groupBy('role')
|
|
46
|
+
// => { admin: [...], user: [...] }
|
|
47
|
+
|
|
48
|
+
users.avg('score') // => 88.75
|
|
49
|
+
users.max('score') // => 95
|
|
50
|
+
users.median('score') // => 90
|
|
51
|
+
|
|
52
|
+
const [admins, regular] = users.partition((u) => u.role === 'admin')
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`where`-style filters support dot-notation paths and comparison operators, and `max`/`min` work on numbers, strings, and `Date`s:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
collect(['banana', 'apple', 'cherry']).max() // => 'cherry'
|
|
59
|
+
collect(orders).where('customer.country', 'FR').sum('total')
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Pattern filtering with `whereLike`
|
|
63
|
+
|
|
64
|
+
`whereLike` / `whereNotLike` filter by an SQL-`LIKE` pattern, where `%` matches any run of characters and `_` matches a single one (case-insensitive by default):
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
collect(users).whereLike('name', '%Smith') // ends with "Smith"
|
|
68
|
+
collect(users).whereLike('email', '%@gmail.com') // gmail addresses
|
|
69
|
+
collect(users).whereNotLike('name', 'A%', true) // case-sensitive: not starting with "A"
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Lazy Collections
|
|
73
|
+
|
|
74
|
+
For large or infinite datasets, use generator-backed lazy evaluation — values are produced on demand:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { LazyCollection } from '@anil-labs/collection-js'
|
|
78
|
+
|
|
79
|
+
new LazyCollection(function* () {
|
|
80
|
+
for (let i = 0; i < 1_000_000; i++) yield i
|
|
81
|
+
})
|
|
82
|
+
.filter((n) => n % 2 === 0)
|
|
83
|
+
.map((n) => n * 2)
|
|
84
|
+
.take(5)
|
|
85
|
+
.toArray()
|
|
86
|
+
// => [0, 4, 8, 12, 16] — only 5 items ever evaluated
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Lazy-only helpers: `tapEach`, `remember` (memoize pulled values), `takeUntilTimeout`, `throttle`, `withHeartbeat`.
|
|
90
|
+
|
|
91
|
+
## Async Collections
|
|
92
|
+
|
|
93
|
+
Stream and transform `AsyncIterable` sources with bounded concurrency:
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { AsyncCollection } from '@anil-labs/collection-js'
|
|
97
|
+
|
|
98
|
+
const results = await AsyncCollection.from(userIds)
|
|
99
|
+
.mapAsync((id) => fetchUser(id), { concurrency: 8 }) // ≤ 8 in flight, source order preserved
|
|
100
|
+
.filter((user) => user.active)
|
|
101
|
+
.take(100)
|
|
102
|
+
.toArray()
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Beyond Laravel
|
|
106
|
+
|
|
107
|
+
These extend the Laravel API for real-world data work:
|
|
108
|
+
|
|
109
|
+
**Statistics** — `variance` · `sampleVariance` · `stddev` · `sampleStddev` · `quantile` · `percentileAt` · `histogram` · `correlation`
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
collect(samples).stddev('latency')
|
|
113
|
+
collect(rows).correlation('spend', 'revenue')
|
|
114
|
+
collect(values).histogram(10)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**SQL-style joins** — `joinOn` (inner) · `leftJoin` · `rightJoin` · `outerJoin`
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
collect(orders).joinOn(customers, 'customerId', 'id', (order, customer) => ({
|
|
121
|
+
...order,
|
|
122
|
+
customerName: customer.name
|
|
123
|
+
}))
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Combinatorics & itertools** — `scan` · `pairwise` · `enumerate` · `cycle` · `interleave` · `permutations` · `combinations` · `powerSet`
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
collect([1, 2, 3]).permutations().all() // all 3! orderings
|
|
130
|
+
collect([1, 2, 3, 4]).combinations(2).all()
|
|
131
|
+
collect([1, 2, 3])
|
|
132
|
+
.scan((sum, n) => sum + n, 0)
|
|
133
|
+
.all() // running totals: [1, 3, 6]
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**CSV / JSONL / streams**
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { parseCsv, toCsv, parseJsonl, toJsonl, lines } from '@anil-labs/collection-js'
|
|
140
|
+
|
|
141
|
+
const rows = collect(parseCsv(csvText, { header: true }))
|
|
142
|
+
const csv = toCsv(rows.all())
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Higher-Order Messages
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
users.sum.score // => 355 (property-style)
|
|
149
|
+
users.where('role', 'admin').each.notify()
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Extending with Macros
|
|
153
|
+
|
|
154
|
+
Add your own methods at runtime — they participate in chaining like built-ins:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { Collection } from '@anil-labs/collection-js'
|
|
158
|
+
|
|
159
|
+
Collection.macro('toUpper', function (this: Collection<string>) {
|
|
160
|
+
return this.map((s) => s.toUpperCase())
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
collect(['a', 'b']).toUpper().all() // => ['A', 'B']
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Available Methods
|
|
167
|
+
|
|
168
|
+
<details>
|
|
169
|
+
<summary>Click to expand the full method list</summary>
|
|
170
|
+
|
|
171
|
+
### Creation
|
|
172
|
+
|
|
173
|
+
`make` · `collect` · `fromJson` · `fromEntries` · `fromMap` · `fromSet` · `times` · `range` · `wrap` · `unwrap` · `empty` · `lazy`
|
|
174
|
+
|
|
175
|
+
### Filtering
|
|
176
|
+
|
|
177
|
+
`filter` · `reject` · `where` · `whereStrict` · `whereBetween` · `whereNotBetween` · `whereIn` · `whereInStrict` · `whereNotIn` · `whereNotInStrict` · `whereNull` · `whereNotNull` · `whereLike` · `whereNotLike` · `whereInstanceOf` · `compact` · `first` · `firstOrFail` · `firstWhere` · `last` · `sole` · `unique` · `uniqueStrict` · `except` · `only` · `select`
|
|
178
|
+
|
|
179
|
+
### Transformation
|
|
180
|
+
|
|
181
|
+
`map` · `mapInto` · `mapSpread` · `mapToGroups` · `mapWithKeys` · `flatMap` · `flatten` · `collapse` · `collapseWithKeys` · `chunk` · `chunkWhile` · `sliding` · `split` · `splitIn` · `dot` · `undot` · `flip` · `pluck` · `values` · `keys` · `zip` · `combine` · `crossJoin`
|
|
182
|
+
|
|
183
|
+
### Aggregation
|
|
184
|
+
|
|
185
|
+
`sum` · `average` / `avg` · `min` · `max` · `minBy` · `maxBy` · `median` · `mode` · `count` · `countBy` · `percentage`
|
|
186
|
+
|
|
187
|
+
### Statistics
|
|
188
|
+
|
|
189
|
+
`variance` · `sampleVariance` · `stddev` · `sampleStddev` · `quantile` · `percentileAt` · `histogram` · `correlation`
|
|
190
|
+
|
|
191
|
+
### Joins
|
|
192
|
+
|
|
193
|
+
`joinOn` · `leftJoin` · `rightJoin` · `outerJoin`
|
|
194
|
+
|
|
195
|
+
### Itertools & combinatorics
|
|
196
|
+
|
|
197
|
+
`scan` · `pairwise` · `enumerate` · `cycle` · `interleave` · `permutations` · `combinations` · `powerSet`
|
|
198
|
+
|
|
199
|
+
### Sorting
|
|
200
|
+
|
|
201
|
+
`sort` · `sortBy` · `sortByDesc` · `sortDesc` · `sortKeys` · `sortKeysDesc` · `sortKeysUsing` · `reverse` · `shuffle`
|
|
202
|
+
|
|
203
|
+
### Searching
|
|
204
|
+
|
|
205
|
+
`contains` · `containsStrict` · `containsOneItem` · `doesntContain` · `doesntContainStrict` · `search` · `has` · `hasAny` · `hasMany` · `hasSole` · `every` · `some`
|
|
206
|
+
|
|
207
|
+
### Iteration
|
|
208
|
+
|
|
209
|
+
`each` · `eachSpread` · `tap` · `tapEach` · `transform` · `pipe` · `pipeInto` · `pipeThrough`
|
|
210
|
+
|
|
211
|
+
### Addition & Removal
|
|
212
|
+
|
|
213
|
+
`push` · `prepend` · `concat` · `merge` · `mergeRecursive` · `union` · `diff` · `diffAssoc` · `diffAssocUsing` · `diffKeys` · `intersect` · `intersectUsing` · `intersectAssoc` · `intersectAssocUsing` · `intersectByKeys` · `pop` · `shift` · `pull` · `forget` · `splice`
|
|
214
|
+
|
|
215
|
+
### Partitioning
|
|
216
|
+
|
|
217
|
+
`partition` · `groupBy` · `groupByMany` · `keyBy` · `take` · `takeUntil` · `takeWhile` · `skip` · `skipUntil` · `skipWhile` · `slice` · `forPage` · `nth` · `after` · `before`
|
|
218
|
+
|
|
219
|
+
### Conditional
|
|
220
|
+
|
|
221
|
+
`when` · `whenEmpty` · `whenNotEmpty` · `unless` · `unlessEmpty` · `unlessNotEmpty`
|
|
222
|
+
|
|
223
|
+
### Conversion
|
|
224
|
+
|
|
225
|
+
`all` · `toArray` · `toJson` · `toPrettyJson` · `toMap` · `toSet` · `implode` · `join` · `dump` · `dd`
|
|
226
|
+
|
|
227
|
+
### Misc
|
|
228
|
+
|
|
229
|
+
`collect` · `clone` · `macro` · `ensure` · `pad` · `multiply` · `random` · `reduce` · `reduceSpread` · `value` · `isEmpty` · `isNotEmpty` · `duplicates` · `duplicatesStrict` · `put` · `get` · `replace` · `replaceRecursive`
|
|
230
|
+
|
|
231
|
+
### Lazy-only
|
|
232
|
+
|
|
233
|
+
`remember` · `tapEach` · `takeUntilTimeout` · `throttle` · `withHeartbeat`
|
|
234
|
+
|
|
235
|
+
### Async (`AsyncCollection`)
|
|
236
|
+
|
|
237
|
+
`map` · `mapAsync` · `filter` · `filterAsync` · `flatMap` · `take` · `skip` · `takeWhile` · `skipWhile` · `chunk` · `tap` · `forEach` · `eachAsync` · `reduce` · `first` · `last` · `every` · `some` · `count` · `toArray` · `collect`
|
|
238
|
+
|
|
239
|
+
### I/O
|
|
240
|
+
|
|
241
|
+
`parseCsv` · `toCsv` · `parseJsonl` · `parseJsonlStream` · `toJsonl` · `fromReadable` · `lines`
|
|
242
|
+
|
|
243
|
+
</details>
|
|
244
|
+
|
|
245
|
+
## Laravel Compatibility
|
|
246
|
+
|
|
247
|
+
This library aims for full API parity with [Laravel 13.x Collections](https://laravel.com/docs/13.x/collections). If you're familiar with Laravel's `Collection` class, you'll feel right at home — `collect()` is the default export.
|
|
248
|
+
|
|
249
|
+
## License
|
|
250
|
+
|
|
251
|
+
[MIT](LICENSE)
|