_nullishCoalesce.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. // https://github.com/alangpierce/sucrase/tree/265887868966917f3b924ce38dfad01fbab1329f
  3. //
  4. // The MIT License (MIT)
  5. //
  6. // Copyright (c) 2012-2018 various contributors (see AUTHORS)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. /**
  26. * Polyfill for the nullish coalescing operator (`??`).
  27. *
  28. * Note that the RHS is wrapped in a function so that if it's a computed value, that evaluation won't happen unless the
  29. * LHS evaluates to a nullish value, to mimic the operator's short-circuiting behavior.
  30. *
  31. * Adapted from Sucrase (https://github.com/alangpierce/sucrase)
  32. *
  33. * @param lhs The value of the expression to the left of the `??`
  34. * @param rhsFn A function returning the value of the expression to the right of the `??`
  35. * @returns The LHS value, unless it's `null` or `undefined`, in which case, the RHS value
  36. */
  37. function _nullishCoalesce(lhs, rhsFn) {
  38. // by checking for loose equality to `null`, we catch both `null` and `undefined`
  39. return lhs != null ? lhs : rhsFn();
  40. }
  41. // Sucrase version:
  42. // function _nullishCoalesce(lhs, rhsFn) {
  43. // if (lhs != null) {
  44. // return lhs;
  45. // } else {
  46. // return rhsFn();
  47. // }
  48. // }
  49. exports._nullishCoalesce = _nullishCoalesce;
  50. //# sourceMappingURL=_nullishCoalesce.js.map