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

mobxjs / serializr / 14486570384

16 Apr 2025 06:53AM UTC coverage: 90.687% (+0.4%) from 90.257%
14486570384

Pull #191

github

web-flow
Merge 073817e77 into a2448d022
Pull Request #191: Decrease tech debt

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

96.0
/src/types/map.ts
1
import {
2
    invariant,
2✔
3
    isAliasedPropSchema,
2!
4
    isPropSchema,
5
    isMapLike,
2✔
6
    processAdditionalPropArgs,
2✔
7
    MapLike,
2✔
8
} from "../utils/utils";
2✔
9
import { _defaultPrimitiveProp } from "../constants";
2✔
10
import list from "./list";
11
import { PropSchema, AdditionalPropArgs } from "../api/types";
12

13
/**
14
 * Similar to list, but map represents a string keyed dynamic collection.
15
 * This can be both plain objects (default) or ES6 Map like structures.
16
 * This will be inferred from the initial value of the targetted attribute.
17
 *
18
 * For `Map`s which are not string-keyed, check out `mapAsArray`.
19
 *
20
 * @param additionalArgs optional object that contains beforeDeserialize and/or afterDeserialize handlers
2!
21
 */
2✔
22
export default function map(
2✔
23
    propSchema: PropSchema,
2✔
24
    additionalArgs?: AdditionalPropArgs
25
): PropSchema {
×
26
    propSchema = propSchema || _defaultPrimitiveProp;
4✔
27
    invariant(isPropSchema(propSchema), "expected prop schema as first argument");
4!
28
    invariant(
2✔
29
        !isAliasedPropSchema(propSchema),
30
        "provided prop is aliased, please put aliases first"
31
    );
×
32
    let result: PropSchema = {
2✔
33
        serializer: function (m: Map<any, any> | { [key: string]: any }) {
34
            invariant(m && typeof m === "object", "expected object or Map");
5✔
35
            const result2: { [key: string]: any } = {};
5✔
36
            if (isMapLike(m)) {
5✔
37
                m.forEach((value, key) => (result2[key] = propSchema.serializer(value, key, m)));
5✔
38
            } else {
3✔
39
                for (const key in m) result2[key] = propSchema.serializer(m[key], key, m);
4✔
40
            }
4✔
41
            return result2;
4!
42
        },
2✔
43
        deserializer: function (jsonObject, done, context, oldValue: MapLike | Record<any, any>) {
3✔
44
            if (!jsonObject || typeof jsonObject !== "object")
5!
45
                return void done("[serializr] expected JSON object");
3!
46
            const keys = Object.keys(jsonObject);
5✔
47
            list(propSchema, additionalArgs).deserializer(
4✔
48
                keys.map((key) => jsonObject[key]),
9✔
49
                function (err, values) {
4✔
50
                    if (err) return void done(err);
5!
51
                    const isMap = isMapLike(oldValue);
5✔
52
                    let newValue: MapLike | Record<any, any>;
53
                    if (isMap) {
5✔
54
                        // if the oldValue is a map, we recycle it
5✔
55
                        // there are many variations and this way we don't have to
6✔
56
                        // know about the original constructor
4✔
57
                        oldValue.clear();
4✔
58
                        newValue = oldValue;
6✔
59
                    } else newValue = {};
3✔
60

5✔
61
                    for (let i = 0, l = keys.length; i < l; i++) {
6✔
62
                        if (isMap) newValue.set(keys[i], values[i]);
8✔
63
                        else (newValue as Record<any, any>)[keys[i]] = values[i];
4✔
64
                    }
6✔
65
                    done(null, newValue);
6✔
66
                },
67
                context
68
            );
69
        },
70
    };
71
    result = processAdditionalPropArgs(result, additionalArgs);
2✔
72
    return result;
2✔
73
}
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