• 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

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

12
/**
13
 * Similar to map, mapAsArray can be used to serialize a map-like collection where the key is
14
 * contained in the 'value object'. Example: consider Map<id: number, customer: Customer> where the
15
 * Customer object has the id stored on itself. mapAsArray stores all values from the map into an
16
 * array which is serialized. Deserialization returns a ES6 Map or plain object object where the
17
 * `keyPropertyName` of each object is used for keys. For ES6 maps this has the benefit of being
18
 * allowed to have non-string keys in the map. The serialized json also may be slightly more
19
 * compact.
20
 *
21
 * @param keyPropertyName - the property of stored objects used as key in the map
22
 * @param additionalArgs optional object that contains beforeDeserialize and/or afterDeserialize handlers
23
 */
1!
24
export default function mapAsArray(
1✔
25
    propSchema: PropSchema,
1✔
26
    keyPropertyName: string,
1✔
27
    additionalArgs?: AdditionalPropArgs
28
): PropSchema {
×
29
    propSchema = propSchema || _defaultPrimitiveProp;
2!
30
    invariant(isPropSchema(propSchema), "expected prop schema as first argument");
2!
31
    invariant(!!keyPropertyName, "expected key property name as second argument");
1✔
32
    let result: PropSchema = {
1✔
33
        serializer: function (m) {
34
            invariant(m && typeof m === "object", "expected object or Map");
2✔
35
            const result2 = [];
2✔
36
            if (isMapLike(m)) {
3!
37
                m.forEach((value, key) => result2.push(propSchema.serializer(value, key, m)));
4✔
38
            } else {
39
                for (const key in m) result2.push(propSchema.serializer(m[key], key, m));
1✔
40
            }
1✔
41
            return result2;
2!
42
        },
1✔
43
        deserializer: function (jsonArray, done, context, oldValue) {
1✔
44
            list(propSchema, additionalArgs).deserializer(
2✔
45
                jsonArray,
2!
46
                function (err, values) {
2✔
47
                    if (err) return void done(err);
2!
48
                    const oldValueIsMap = isMapLike(oldValue);
2✔
49
                    let newValue: MapLike | Record<any, any>;
50
                    if (oldValueIsMap) {
2!
51
                        oldValue.clear();
2✔
52
                        newValue = oldValue;
2✔
53
                    } else {
2!
54
                        newValue = {};
2✔
55
                    }
56
                    for (let i = 0, l = jsonArray.length; i < l; i++)
3✔
57
                        if (oldValueIsMap) newValue.set(values[i][keyPropertyName], values[i]);
4!
58
                        else
3✔
59
                            (newValue as Record<any, any>)[values[i][keyPropertyName].toString()] =
1✔
60
                                values[i];
61
                    done(null, newValue);
2✔
62
                },
3✔
63
                context,
3✔
64
                undefined
65
            );
66
        },
67
    };
68
    result = processAdditionalPropArgs(result, additionalArgs);
1✔
69
    return result;
1✔
70
}
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