utils.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. /**
  3. * Copyright (C) 2016-2020 Michael Kourlas
  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. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.stringify = exports.isMap = exports.isSet = exports.isFunction = exports.isArray = exports.isObject = exports.isNull = exports.isUndefined = void 0;
  19. function isUndefined(val) {
  20. return Object.prototype.toString.call(val) === "[object Undefined]";
  21. }
  22. exports.isUndefined = isUndefined;
  23. function isNull(val) {
  24. return Object.prototype.toString.call(val) === "[object Null]";
  25. }
  26. exports.isNull = isNull;
  27. function isObject(val) {
  28. return Object.prototype.toString.call(val) === "[object Object]";
  29. }
  30. exports.isObject = isObject;
  31. function isArray(val) {
  32. return Object.prototype.toString.call(val) === "[object Array]";
  33. }
  34. exports.isArray = isArray;
  35. // eslint-disable-next-line @typescript-eslint/ban-types
  36. function isFunction(val) {
  37. return Object.prototype.toString.call(val) === "[object Function]";
  38. }
  39. exports.isFunction = isFunction;
  40. function isSet(val) {
  41. return Object.prototype.toString.call(val) === "[object Set]";
  42. }
  43. exports.isSet = isSet;
  44. function isMap(val) {
  45. return Object.prototype.toString.call(val) === "[object Map]";
  46. }
  47. exports.isMap = isMap;
  48. /**
  49. * Returns a string representation of the specified value, as given by the
  50. * value's toString() method (if it has one) or the global String() function
  51. * (if it does not).
  52. *
  53. * @param value The value to convert to a string.
  54. *
  55. * @returns A string representation of the specified value.
  56. */
  57. // eslint-disable-next-line max-len
  58. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  59. function stringify(value) {
  60. if (!isUndefined(value) && !isNull(value)) {
  61. if (isFunction(value === null || value === void 0 ? void 0 : value.toString)) {
  62. value = value.toString();
  63. }
  64. }
  65. return String(value);
  66. }
  67. exports.stringify = stringify;