Skip to content

Commit 9a81275

Browse files
committed
refactor: simplify pin input normalization with toPinnable helper
1 parent 69e67d6 commit 9a81275

File tree

1 file changed

+22
-67
lines changed

1 file changed

+22
-67
lines changed

src/lib/pins/normalise-input.ts

Lines changed: 22 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -90,41 +90,12 @@ export async function * normaliseInput (input: Source): AsyncGenerator<Pin> {
9090
return iterator
9191
}
9292

93-
// Iterable<CID>
94-
if (isCID(first.value)) {
95-
yield toPin({ cid: first.value })
96-
for (const cid of iterator) {
97-
yield toPin({ cid })
98-
}
99-
return
93+
// Iterable<ToPin>
94+
yield toPin(toPinnable(first.value))
95+
for (const obj of iterator) {
96+
yield toPin(toPinnable(obj))
10097
}
101-
102-
// Iterable<String>
103-
if (typeof first.value === 'string') {
104-
yield toPin({ path: first.value })
105-
for (const path of iterator) {
106-
yield toPin({ path })
107-
}
108-
return
109-
}
110-
111-
// Iterable<Pinnable>
112-
if (first.value.cid != null || first.value.path != null) {
113-
yield toPin(first.value)
114-
for (const obj of iterator) {
115-
// Handle mixed iterables - obj could be CID or Pinnable
116-
if (isCID(obj)) {
117-
yield toPin({ cid: obj })
118-
} else if (typeof obj === 'string') {
119-
yield toPin({ path: obj })
120-
} else {
121-
yield toPin(obj)
122-
}
123-
}
124-
return
125-
}
126-
127-
throw new InvalidParametersError(`Unexpected input: ${typeof input}`)
98+
return
12899
}
129100

130101
// AsyncIterable<?>
@@ -133,43 +104,27 @@ export async function * normaliseInput (input: Source): AsyncGenerator<Pin> {
133104
const first = await iterator.next()
134105
if (first.done === true) return iterator
135106

136-
// AsyncIterable<CID>
137-
if (isCID(first.value)) {
138-
yield toPin({ cid: first.value })
139-
for await (const cid of iterator) {
140-
yield toPin({ cid })
141-
}
142-
return
143-
}
144-
145-
// AsyncIterable<String>
146-
if (typeof first.value === 'string') {
147-
yield toPin({ path: first.value })
148-
for await (const path of iterator) {
149-
yield toPin({ path })
150-
}
151-
return
107+
// AsyncIterable<ToPin>
108+
yield toPin(toPinnable(first.value))
109+
for await (const obj of iterator) {
110+
yield toPin(toPinnable(obj))
152111
}
112+
return
113+
}
153114

154-
// AsyncIterable<{ cid: CID|String recursive, metadata }>
155-
if (first.value.cid != null || first.value.path != null) {
156-
yield toPin(first.value)
157-
for await (const obj of iterator) {
158-
// Handle mixed async iterables - obj could be CID or Pinnable
159-
if (isCID(obj)) {
160-
yield toPin({ cid: obj })
161-
} else if (typeof obj === 'string') {
162-
yield toPin({ path: obj })
163-
} else {
164-
yield toPin(obj)
165-
}
166-
}
167-
return
168-
}
115+
throw new InvalidParametersError(`Unexpected input: ${typeof input}`)
116+
}
169117

170-
throw new InvalidParametersError(`Unexpected input: ${typeof input}`)
118+
function toPinnable (input: ToPin): Pinnable {
119+
if (isCID(input)) {
120+
return { cid: input }
121+
}
122+
if (typeof input === 'string') {
123+
return { path: input }
124+
}
125+
if (typeof input === 'object' && (input.cid != null || input.path != null)) {
126+
return input
171127
}
172-
173128
throw new InvalidParametersError(`Unexpected input: ${typeof input}`)
174129
}
175130

0 commit comments

Comments
 (0)