Skip to content

Commit 468a615

Browse files
committed
chore: wip
1 parent 25a4ef5 commit 468a615

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

storage/framework/core/arrays/build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { dts } from 'bun-plugin-dts-auto'
12
import { intro, outro } from '../build/src'
23

34
const { startTime } = await intro({
@@ -12,6 +13,7 @@ const result = await Bun.build({
1213
sourcemap: 'linked',
1314
minify: true,
1415
external: ['@stacksjs/utils'],
16+
plugins: [dts()],
1517
})
1618

1719
await outro({

storage/framework/core/arrays/src/contains.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* contains('foo', ['bar']) // false
1010
* ```
1111
*/
12-
export function contains(needle: string, haystack: string[]) {
12+
export function contains(needle: string, haystack: string[]): boolean {
1313
return haystack.some((hay) => needle.includes(hay))
1414
}
1515

@@ -23,7 +23,7 @@ export function contains(needle: string, haystack: string[]) {
2323
* containsAll(['foo', 'bar'], ['foo', 'baz']) // false
2424
* ```
2525
*/
26-
export function containsAll(needles: string[], haystack: string[]) {
26+
export function containsAll(needles: string[], haystack: string[]): boolean {
2727
return needles.every((needle) => contains(needle, haystack))
2828
}
2929

@@ -38,7 +38,7 @@ export function containsAll(needles: string[], haystack: string[]) {
3838
* containsAny(['foo', 'bar'], ['baz']) // false
3939
* ```
4040
*/
41-
export function containsAny(needles: string[], haystack: string[]) {
41+
export function containsAny(needles: string[], haystack: string[]): boolean {
4242
return needles.some((needle) => contains(needle, haystack))
4343
}
4444

@@ -53,7 +53,7 @@ export function containsAny(needles: string[], haystack: string[]) {
5353
* containsNone(['foo', 'bar'], ['baz']) // true
5454
* ```
5555
*/
56-
export function containsNone(needles: string[], haystack: string[]) {
56+
export function containsNone(needles: string[], haystack: string[]): boolean {
5757
return !containsAny(needles, haystack)
5858
}
5959

@@ -68,7 +68,7 @@ export function containsNone(needles: string[], haystack: string[]) {
6868
* containsOnly(['foo', 'bar'], ['foo', 'bar']) // true
6969
* ```
7070
*/
71-
export function containsOnly(needles: string[], haystack: string[]) {
71+
export function containsOnly(needles: string[], haystack: string[]): boolean {
7272
return containsAll(haystack, needles)
7373
}
7474

@@ -82,6 +82,6 @@ export function containsOnly(needles: string[], haystack: string[]) {
8282
* doesNotContain('foo', ['bar']) // true
8383
* ```
8484
*/
85-
export function doesNotContain(needle: string, haystack: string[]) {
85+
export function doesNotContain(needle: string, haystack: string[]): boolean {
8686
return !contains(needle, haystack)
8787
}

storage/framework/core/arrays/src/helpers.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
3131
* flatten([1, [2, [3, [4, [5]]]]]) // [1, 2, 3, 4, 5]
3232
* ```
3333
*/
34-
export function flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T> {
35-
return toArray(array).reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), [] as T[])
34+
export function flatten<T>(array?: Nullable<Arrayable<T | T[]>>): T[] {
35+
return toArray(array).reduce((acc: T[], val) => acc.concat(Array.isArray(val) ? flatten(val) : (val as T)), [])
3636
}
3737

3838
/**
@@ -181,7 +181,7 @@ export function last<T>(array: readonly T[]): T | undefined {
181181
* Arr.remove(arr, 4) // false
182182
* console.log(arr) // [1, 3]
183183
*/
184-
export function remove<T>(array: T[], value: T) {
184+
export function remove<T>(array: T[], value: T): boolean {
185185
if (!array) return false
186186

187187
const index = array.indexOf(value)
@@ -258,7 +258,7 @@ export function move<T>(array: T[], from: number, to: number): T[] {
258258
* clampArrayRange([1, 2, 3], 4) // 2
259259
* clampArrayRange([1, 2, 3], -1) // 0
260260
*/
261-
export function clampArrayRange(arr: readonly unknown[], n: number) {
261+
export function clampArrayRange(arr: readonly unknown[], n: number): number {
262262
return clamp(n, 0, arr.length - 1)
263263
}
264264

@@ -271,8 +271,9 @@ export function clampArrayRange(arr: readonly unknown[], n: number) {
271271
* sample([1, 2, 3, 4], 2) // [2, 3]
272272
* ```
273273
*/
274-
export function sample<T>(arr: T[], count: number) {
275-
return Array.from({ length: count }, (_) => arr[Math.round(Math.random() * (arr.length - 1))])
274+
export function sample<T>(arr: T[], count: number): T[] {
275+
// biome-ignore lint/style/noNonNullAssertion: it should be fine, open to improvements
276+
return Array.from({ length: count }, () => arr[Math.floor(Math.random() * arr.length)]!)
276277
}
277278

278279
/**

storage/framework/core/arrays/src/macro.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,36 @@ import {
1919
import { average, median, mode, range, sum } from './math'
2020

2121
export const Arr = {
22-
contains(needle: string, haystack: string[]) {
22+
contains(needle: string, haystack: string[]): boolean {
2323
return contains(needle, haystack)
2424
},
2525

26-
containsAll(needles: string[], haystack: string[]) {
26+
containsAll(needles: string[], haystack: string[]): boolean {
2727
return containsAll(needles, haystack)
2828
},
2929

30-
containsAny(needles: string[], haystack: string[]) {
30+
containsAny(needles: string[], haystack: string[]): boolean {
3131
return containsAny(needles, haystack)
3232
},
3333

34-
containsNone(needles: string[], haystack: string[]) {
34+
containsNone(needles: string[], haystack: string[]): boolean {
3535
return containsNone(needles, haystack)
3636
},
3737

38-
containsOnly(needles: string[], haystack: string[]) {
38+
containsOnly(needles: string[], haystack: string[]): boolean {
3939
return containsOnly(needles, haystack)
4040
},
4141

42-
doesNotContain(needle: string, haystack: string[]) {
42+
doesNotContain(needle: string, haystack: string[]): boolean {
4343
return doesNotContain(needle, haystack)
4444
},
4545

4646
toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
4747
return toArray(array)
4848
},
4949

50-
flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T> {
51-
return toArray(array).reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), [] as T[])
50+
flatten<T>(array?: Nullable<Arrayable<T | T[]>>): T[] {
51+
return flatten(array)
5252
},
5353

5454
mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T> {
@@ -70,11 +70,7 @@ export const Arr = {
7070
* Returns random item/s from the array
7171
*/
7272
sample<T>(arr: T[], count = 1): T[] {
73-
// Get the sample array (may contain undefined values)
74-
const sampleArr = sample(arr, count)
75-
76-
// Filter out any undefined values
77-
return sampleArr.filter((item): item is T => item !== undefined)
73+
return sample(arr, count)
7874
},
7975

8076
unique<T>(arr: T[]): T[] {
@@ -89,7 +85,7 @@ export const Arr = {
8985
return last(arr)
9086
},
9187

92-
remove<T>(arr: T[], value: T) {
88+
remove<T>(arr: T[], value: T): boolean {
9389
return remove(arr, value)
9490
},
9591

@@ -105,7 +101,7 @@ export const Arr = {
105101
return move(arr, from, to)
106102
},
107103

108-
clampArrayRange(arr: readonly unknown[], n: number) {
104+
clampArrayRange(arr: readonly unknown[], n: number): number {
109105
return clampArrayRange(arr, n)
110106
},
111107

@@ -146,4 +142,4 @@ export const Arr = {
146142
},
147143
}
148144

149-
export const arr = Arr
145+
export const arr: typeof Arr = Arr

storage/framework/core/arrays/src/math.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ export function percentile(array: number[], num: number): number {
199199
const upper = Math.ceil(index)
200200
const weight = index - lower
201201

202-
if (upper === lower) return sorted[index]
203-
return (1 - weight) * sorted[lower] + weight * sorted[upper]
202+
if (upper === lower) return sorted[index] ?? 0
203+
return (1 - weight) * (sorted[lower] ?? 0) + weight * (sorted[upper] ?? 0)
204204
}
205205

206206
/**

storage/framework/core/cli/build.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// import { dts } from 'bun-plugin-dts-auto'
21
import { intro, outro } from '../build/src'
32
import { dts } from './dts'
43

4+
// import { dts } from 'bun-plugin-dts-auto'
5+
56
const { startTime } = await intro({
67
dir: import.meta.dir,
78
})

0 commit comments

Comments
 (0)