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
'use strict';
 
const Hoek = require('@hapi/hoek');
 
const ObjectType = require('../object');
const Ref = require('../../ref');
 
 
const internals = {};
 
 
internals.Func = class extends ObjectType.constructor {
 
    constructor() {
 
        super();
        this._flags.func = true;
    }
 
    arity(n) {
 
        Hoek.assert(Number.isSafeInteger(n) && n >= 0, 'n must be a positive integer');
 
        return this._test('arity', n, function (value, state, options) {
 
            if (value.length === n) {
                return value;
            }
 
            return this.createError('function.arity', { n }, state, options);
        });
    }
 
    minArity(n) {
 
        Hoek.assert(Number.isSafeInteger(n) && n > 0, 'n must be a strict positive integer');
 
        return this._test('minArity', n, function (value, state, options) {
 
            if (value.length >= n) {
                return value;
            }
 
            return this.createError('function.minArity', { n }, state, options);
        });
    }
 
    maxArity(n) {
 
        Hoek.assert(Number.isSafeInteger(n) && n >= 0, 'n must be a positive integer');
 
        return this._test('maxArity', n, function (value, state, options) {
 
            if (value.length <= n) {
                return value;
            }
 
            return this.createError('function.maxArity', { n }, state, options);
        });
    }
 
    ref() {
 
        return this._test('ref', null, function (value, state, options) {
 
            if (Ref.isRef(value)) {
                return value;
            }
 
            return this.createError('function.ref', { value }, state, options);
        });
    }
 
    class() {
 
        return this._test('class', null, function (value, state, options) {
 
            if ((/^\s*class\s/).test(value.toString())) {
                return value;
            }
 
            return this.createError('function.class', { value }, state, options);
        });
    }
};
 
module.exports = new internals.Func();