‘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
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
'use strict';
 
const Hoek = require('@hapi/hoek');
 
const Any = require('../any');
const Ref = require('../../ref');
 
 
const internals = {
    precisionRx: /(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/,
    normalizeExponent(str) {
 
        return str
            .replace(/\.?0+e/, 'e')
            .replace(/e\+/, 'e')
            .replace(/^\+/, '')
            .replace(/^(-?)0+([1-9])/, '$1$2');
    },
    normalizeDecimal(str) {
 
        str = str
            .replace(/^\+/, '')
            .replace(/\.0+$/, '')
            .replace(/^(-?)0+([1-9])/, '$1$2');
 
        if (str.includes('.') && str.endsWith('0')) {
            str = str.replace(/0+$/, '');
        }
 
        return str;
    }
};
 
 
internals.Number = class extends Any {
 
    constructor() {
 
        super();
        this._type = 'number';
        this._flags.unsafe = false;
        this._invalids.add(Infinity);
        this._invalids.add(-Infinity);
    }
 
    _base(value, state, options) {
 
        const result = {
            errors: null,
            value
        };
 
        if (typeof value === 'string' &&
            options.convert) {
 
            const matches = value.match(/^\s*[+-]?\d+(?:\.\d+)?(?:e([+-]?\d+))?\s*$/i);
            if (matches) {
 
                value = value.trim();
                result.value = parseFloat(value);
 
                if (!this._flags.unsafe) {
                    if (value.includes('e')) {
                        if (internals.normalizeExponent(`${result.value / Math.pow(10, matches[1])}e${matches[1]}`) !== internals.normalizeExponent(value)) {
                            result.errors = this.createError('number.unsafe', { value }, state, options);
                            return result;
                        }
                    }
                    else {
                        if (result.value.toString() !== internals.normalizeDecimal(value)) {
                            result.errors = this.createError('number.unsafe', { value }, state, options);
                            return result;
                        }
                    }
                }
            }
        }
 
        const isNumber = typeof result.value === 'number' && !isNaN(result.value);
 
        if (options.convert && 'precision' in this._flags && isNumber) {
 
            // This is conceptually equivalent to using toFixed but it should be much faster
            const precision = Math.pow(10, this._flags.precision);
            result.value = Math.round(result.value * precision) / precision;
        }
 
        if (isNumber) {
            if (!this._flags.unsafe &&
                (value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER)) {
                result.errors = this.createError('number.unsafe', { value }, state, options);
            }
        }
        else {
            result.errors = this.createError('number.base', { value }, state, options);
        }
 
        return result;
    }
 
    multiple(base) {
 
        const isRef = Ref.isRef(base);
 
        if (!isRef) {
            Hoek.assert(typeof base === 'number' && isFinite(base), 'multiple must be a number');
            Hoek.assert(base > 0, 'multiple must be greater than 0');
        }
 
        return this._test('multiple', base, function (value, state, options) {
 
            const divisor = isRef ? base(state.reference || state.parent, options) : base;
 
            if (isRef && (typeof divisor !== 'number' || !isFinite(divisor))) {
                return this.createError('number.ref', { ref: base.key }, state, options);
            }
 
            if (value % divisor === 0) {
                return value;
            }
 
            return this.createError('number.multiple', { multiple: base, value }, state, options);
        });
    }
 
    integer() {
 
        return this._test('integer', undefined, function (value, state, options) {
 
            return Math.trunc(value) - value === 0 ? value : this.createError('number.integer', { value }, state, options);
        });
    }
 
    unsafe(enabled = true) {
 
        Hoek.assert(typeof enabled === 'boolean', 'enabled must be a boolean');
 
        if (this._flags.unsafe === enabled) {
            return this;
        }
 
        const obj = this.clone();
        obj._flags.unsafe = enabled;
        return obj;
    }
 
    negative() {
 
        return this._test('negative', undefined, function (value, state, options) {
 
            if (value < 0) {
                return value;
            }
 
            return this.createError('number.negative', { value }, state, options);
        });
    }
 
    positive() {
 
        return this._test('positive', undefined, function (value, state, options) {
 
            if (value > 0) {
                return value;
            }
 
            return this.createError('number.positive', { value }, state, options);
        });
    }
 
    precision(limit) {
 
        Hoek.assert(Number.isSafeInteger(limit), 'limit must be an integer');
        Hoek.assert(!('precision' in this._flags), 'precision already set');
 
        const obj = this._test('precision', limit, function (value, state, options) {
 
            const places = value.toString().match(internals.precisionRx);
            const decimals = Math.max((places[1] ? places[1].length : 0) - (places[2] ? parseInt(places[2], 10) : 0), 0);
            if (decimals <= limit) {
                return value;
            }
 
            return this.createError('number.precision', { limit, value }, state, options);
        });
 
        obj._flags.precision = limit;
        return obj;
    }
 
    port() {
 
        return this._test('port', undefined, function (value, state, options) {
 
            if (!Number.isSafeInteger(value) || value < 0 || value > 65535) {
                return this.createError('number.port', { value }, state, options);
            }
 
            return value;
        });
    }
 
};
 
 
internals.compare = function (type, compare) {
 
    return function (limit) {
 
        const isRef = Ref.isRef(limit);
        const isNumber = typeof limit === 'number' && !isNaN(limit);
 
        Hoek.assert(isNumber || isRef, 'limit must be a number or reference');
 
        return this._test(type, limit, function (value, state, options) {
 
            let compareTo;
            if (isRef) {
                compareTo = limit(state.reference || state.parent, options);
 
                if (!(typeof compareTo === 'number' && !isNaN(compareTo))) {
                    return this.createError('number.ref', { ref: limit.key }, state, options);
                }
            }
            else {
                compareTo = limit;
            }
 
            if (compare(value, compareTo)) {
                return value;
            }
 
            return this.createError('number.' + type, { limit: compareTo, value }, state, options);
        });
    };
};
 
 
internals.Number.prototype.min = internals.compare('min', (value, limit) => value >= limit);
internals.Number.prototype.max = internals.compare('max', (value, limit) => value <= limit);
internals.Number.prototype.greater = internals.compare('greater', (value, limit) => value > limit);
internals.Number.prototype.less = internals.compare('less', (value, limit) => value < limit);
 
 
module.exports = new internals.Number();