-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharrays.ts
More file actions
32 lines (30 loc) · 721 Bytes
/
arrays.ts
File metadata and controls
32 lines (30 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {
failure,
isSuccess,
ParseSuccess,
Parser,
ParseResult,
success,
propagateFailure,
} from './types'
/**
* Validate arrays
* @return a function that parses arrays
* @param parseItem
*/
export const array =
<T>(parseItem: Parser<T>): Parser<T[]> =>
(data) => {
if (!Array.isArray(data)) {
return failure('Not an array')
}
const dataOutput = []
for (let i = 0; i < data.length; i++) {
const parseResult = parseItem(data[i])
if (parseResult.tag === 'failure') {
return propagateFailure(parseResult, { tag: 'array', index: i })
}
dataOutput.push((parseResult as ParseSuccess<unknown>).value)
}
return success(dataOutput as T[])
}