Skip to content

Commit 477f870

Browse files
committed
Move isErrnoException to utils/errors and stringJoinWithSeparateFinalSeparator to utils/strings
1 parent aa73518 commit 477f870

File tree

7 files changed

+29
-33
lines changed

7 files changed

+29
-33
lines changed

.config/rollup.test.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const {
1818

1919
export default () =>
2020
baseConfig({
21-
input: ['alert-rules', 'misc', 'path-resolve'].reduce((o, k) => {
21+
input: ['alert-rules', 'errors', 'path-resolve'].reduce((o, k) => {
2222
o[k] = `${rootSrcPath}/utils/${k}.ts`
2323
return o
2424
}, {}),

src/utils/alert-rules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { setTimeout as wait } from 'node:timers/promises'
55
import config from '@socketsecurity/config'
66
import { isObject } from '@socketsecurity/registry/lib/objects'
77

8-
import { isErrnoException } from './misc'
8+
import { isErrnoException } from './errors'
99
import { getPublicToken, setupSdk } from './sdk'
1010
import { getSetting } from './settings'
1111
import constants from '../constants'

src/utils/errors.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ export class InputError extends Error {
99
this.body = body
1010
}
1111
}
12+
13+
export function isErrnoException(
14+
value: unknown
15+
): value is NodeJS.ErrnoException {
16+
if (!(value instanceof Error)) {
17+
return false
18+
}
19+
return (value as NodeJS.ErrnoException).code !== undefined
20+
}

src/utils/format-issues.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { stringJoinWithSeparateFinalSeparator } from './misc'
21
import { pick } from './objects'
2+
import { stringJoinWithSeparateFinalSeparator } from './strings'
33

44
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
55

@@ -22,28 +22,24 @@ function getDesiredSeverities(
2222
lowestToInclude: SocketIssue['severity'] | undefined
2323
): SocketIssue['severity'][] {
2424
const result: SocketIssue['severity'][] = []
25-
2625
for (const severity of SEVERITIES_BY_ORDER) {
2726
result.push(severity)
2827
if (severity === lowestToInclude) {
2928
break
3029
}
3130
}
32-
3331
return result
3432
}
3533

3634
export function formatSeverityCount(
3735
severityCount: Record<SocketIssue['severity'], number>
3836
): string {
3937
const summary: string[] = []
40-
4138
for (const severity of SEVERITIES_BY_ORDER) {
4239
if (severityCount[severity]) {
4340
summary.push(`${severityCount[severity]} ${severity}`)
4441
}
4542
}
46-
4743
return stringJoinWithSeparateFinalSeparator(summary)
4844
}
4945

@@ -57,16 +53,13 @@ export function getSeverityCount(
5753
) as Record<SocketIssue['severity'], number>
5854

5955
for (const issue of issues) {
60-
const value = issue.value
61-
56+
const { value } = issue
6257
if (!value) {
6358
continue
6459
}
65-
6660
if (severityCount[value.severity] !== undefined) {
6761
severityCount[value.severity] += 1
6862
}
6963
}
70-
7164
return severityCount
7265
}

src/utils/misc.ts

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,3 @@ export function createDebugLogger(
77
? (...params: unknown[]): void => console.error(logSymbols.info, ...params)
88
: () => {}
99
}
10-
11-
export function isErrnoException(
12-
value: unknown
13-
): value is NodeJS.ErrnoException {
14-
if (!(value instanceof Error)) {
15-
return false
16-
}
17-
return (value as NodeJS.ErrnoException).code !== undefined
18-
}
19-
20-
export function stringJoinWithSeparateFinalSeparator(
21-
list: (string | undefined)[],
22-
separator: string = ' and '
23-
): string {
24-
const values = list.filter(value => !!value)
25-
if (values.length < 2) {
26-
return values[0] || ''
27-
}
28-
const finalValue = values.pop()
29-
return values.join(', ') + separator + finalValue
30-
}

src/utils/strings.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function stringJoinWithSeparateFinalSeparator(
2+
list: (string | undefined)[],
3+
separator: string = ' and '
4+
): string {
5+
const values = list.filter(Boolean)
6+
const { length } = values
7+
if (!length) {
8+
return ''
9+
}
10+
if (length === 1) {
11+
return values[0]
12+
}
13+
const finalValue = values.pop()
14+
return `${values.join(', ')}${separator}${finalValue}`
15+
}

test/misc.test.ts renamed to test/errors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { readFileSync } from 'node:fs'
33
import path from 'node:path'
44
import { describe, it } from 'node:test'
55

6-
import { isErrnoException } from './dist/misc'
6+
import { isErrnoException } from './dist/errors'
77

88
const testPath = __dirname
99

0 commit comments

Comments
 (0)