zhangnaisong
2023-08-05 24d66c8d82b628a06e93dbb1abfea2049b3d45ab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
'use strict';
 
const Assert = require('./assert');
const DeepEqual = require('./deepEqual');
const EscapeRegex = require('./escapeRegex');
const Utils = require('./utils');
 
 
const internals = {};
 
 
module.exports = function (ref, values, options = {}) {        // options: { deep, once, only, part, symbols }
 
    /*
        string -> string(s)
        array -> item(s)
        object -> key(s)
        object -> object (key:value)
    */
 
    if (typeof values !== 'object') {
        values = [values];
    }
 
    Assert(!Array.isArray(values) || values.length, 'Values array cannot be empty');
 
    // String
 
    if (typeof ref === 'string') {
        return internals.string(ref, values, options);
    }
 
    // Array
 
    if (Array.isArray(ref)) {
        return internals.array(ref, values, options);
    }
 
    // Object
 
    Assert(typeof ref === 'object', 'Reference must be string or an object');
    return internals.object(ref, values, options);
};
 
 
internals.array = function (ref, values, options) {
 
    if (!Array.isArray(values)) {
        values = [values];
    }
 
    if (!ref.length) {
        return false;
    }
 
    if (options.only &&
        options.once &&
        ref.length !== values.length) {
 
        return false;
    }
 
    let compare;
 
    // Map values
 
    const map = new Map();
    for (const value of values) {
        if (!options.deep ||
            !value ||
            typeof value !== 'object') {
 
            const existing = map.get(value);
            if (existing) {
                ++existing.allowed;
            }
            else {
                map.set(value, { allowed: 1, hits: 0 });
            }
        }
        else {
            compare = compare || internals.compare(options);
 
            let found = false;
            for (const [key, existing] of map.entries()) {
                if (compare(key, value)) {
                    ++existing.allowed;
                    found = true;
                    break;
                }
            }
 
            if (!found) {
                map.set(value, { allowed: 1, hits: 0 });
            }
        }
    }
 
    // Lookup values
 
    let hits = 0;
    for (const item of ref) {
        let match;
        if (!options.deep ||
            !item ||
            typeof item !== 'object') {
 
            match = map.get(item);
        }
        else {
            for (const [key, existing] of map.entries()) {
                if (compare(key, item)) {
                    match = existing;
                    break;
                }
            }
        }
 
        if (match) {
            ++match.hits;
            ++hits;
 
            if (options.once &&
                match.hits > match.allowed) {
 
                return false;
            }
        }
    }
 
    // Validate results
 
    if (options.only &&
        hits !== ref.length) {
 
        return false;
    }
 
    for (const match of map.values()) {
        if (match.hits === match.allowed) {
            continue;
        }
 
        if (match.hits < match.allowed &&
            !options.part) {
 
            return false;
        }
    }
 
    return !!hits;
};
 
 
internals.object = function (ref, values, options) {
 
    Assert(options.once === undefined, 'Cannot use option once with object');
 
    const keys = Utils.keys(ref, options);
    if (!keys.length) {
        return false;
    }
 
    // Keys list
 
    if (Array.isArray(values)) {
        return internals.array(keys, values, options);
    }
 
    // Key value pairs
 
    const symbols = Object.getOwnPropertySymbols(values).filter((sym) => values.propertyIsEnumerable(sym));
    const targets = [...Object.keys(values), ...symbols];
 
    const compare = internals.compare(options);
    const set = new Set(targets);
 
    for (const key of keys) {
        if (!set.has(key)) {
            if (options.only) {
                return false;
            }
 
            continue;
        }
 
        if (!compare(values[key], ref[key])) {
            return false;
        }
 
        set.delete(key);
    }
 
    if (set.size) {
        return options.part ? set.size < targets.length : false;
    }
 
    return true;
};
 
 
internals.string = function (ref, values, options) {
 
    // Empty string
 
    if (ref === '') {
        return values.length === 1 && values[0] === '' ||               // '' contains ''
            !options.once && !values.some((v) => v !== '');             // '' contains multiple '' if !once
    }
 
    // Map values
 
    const map = new Map();
    const patterns = [];
 
    for (const value of values) {
        Assert(typeof value === 'string', 'Cannot compare string reference to non-string value');
 
        if (value) {
            const existing = map.get(value);
            if (existing) {
                ++existing.allowed;
            }
            else {
                map.set(value, { allowed: 1, hits: 0 });
                patterns.push(EscapeRegex(value));
            }
        }
        else if (options.once ||
            options.only) {
 
            return false;
        }
    }
 
    if (!patterns.length) {                     // Non-empty string contains unlimited empty string
        return true;
    }
 
    // Match patterns
 
    const regex = new RegExp(`(${patterns.join('|')})`, 'g');
    const leftovers = ref.replace(regex, ($0, $1) => {
 
        ++map.get($1).hits;
        return '';                              // Remove from string
    });
 
    // Validate results
 
    if (options.only &&
        leftovers) {
 
        return false;
    }
 
    let any = false;
    for (const match of map.values()) {
        if (match.hits) {
            any = true;
        }
 
        if (match.hits === match.allowed) {
            continue;
        }
 
        if (match.hits < match.allowed &&
            !options.part) {
 
            return false;
        }
 
        // match.hits > match.allowed
 
        if (options.once) {
            return false;
        }
    }
 
    return !!any;
};
 
 
internals.compare = function (options) {
 
    if (!options.deep) {
        return internals.shallow;
    }
 
    const hasOnly = options.only !== undefined;
    const hasPart = options.part !== undefined;
 
    const flags = {
        prototype: hasOnly ? options.only : hasPart ? !options.part : false,
        part: hasOnly ? !options.only : hasPart ? options.part : false
    };
 
    return (a, b) => DeepEqual(a, b, flags);
};
 
 
internals.shallow = function (a, b) {
 
    return a === b;
};