Skip to content

Commit 342adea

Browse files
committed
improvement(utils): add toArray and the scalar coercions, replacing 44 copies
Ten files declared `Array.isArray(v) ? v : []` and 34 declared the `typeof v === 'x' ? v : null` one-liner under eleven different names.
1 parent 83fd36a commit 342adea

64 files changed

Lines changed: 1381 additions & 1398 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/sim/app/api/v2/tables/utils.ts‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { toStringOrNull } from '@sim/utils/coerce'
12
import type {
23
V2ApiTable,
34
V2EnrichmentProviderOutcome,
@@ -238,11 +239,6 @@ function storedNumber(value: unknown): number {
238239
return typeof value === 'number' && Number.isFinite(value) ? value : 0
239240
}
240241

241-
/** Reads a stored field that the published shape declares as a nullable string. */
242-
function storedNullableString(value: unknown): string | null {
243-
return typeof value === 'string' ? value : null
244-
}
245-
246242
/**
247243
* Reads a stored timestamp, keeping only a value the published `date-time`
248244
* format will accept. A Postgres literal or a half-written blob becomes `null`
@@ -257,13 +253,13 @@ function storedTimestamp(value: unknown): string | null {
257253
function toApiEnrichmentProvider(value: unknown): V2EnrichmentProviderOutcome {
258254
const provider = (value ?? {}) as Record<string, unknown>
259255
return {
260-
id: storedNullableString(provider.id) ?? '',
261-
label: storedNullableString(provider.label) ?? '',
262-
toolId: storedNullableString(provider.toolId) ?? '',
263-
status: storedNullableString(provider.status) ?? 'not_run',
256+
id: toStringOrNull(provider.id) ?? '',
257+
label: toStringOrNull(provider.label) ?? '',
258+
toolId: toStringOrNull(provider.toolId) ?? '',
259+
status: toStringOrNull(provider.status) ?? 'not_run',
264260
cost: storedNumber(provider.cost),
265261
durationMs: storedNumber(provider.durationMs),
266-
error: storedNullableString(provider.error),
262+
error: toStringOrNull(provider.error),
267263
}
268264
}
269265

@@ -287,7 +283,7 @@ export function toApiEnrichmentDetail(
287283
completedAt: storedTimestamp(stored.completedAt),
288284
durationMs: storedNumber(stored.durationMs),
289285
totalCost: storedNumber(stored.totalCost),
290-
matchedProvider: storedNullableString(stored.matchedProvider),
286+
matchedProvider: toStringOrNull(stored.matchedProvider),
291287
aborted: stored.aborted === true,
292288
providers: Array.isArray(stored.providers) ? stored.providers.map(toApiEnrichmentProvider) : [],
293289
}

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/utils.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { toArray } from '@sim/utils/object'
12
/**
23
* Extracts the raw value from a preview context entry.
34
*
@@ -41,5 +42,5 @@ export function parseJsonArrayValue<T>(value: unknown): T[] {
4142
return []
4243
}
4344
}
44-
return Array.isArray(parsed) ? (parsed as T[]) : []
45+
return toArray<T>(parsed)
4546
}

‎apps/sim/connectors/grain/grain.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createLogger } from '@sim/logger'
22
import { getErrorMessage } from '@sim/utils/errors'
3+
import { toArray } from '@sim/utils/object'
34
import { fetchWithRetry } from '@/lib/knowledge/documents/secure-fetch.server'
45
import { VALIDATE_RETRY_OPTIONS } from '@/lib/knowledge/documents/utils'
56
import { grainConnectorMeta } from '@/connectors/grain/meta'
@@ -328,7 +329,7 @@ async function fetchTranscript(
328329
}
329330

330331
const data = await response.json()
331-
return Array.isArray(data) ? (data as GrainTranscriptSegment[]) : []
332+
return toArray<GrainTranscriptSegment>(data)
332333
}
333334

334335
export const grainConnector: ConnectorConfig = {

‎apps/sim/lib/internal/asana/client.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { toArray } from '@sim/utils/object'
12
import { readResponseTextWithLimit } from '@/lib/core/utils/stream-limits'
23
import { AsanaOperationError } from '@/lib/internal/asana/errors'
34

@@ -13,7 +14,7 @@ export function asObject(value: unknown): AsanaJsonObject {
1314
}
1415

1516
export function asArray(value: unknown): unknown[] {
16-
return Array.isArray(value) ? value : []
17+
return toArray(value)
1718
}
1819

1920
function providerErrorMessage(response: Response, text: string): string {

‎apps/sim/lib/internal/cbinsights/operations/chat.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { toStringOrNull } from '@sim/utils/coerce'
12
import type { InternalToolOperationImplementation } from '@/lib/internal/tool-operations/types'
23
import type { CbInsightsChatParams } from '@/tools/cbinsights/chat'
34
import {
45
asArray,
5-
asString,
66
asStringArray,
77
cbInsightsRequest,
88
compactBody,
@@ -29,9 +29,9 @@ export const executeCbinsightsChatOperation: InternalToolOperationImplementation
2929
body: compactBody({ message, chatID: parseOptionalStringParam(params.chatId, 'chatId') }),
3030
},
3131
(data) => ({
32-
chatId: asString(data.chatID),
33-
title: asString(data.title),
34-
message: asString(data.message),
32+
chatId: toStringOrNull(data.chatID),
33+
title: toStringOrNull(data.title),
34+
message: toStringOrNull(data.message),
3535
sources: asArray(data.sources),
3636
relatedContent: asArray(data.relatedContent),
3737
suggestions: asStringArray(data.suggestions),

‎apps/sim/lib/internal/cbinsights/operations/get-exit-probability-history.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { toStringOrNull } from '@sim/utils/coerce'
12
import type { InternalToolOperationImplementation } from '@/lib/internal/tool-operations/types'
23
import type { CbInsightsExitProbabilityHistoryParams } from '@/tools/cbinsights/get_exit_probability_history'
34
import {
45
asArray,
5-
asString,
66
cbInsightsRequest,
77
compactBody,
88
parseOptionalStringParam,
@@ -25,7 +25,7 @@ export const executeCbinsightsGetExitProbabilityHistoryOperation: InternalToolOp
2525
(data) => ({
2626
ipo: asArray(data.ipo),
2727
mna: asArray(data.mna),
28-
incompleteRoundType: asString(data.incompleteRoundType),
28+
incompleteRoundType: toStringOrNull(data.incompleteRoundType),
2929
}),
3030
signal
3131
)

‎apps/sim/lib/internal/cbinsights/operations/get-org-funding-window.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { toNumberOrNull, toStringOrNull } from '@sim/utils/coerce'
12
import { toRecordOrNull } from '@sim/utils/object'
23
import type { InternalToolOperationImplementation } from '@/lib/internal/tool-operations/types'
34
import type { CbInsightsOrgParams } from '@/tools/cbinsights/types'
4-
import { asNumber, asString, cbInsightsRequest, requireOrgId } from '@/tools/cbinsights/utils'
5+
import { cbInsightsRequest, requireOrgId } from '@/tools/cbinsights/utils'
56

67
export const executeCbinsightsGetOrgFundingWindowOperation: InternalToolOperationImplementation<
78
CbInsightsOrgParams
@@ -17,9 +18,9 @@ export const executeCbinsightsGetOrgFundingWindowOperation: InternalToolOperatio
1718
params,
1819
{ path: `/v2/organizations/${orgId}/fundingwindow` },
1920
(data) => ({
20-
windowStart: asString(data.windowStart),
21-
windowEnd: asString(data.windowEnd),
22-
cohortNextRoundRate: asNumber(data.cohortNextRoundRate),
21+
windowStart: toStringOrNull(data.windowStart),
22+
windowEnd: toStringOrNull(data.windowEnd),
23+
cohortNextRoundRate: toNumberOrNull(data.cohortNextRoundRate),
2324
cohortCriteria: toRecordOrNull(data.cohortCriteria),
2425
latestFunding: toRecordOrNull(data.latestFunding),
2526
}),

‎apps/sim/lib/internal/cbinsights/operations/get-org-management-and-board.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { toNumberOrNull } from '@sim/utils/coerce'
12
import type { InternalToolOperationImplementation } from '@/lib/internal/tool-operations/types'
23
import type { CbInsightsOrgManagementParams } from '@/tools/cbinsights/get_org_management_and_board'
34
import {
45
asArray,
5-
asNumber,
66
cbInsightsRequest,
77
compactBody,
88
parseIdListParam,
@@ -21,7 +21,7 @@ export const executeCbinsightsGetOrgManagementAndBoardOperation: InternalToolOpe
2121
},
2222
(data) => ({
2323
people: asArray(data.people),
24-
mosaicManagement: asNumber(data.mosaicManagement),
24+
mosaicManagement: toNumberOrNull(data.mosaicManagement),
2525
}),
2626
signal
2727
)

‎apps/sim/lib/internal/cbinsights/operations/get-org-revenue.ts‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1+
import { toNumberOrNull, toStringOrNull } from '@sim/utils/coerce'
12
import type { InternalToolOperationImplementation } from '@/lib/internal/tool-operations/types'
23
import type { CbInsightsOrgParams } from '@/tools/cbinsights/types'
3-
import {
4-
asArray,
5-
asNumber,
6-
asString,
7-
cbInsightsRequest,
8-
requireOrgId,
9-
} from '@/tools/cbinsights/utils'
4+
import { asArray, cbInsightsRequest, requireOrgId } from '@/tools/cbinsights/utils'
105

116
export const executeCbinsightsGetOrgRevenueOperation: InternalToolOperationImplementation<
127
CbInsightsOrgParams
@@ -21,9 +16,9 @@ export const executeCbinsightsGetOrgRevenueOperation: InternalToolOperationImple
2116
params,
2217
{ path: `/v2/organizations/${orgId}/revenuebyyear` },
2318
(data) => ({
24-
orgId: asNumber(data.orgId),
25-
orgName: asString(data.orgName),
26-
orgUrl: asString(data.orgUrl),
19+
orgId: toNumberOrNull(data.orgId),
20+
orgName: toStringOrNull(data.orgName),
21+
orgUrl: toStringOrNull(data.orgUrl),
2722
revenue: asArray(data.revenue),
2823
}),
2924
signal

‎apps/sim/lib/internal/cbinsights/operations/get-scouting-report.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { toStringOrNull } from '@sim/utils/coerce'
12
import { toRecordOrNull } from '@sim/utils/object'
23
import type { InternalToolOperationImplementation } from '@/lib/internal/tool-operations/types'
34
import type { CbInsightsOrgParams } from '@/tools/cbinsights/types'
45
import {
5-
asString,
66
cbInsightsRequest,
77
requireOrgId,
88
SCOUTING_REPORT_TIMEOUT_MS,
@@ -24,8 +24,8 @@ export const executeCbinsightsGetScoutingReportOperation: InternalToolOperationI
2424
},
2525
(data) => ({
2626
orgInfo: toRecordOrNull(data.orgInfo),
27-
reportMarkdown: asString(data.reportMarkdown),
28-
reportJson: asString(data.reportJson),
27+
reportMarkdown: toStringOrNull(data.reportMarkdown),
28+
reportJson: toStringOrNull(data.reportJson),
2929
}),
3030
signal
3131
)

0 commit comments

Comments
 (0)