f8c72b5b3d97762cebea29d1b64d88f1b495012f2901971d3b26df7a436427843047d79a0233db9e309776b59806f6cffbfe2642b38db0166a2b771c6c7721-exec 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. const Hoek = require('@hapi/hoek');
  3. const ObjectType = require('../object');
  4. const Ref = require('../../ref');
  5. const internals = {};
  6. internals.Func = class extends ObjectType.constructor {
  7. constructor() {
  8. super();
  9. this._flags.func = true;
  10. }
  11. arity(n) {
  12. Hoek.assert(Number.isSafeInteger(n) && n >= 0, 'n must be a positive integer');
  13. return this._test('arity', n, function (value, state, options) {
  14. if (value.length === n) {
  15. return value;
  16. }
  17. return this.createError('function.arity', { n }, state, options);
  18. });
  19. }
  20. minArity(n) {
  21. Hoek.assert(Number.isSafeInteger(n) && n > 0, 'n must be a strict positive integer');
  22. return this._test('minArity', n, function (value, state, options) {
  23. if (value.length >= n) {
  24. return value;
  25. }
  26. return this.createError('function.minArity', { n }, state, options);
  27. });
  28. }
  29. maxArity(n) {
  30. Hoek.assert(Number.isSafeInteger(n) && n >= 0, 'n must be a positive integer');
  31. return this._test('maxArity', n, function (value, state, options) {
  32. if (value.length <= n) {
  33. return value;
  34. }
  35. return this.createError('function.maxArity', { n }, state, options);
  36. });
  37. }
  38. ref() {
  39. return this._test('ref', null, function (value, state, options) {
  40. if (Ref.isRef(value)) {
  41. return value;
  42. }
  43. return this.createError('function.ref', { value }, state, options);
  44. });
  45. }
  46. class() {
  47. return this._test('class', null, function (value, state, options) {
  48. if ((/^\s*class\s/).test(value.toString())) {
  49. return value;
  50. }
  51. return this.createError('function.class', { value }, state, options);
  52. });
  53. }
  54. };
  55. module.exports = new internals.Func();