‘liusuyi’
2023-08-09 161b9318e345c8a0c9cdc133b33a1c759495f323
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
'use strict';
 
const Util = require('util');
 
const Hoek = require('@hapi/hoek');
 
const Any = require('../any');
 
 
const internals = {};
 
 
internals.Map = class extends Map {
 
    slice() {
 
        return new internals.Map(this);
    }
 
    toString() {
 
        return Util.inspect(this);
    }
};
 
 
internals.Symbol = class extends Any {
 
    constructor() {
 
        super();
        this._type = 'symbol';
        this._inner.map = new internals.Map();
    }
 
    _base(value, state, options) {
 
        if (options.convert) {
            const lookup = this._inner.map.get(value);
            if (lookup) {
                value = lookup;
            }
 
            if (this._flags.allowOnly) {
                return {
                    value,
                    errors: (typeof value === 'symbol') ? null : this.createError('symbol.map', { value, map: this._inner.map }, state, options)
                };
            }
        }
 
        return {
            value,
            errors: (typeof value === 'symbol') ? null : this.createError('symbol.base', { value }, state, options)
        };
    }
 
    map(iterable) {
 
        if (iterable && !iterable[Symbol.iterator] && typeof iterable === 'object') {
            iterable = Object.entries(iterable);
        }
 
        Hoek.assert(iterable && iterable[Symbol.iterator], 'Iterable must be an iterable or object');
        const obj = this.clone();
 
        const symbols = [];
        for (const entry of iterable) {
            Hoek.assert(entry && entry[Symbol.iterator], 'Entry must be an iterable');
            const [key, value] = entry;
 
            Hoek.assert(typeof key !== 'object' && typeof key !== 'function' && typeof key !== 'symbol', 'Key must not be an object, function, or Symbol');
            Hoek.assert(typeof value === 'symbol', 'Value must be a Symbol');
            obj._inner.map.set(key, value);
            symbols.push(value);
        }
 
        return obj.valid(...symbols);
    }
 
    describe() {
 
        const description = super.describe();
        description.map = new Map(this._inner.map);
        return description;
    }
};
 
 
module.exports = new internals.Symbol();