assert.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2018 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import {WorkboxError} from '../_private/WorkboxError.mjs';
  8. import '../_version.mjs';
  9. /*
  10. * This method returns true if the current context is a service worker.
  11. */
  12. const isSWEnv = (moduleName) => {
  13. if (!('ServiceWorkerGlobalScope' in self)) {
  14. throw new WorkboxError('not-in-sw', {moduleName});
  15. }
  16. };
  17. /*
  18. * This method throws if the supplied value is not an array.
  19. * The destructed values are required to produce a meaningful error for users.
  20. * The destructed and restructured object is so it's clear what is
  21. * needed.
  22. */
  23. const isArray = (value, {moduleName, className, funcName, paramName}) => {
  24. if (!Array.isArray(value)) {
  25. throw new WorkboxError('not-an-array', {
  26. moduleName,
  27. className,
  28. funcName,
  29. paramName,
  30. });
  31. }
  32. };
  33. const hasMethod = (object, expectedMethod,
  34. {moduleName, className, funcName, paramName}) => {
  35. const type = typeof object[expectedMethod];
  36. if (type !== 'function') {
  37. throw new WorkboxError('missing-a-method', {paramName, expectedMethod,
  38. moduleName, className, funcName});
  39. }
  40. };
  41. const isType = (object, expectedType,
  42. {moduleName, className, funcName, paramName}) => {
  43. if (typeof object !== expectedType) {
  44. throw new WorkboxError('incorrect-type', {paramName, expectedType,
  45. moduleName, className, funcName});
  46. }
  47. };
  48. const isInstance = (object, expectedClass,
  49. {moduleName, className, funcName,
  50. paramName, isReturnValueProblem}) => {
  51. if (!(object instanceof expectedClass)) {
  52. throw new WorkboxError('incorrect-class', {paramName, expectedClass,
  53. moduleName, className, funcName, isReturnValueProblem});
  54. }
  55. };
  56. const isOneOf = (value, validValues, {paramName}) => {
  57. if (!validValues.includes(value)) {
  58. throw new WorkboxError('invalid-value', {
  59. paramName,
  60. value,
  61. validValueDescription: `Valid values are ${JSON.stringify(validValues)}.`,
  62. });
  63. }
  64. };
  65. const isArrayOfClass = (value, expectedClass,
  66. {moduleName, className, funcName, paramName}) => {
  67. const error = new WorkboxError('not-array-of-class', {
  68. value, expectedClass,
  69. moduleName, className, funcName, paramName,
  70. });
  71. if (!Array.isArray(value)) {
  72. throw error;
  73. }
  74. for (let item of value) {
  75. if (!(item instanceof expectedClass)) {
  76. throw error;
  77. }
  78. }
  79. };
  80. const finalAssertExports = process.env.NODE_ENV === 'production' ? null : {
  81. hasMethod,
  82. isArray,
  83. isInstance,
  84. isOneOf,
  85. isSWEnv,
  86. isType,
  87. isArrayOfClass,
  88. };
  89. export {finalAssertExports as assert};