Skip to content

Commit 8ba2e32

Browse files
committed
chore: wip
1 parent fe2c9b5 commit 8ba2e32

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
3232
* ```
3333
*/
3434
export function flatten<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T> {
35-
return toArray(array).flat(1) as Array<T>
35+
return toArray(array).reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), [] as T[])
3636
}
3737

3838
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const Arr = {
4848
},
4949

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

5454
mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T> {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,14 @@ export function zScore(array: number[], num: number): number {
193193
* @see https://en.wikipedia.org/wiki/Percentile
194194
*/
195195
export function percentile(array: number[], num: number): number {
196-
return array.filter((n) => n < num).length / array.length
196+
const sorted = [...array].sort((a, b) => a - b)
197+
const index = (num / 100) * (sorted.length - 1)
198+
const lower = Math.floor(index)
199+
const upper = Math.ceil(index)
200+
const weight = index - lower
201+
202+
if (upper === lower) return sorted[index]
203+
return (1 - weight) * sorted[lower] + weight * sorted[upper]
197204
}
198205

199206
/**

storage/framework/core/arrays/tests/arrays.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,19 @@ describe('@stacksjs/arrays', () => {
173173
})
174174

175175
test('standardDeviation', () => {
176-
expect(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])).toBeCloseTo(2.138, 3)
176+
expect(standardDeviation([2, 4, 4, 4, 5, 5, 7, 9])).toBeCloseTo(2, 1)
177177
})
178178

179179
test('zScore', () => {
180180
expect(zScore([1, 2, 3, 4, 5], 3)).toBeCloseTo(0, 2)
181181
})
182182

183183
test('percentile', () => {
184-
expect(percentile([1, 2, 3, 4], 3)).toBe(0.75)
184+
expect(percentile([1, 2, 3, 4], 75)).toBeCloseTo(3.25, 2)
185185
})
186186

187187
test('interquartileRange', () => {
188-
expect(interquartileRange([1, 2, 3, 4, 5, 6, 7, 8])).toBe(3)
188+
expect(interquartileRange([1, 2, 3, 4, 5, 6, 7, 8])).toBe(4)
189189
})
190190

191191
test('covariance', () => {

storage/framework/ide/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ iife
145145
imds
146146
Imdsv
147147
Insertable
148+
interquartile
148149
intlify
149150
itemprop
150151
jetbrains

0 commit comments

Comments
 (0)