UNPKG

5.27 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.MapPredicate = void 0;
4const isEqual = require("lodash.isequal");
5const has_items_1 = require("../utils/has-items");
6const of_type_1 = require("../utils/of-type");
7const predicate_1 = require("./predicate");
8class MapPredicate extends predicate_1.Predicate {
9 /**
10 @hidden
11 */
12 constructor(options) {
13 super('Map', options);
14 }
15 /**
16 Test a Map to have a specific size.
17
18 @param size - The size of the Map.
19 */
20 size(size) {
21 return this.addValidator({
22 message: (map, label) => `Expected ${label} to have size \`${size}\`, got \`${map.size}\``,
23 validator: map => map.size === size
24 });
25 }
26 /**
27 Test an Map to have a minimum size.
28
29 @param size - The minimum size of the Map.
30 */
31 minSize(size) {
32 return this.addValidator({
33 message: (map, label) => `Expected ${label} to have a minimum size of \`${size}\`, got \`${map.size}\``,
34 validator: map => map.size >= size,
35 negatedMessage: (map, label) => `Expected ${label} to have a maximum size of \`${size - 1}\`, got \`${map.size}\``
36 });
37 }
38 /**
39 Test an Map to have a maximum size.
40
41 @param size - The maximum size of the Map.
42 */
43 maxSize(size) {
44 return this.addValidator({
45 message: (map, label) => `Expected ${label} to have a maximum size of \`${size}\`, got \`${map.size}\``,
46 validator: map => map.size <= size,
47 negatedMessage: (map, label) => `Expected ${label} to have a minimum size of \`${size + 1}\`, got \`${map.size}\``
48 });
49 }
50 /**
51 Test a Map to include all the provided keys. The keys are tested by identity, not structure.
52
53 @param keys - The keys that should be a key in the Map.
54 */
55 hasKeys(...keys) {
56 return this.addValidator({
57 message: (_, label, missingKeys) => `Expected ${label} to have keys \`${JSON.stringify(missingKeys)}\``,
58 validator: map => has_items_1.default(map, keys)
59 });
60 }
61 /**
62 Test a Map to include any of the provided keys. The keys are tested by identity, not structure.
63
64 @param keys - The keys that could be a key in the Map.
65 */
66 hasAnyKeys(...keys) {
67 return this.addValidator({
68 message: (_, label) => `Expected ${label} to have any key of \`${JSON.stringify(keys)}\``,
69 validator: map => keys.some(key => map.has(key))
70 });
71 }
72 /**
73 Test a Map to include all the provided values. The values are tested by identity, not structure.
74
75 @param values - The values that should be a value in the Map.
76 */
77 hasValues(...values) {
78 return this.addValidator({
79 message: (_, label, missingValues) => `Expected ${label} to have values \`${JSON.stringify(missingValues)}\``,
80 validator: map => has_items_1.default(new Set(map.values()), values)
81 });
82 }
83 /**
84 Test a Map to include any of the provided values. The values are tested by identity, not structure.
85
86 @param values - The values that could be a value in the Map.
87 */
88 hasAnyValues(...values) {
89 return this.addValidator({
90 message: (_, label) => `Expected ${label} to have any value of \`${JSON.stringify(values)}\``,
91 validator: map => {
92 const valueSet = new Set(map.values());
93 return values.some(key => valueSet.has(key));
94 }
95 });
96 }
97 /**
98 Test all the keys in the Map to match the provided predicate.
99
100 @param predicate - The predicate that should be applied against every key in the Map.
101 */
102 keysOfType(predicate) {
103 return this.addValidator({
104 message: (_, label, error) => `(${label}) ${error}`,
105 validator: map => of_type_1.default(map.keys(), predicate)
106 });
107 }
108 /**
109 Test all the values in the Map to match the provided predicate.
110
111 @param predicate - The predicate that should be applied against every value in the Map.
112 */
113 valuesOfType(predicate) {
114 return this.addValidator({
115 message: (_, label, error) => `(${label}) ${error}`,
116 validator: map => of_type_1.default(map.values(), predicate)
117 });
118 }
119 /**
120 Test a Map to be empty.
121 */
122 get empty() {
123 return this.addValidator({
124 message: (map, label) => `Expected ${label} to be empty, got \`${JSON.stringify([...map])}\``,
125 validator: map => map.size === 0
126 });
127 }
128 /**
129 Test a Map to be not empty.
130 */
131 get nonEmpty() {
132 return this.addValidator({
133 message: (_, label) => `Expected ${label} to not be empty`,
134 validator: map => map.size > 0
135 });
136 }
137 /**
138 Test a Map to be deeply equal to the provided Map.
139
140 @param expected - Expected Map to match.
141 */
142 deepEqual(expected) {
143 return this.addValidator({
144 message: (map, label) => `Expected ${label} to be deeply equal to \`${JSON.stringify([...expected])}\`, got \`${JSON.stringify([...map])}\``,
145 validator: map => isEqual(map, expected)
146 });
147 }
148}
149exports.MapPredicate = MapPredicate;