value-impl.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*! firebase-admin v12.1.1 */
  2. /*!
  3. * Copyright 2024 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. 'use strict';
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.ValueImpl = void 0;
  20. /**
  21. * Implements type-safe getters for parameter values.
  22. *
  23. * Visible for testing.
  24. *
  25. * @internal
  26. */
  27. class ValueImpl {
  28. constructor(source, value = ValueImpl.DEFAULT_VALUE_FOR_STRING) {
  29. this.source = source;
  30. this.value = value;
  31. }
  32. asString() {
  33. return this.value;
  34. }
  35. asBoolean() {
  36. if (this.source === 'static') {
  37. return ValueImpl.DEFAULT_VALUE_FOR_BOOLEAN;
  38. }
  39. return ValueImpl.BOOLEAN_TRUTHY_VALUES.indexOf(this.value.toLowerCase()) >= 0;
  40. }
  41. asNumber() {
  42. if (this.source === 'static') {
  43. return ValueImpl.DEFAULT_VALUE_FOR_NUMBER;
  44. }
  45. const num = Number(this.value);
  46. if (isNaN(num)) {
  47. return ValueImpl.DEFAULT_VALUE_FOR_NUMBER;
  48. }
  49. return num;
  50. }
  51. getSource() {
  52. return this.source;
  53. }
  54. }
  55. exports.ValueImpl = ValueImpl;
  56. ValueImpl.DEFAULT_VALUE_FOR_BOOLEAN = false;
  57. ValueImpl.DEFAULT_VALUE_FOR_STRING = '';
  58. ValueImpl.DEFAULT_VALUE_FOR_NUMBER = 0;
  59. ValueImpl.BOOLEAN_TRUTHY_VALUES = ['1', 'true', 't', 'yes', 'y', 'on'];