Skip to content

Commit 1f681b6

Browse files
committed
fix(aggregators): 집계 함수 null/undefined 체크 strictNullChecks 대응
- uniques, sum, extremes, quantile, runningStat, sumOverSum 등 주요 집계 함수에서 null/undefined 안전성 강화 - 옵셔널 체이닝(record?.[attr]) 및 raw != null 패턴 적용 - 기존 동작(값이 없으면 집계에서 무시)은 100% 유지, 타입스크립트 strict 옵션에서 타입 안전성만 강화 관련 이슈: #132
1 parent e93aecf commit 1f681b6

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/helper/utilities.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,9 @@ const aggregatorTemplates: AggregatorTemplates = {
313313
return ([attr]: [string]) => () => ({
314314
uniq: [] as any[],
315315
push (record: DataRecord) {
316-
if (!this.uniq.includes(record[attr])) {
317-
this.uniq.push(record[attr])
316+
const value = record?.[attr]
317+
if (!this.uniq.includes(value)) {
318+
this.uniq.push(value)
318319
}
319320
},
320321
value () {
@@ -329,7 +330,8 @@ const aggregatorTemplates: AggregatorTemplates = {
329330
return ([attr]: [string]) => () => ({
330331
sum: 0,
331332
push (record: DataRecord) {
332-
const val = parseFloat(String(record[attr]))
333+
const raw = record?.[attr]
334+
const val = raw != null ? parseFloat(String(raw)) : NaN
333335
if (!isNaN(val)) {
334336
this.sum += val
335337
}
@@ -350,9 +352,10 @@ const aggregatorTemplates: AggregatorTemplates = {
350352
attr
351353
),
352354
push (record: DataRecord) {
353-
let x = record[attr]
355+
const raw = record?.[attr]
356+
let x = raw
354357
if (['min', 'max'].includes(mode)) {
355-
const numX = parseFloat(String(x))
358+
const numX = x != null ? parseFloat(String(x)) : NaN
356359
if (!isNaN(numX)) {
357360
this.val = Math[mode as 'min' | 'max'](numX, this.val !== null ? this.val : numX)
358361
}
@@ -387,12 +390,13 @@ const aggregatorTemplates: AggregatorTemplates = {
387390
return ([attr]: [string]) => () => ({
388391
vals: [] as number[],
389392
push (record: DataRecord) {
390-
const x = parseFloat(String(record[attr]))
393+
const raw = record?.[attr]
394+
const x = raw != null ? parseFloat(String(raw)) : NaN
391395
if (!isNaN(x)) {
392396
this.vals.push(x)
393397
}
394398
},
395-
value () {
399+
value (): number | null {
396400
if (this.vals.length === 0) {
397401
return null
398402
}
@@ -411,7 +415,8 @@ const aggregatorTemplates: AggregatorTemplates = {
411415
m: 0.0,
412416
s: 0.0,
413417
push (record: DataRecord) {
414-
const x = parseFloat(String(record[attr]))
418+
const raw = record?.[attr]
419+
const x = raw != null ? parseFloat(String(raw)) : NaN
415420
if (isNaN(x)) {
416421
return
417422
}
@@ -426,7 +431,7 @@ const aggregatorTemplates: AggregatorTemplates = {
426431
value () {
427432
if (mode === 'mean') {
428433
if (this.n === 0) {
429-
return 0 / 0
434+
return NaN
430435
}
431436
return this.m
432437
}
@@ -452,8 +457,10 @@ const aggregatorTemplates: AggregatorTemplates = {
452457
sumNum: 0,
453458
sumDenom: 0,
454459
push (record: DataRecord) {
455-
const numVal = parseFloat(String(record[num]))
456-
const denomVal = parseFloat(String(record[denom]))
460+
const rawNum = record?.[num]
461+
const rawDenom = record?.[denom]
462+
const numVal = rawNum != null ? parseFloat(String(rawNum)) : NaN
463+
const denomVal = rawDenom != null ? parseFloat(String(rawDenom)) : NaN
457464
if (!isNaN(numVal)) {
458465
this.sumNum += numVal
459466
}

0 commit comments

Comments
 (0)