• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

C2FO / fast-csv / 13550863665

26 Feb 2025 06:30PM CUT coverage: 95.907%. Remained the same
13550863665

Pull #1101

github

web-flow
Merge 78434839f into 947bf442a
Pull Request #1101: chore(deps): update node.js to v22

325 of 353 branches covered (92.07%)

Branch coverage included in aggregate %.

753 of 771 relevant lines covered (97.67%)

2176197.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/packages/parse/src/transforms/RowTransformerValidator.ts
1
import isFunction from 'lodash.isfunction';
126✔
2
import {
126✔
3
    Row,
4
    RowTransformFunction,
5
    RowValidatorCallback,
6
    isSyncValidate,
7
    RowValidate,
8
    isSyncTransform,
9
    AsyncRowTransform,
10
    RowTransformCallback,
11
} from '../types';
12

13
type RowValidator<R extends Row> = (row: R, cb: RowValidatorCallback<R>) => void;
14

15
export class RowTransformerValidator<I extends Row, O extends Row> {
126✔
16
    // eslint-disable-next-line @typescript-eslint/no-shadow
17
    private static createTransform<I extends Row, O extends Row>(
18
        transformFunction: RowTransformFunction<I, O>,
19
    ): AsyncRowTransform<I, O> {
20
        if (isSyncTransform(transformFunction)) {
78✔
21
            return (row: I, cb: RowTransformCallback<O>): void => {
36✔
22
                let transformed: O | null = null;
174✔
23
                try {
174✔
24
                    transformed = transformFunction(row);
174✔
25
                } catch (e) {
26
                    return cb(e);
6✔
27
                }
28
                return cb(null, transformed);
168✔
29
            };
30
        }
31
        return transformFunction;
42✔
32
    }
33

34
    private static createValidator<R extends Row>(validateFunction: RowValidate<R>): RowValidator<R> {
35
        if (isSyncValidate(validateFunction)) {
78✔
36
            return (row: R, cb: RowValidatorCallback<R>): void => {
30✔
37
                cb(null, { row, isValid: validateFunction(row) });
156✔
38
            };
39
        }
40
        return (row: R, cb: RowValidatorCallback<R>): void => {
48✔
41
            validateFunction(row, (err, isValid, reason): void => {
192✔
42
                if (err) {
180✔
43
                    return cb(err);
18✔
44
                }
45
                if (isValid) {
162✔
46
                    return cb(null, { row, isValid, reason });
126✔
47
                }
48
                return cb(null, { row, isValid: false, reason });
36✔
49
            });
50
        };
51
    }
52

53
    private _rowTransform: AsyncRowTransform<I, O> | null = null;
540✔
54

55
    private _rowValidator: RowValidator<O> | null = null;
540✔
56

57
    public set rowTransform(transformFunction: RowTransformFunction<I, O>) {
58
        if (!isFunction(transformFunction)) {
90✔
59
            throw new TypeError('The transform should be a function');
12✔
60
        }
61
        this._rowTransform = RowTransformerValidator.createTransform(transformFunction);
78✔
62
    }
63

64
    public set rowValidator(validateFunction: RowValidate<O>) {
65
        if (!isFunction(validateFunction)) {
90✔
66
            throw new TypeError('The validate should be a function');
12✔
67
        }
68
        this._rowValidator = RowTransformerValidator.createValidator(validateFunction);
78✔
69
    }
70

71
    public transformAndValidate(row: I, cb: RowValidatorCallback<O>): void {
72
        return this.callTransformer(row, (transformErr, transformedRow): void => {
728,292✔
73
            if (transformErr) {
728,280✔
74
                return cb(transformErr);
24✔
75
            }
76
            if (!transformedRow) {
728,256!
77
                return cb(null, { row: null, isValid: true });
×
78
            }
79
            return this.callValidator(transformedRow, (validateErr, validationResult): void => {
728,256✔
80
                if (validateErr) {
728,238✔
81
                    return cb(validateErr);
18✔
82
                }
83
                if (validationResult && !validationResult.isValid) {
728,220✔
84
                    return cb(null, { row: transformedRow, isValid: false, reason: validationResult.reason });
108✔
85
                }
86
                return cb(null, { row: transformedRow, isValid: true });
728,112✔
87
            });
88
        });
89
    }
90

91
    private callTransformer(row: I, cb: RowTransformCallback<O>): void {
92
        if (!this._rowTransform) {
728,292✔
93
            return cb(null, row as never as O);
727,932✔
94
        }
95
        return this._rowTransform(row, cb);
360✔
96
    }
97

98
    private callValidator(row: O, cb: RowValidatorCallback<O>): void {
99
        if (!this._rowValidator) {
728,256✔
100
            return cb(null, { row, isValid: true });
727,908✔
101
        }
102
        return this._rowValidator(row, cb);
348✔
103
    }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc