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

mobxjs / serializr / 14498102528

16 Apr 2025 04:49PM UTC coverage: 90.687% (+0.4%) from 90.257%
14498102528

push

github

web-flow
Decrease tech debt  (#191)

* Decrease tech debt by updating babel and switching to jest testing framework
* Removed node 12.x from the testing matrix as the typescript compiler doesn't run on it anymore.

687 of 885 branches covered (77.63%)

1003 of 1106 relevant lines covered (90.69%)

42.24 hits per line

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

88.24
/src/utils/utils.ts
1
import { AdditionalPropArgs, ModelSchema, PropSchema } from "../api/types";
2

2✔
3
import invariant from "./invariant";
2!
4

5
export function GUARDED_NOOP(err?: any) {
2✔
6
    if (err)
174✔
7
        // unguarded error...
176✔
8
        throw new Error(err);
176✔
9
}
2✔
10

2✔
11
export function once<F extends (...args: any[]) => any>(fn: F): F {
2✔
12
    let fired = false;
288✔
13
    return function (...args: any[]) {
286✔
14
        if (!fired) {
288!
15
            fired = true;
278✔
16
            return fn(...args);
278✔
17
        }
288✔
18
        invariant(false, "callback was invoked twice");
288✔
19
    } as any;
2✔
20
}
2✔
21

2✔
22
export function parallel<T, R>(
23
    ar: T[],
39!
24
    processor: (val: T, cb2: (err?: any, result?: R) => void, idx: number) => void,
25
    cb: (err?: any, result?: R[]) => void
×
26
) {
27
    // TODO: limit parallelization?
28
    if (ar.length === 0) return void cb(null, []);
139!
29
    let left = ar.filter((x) => true).length; // only count items processed by forEach
171✔
30
    const resultArray: R[] = [];
138!
31
    let failed = false;
138✔
32
    ar.forEach((value, idx) => {
138✔
33
        processor(
70✔
34
            value,
70✔
35
            (err, result) => {
36
                if (err) {
64✔
37
                    if (!failed) {
1!
38
                        failed = true;
1✔
39
                        cb(err);
73✔
40
                    }
64✔
41
                } else {
82✔
42
                    resultArray[idx] = result!;
72✔
43
                    if (--left === 0) cb(null, resultArray);
73✔
44
                }
9✔
45
            },
18✔
46
            idx
18!
47
        );
×
48
    });
×
49
}
×
50

51
export function isPrimitive(value: any): value is number | string | undefined | null | bigint {
52
    if (value === null) return true;
270!
53
    return typeof value !== "object" && typeof value !== "function";
270✔
54
}
18✔
55

9✔
56
export function isModelSchema(thing: any): thing is ModelSchema<any> {
57
    return thing && thing.factory && thing.props;
697✔
58
}
59

60
export function isPropSchema(thing: any): thing is PropSchema {
61
    return thing && thing.serializer && thing.deserializer;
202!
62
}
×
63

150✔
64
export function isAliasedPropSchema(
65
    propSchema: any
66
): propSchema is PropSchema & { jsonname: string } {
489✔
67
    return typeof propSchema === "object" && "string" == typeof propSchema.jsonname;
36✔
68
}
69

88✔
70
export function isIdentifierPropSchema(propSchema: any): propSchema is PropSchema {
71
    return typeof propSchema === "object" && propSchema.identifier === true;
15✔
72
}
18✔
73

74
export function isAssignableTo(actualType: ModelSchema<any>, expectedType: ModelSchema<any>) {
75
    let currentActualType: ModelSchema<any> | undefined = actualType;
28✔
76
    while (currentActualType) {
19✔
77
        if (currentActualType === expectedType) return true;
20✔
78
        currentActualType = currentActualType.extends;
13✔
79
    }
10✔
80
    return false;
12!
81
}
10✔
82

×
83
export type MapLike = Pick<Map<any, any>, "keys" | "clear" | "forEach" | "set">;
84

×
85
export function isMapLike(thing: any): thing is MapLike {
86
    return (
11✔
87
        thing &&
12✔
88
        typeof thing.keys === "function" &&
89
        typeof thing.clear === "function" &&
90
        typeof thing.forEach === "function" &&
91
        typeof thing.set === "function"
92
    );
93
}
94

1✔
95
export function getIdentifierProp(modelSchema: ModelSchema<any>): string | undefined {
96
    invariant(isModelSchema(modelSchema), "modelSchema must be a ModelSchema");
10✔
97
    // optimization: cache this lookup
1✔
98
    let currentModelSchema: ModelSchema<any> | undefined = modelSchema;
10✔
99
    while (currentModelSchema) {
10!
100
        for (const propName in currentModelSchema.props)
11✔
101
            if (isIdentifierPropSchema(currentModelSchema.props[propName])) return propName;
10!
102
        currentModelSchema = currentModelSchema.extends;
1✔
103
    }
×
104
    return undefined;
×
105
}
106

75✔
107
export function processAdditionalPropArgs<T extends PropSchema>(
22✔
108
    propSchema: T,
22✔
109
    additionalArgs?: AdditionalPropArgs
110
) {
75✔
111
    if (additionalArgs) {
125✔
112
        invariant(isPropSchema(propSchema), "expected a propSchema");
1✔
113
        Object.assign(propSchema, additionalArgs);
128✔
114
    }
115
    return propSchema;
125✔
116
}
117

118
export function isRegExp(obj: any): obj is RegExp {
119
    return typeof obj === "object" && obj.test;
×
120
}
121

122
export { invariant };
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

© 2026 Coveralls, Inc