05b383f9849d05f31f034747b05bc855bb297e832fa0071d337689d1b3a971a15558eb0fe828aebf30207040c59fd895c8fe248e970bde123303e671547d30 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Converts any value to string.
  3. *
  4. * @param {*} value
  5. * @returns {String}
  6. */
  7. export function stringify(value) {
  8. let result;
  9. switch (typeof value) {
  10. case 'string':
  11. case 'number':
  12. result = `${value}`;
  13. break;
  14. case 'object':
  15. result = value === null ? '' : value.toString();
  16. break;
  17. case 'undefined':
  18. result = '';
  19. break;
  20. default:
  21. result = value.toString();
  22. break;
  23. }
  24. return result;
  25. }
  26. /**
  27. * Checks if given variable is defined.
  28. *
  29. * @param {*} variable Variable to check.
  30. * @returns {Boolean}
  31. */
  32. export function isDefined(variable) {
  33. return typeof variable !== 'undefined';
  34. }
  35. /**
  36. * Checks if given variable is undefined.
  37. *
  38. * @param {*} variable Variable to check.
  39. * @returns {Boolean}
  40. */
  41. export function isUndefined(variable) {
  42. return typeof variable === 'undefined';
  43. }
  44. /**
  45. * Check if given variable is null, empty string or undefined.
  46. *
  47. * @param {*} variable Variable to check.
  48. * @returns {Boolean}
  49. */
  50. export function isEmpty(variable) {
  51. return variable === null || variable === '' || isUndefined(variable);
  52. }
  53. /**
  54. * Check if given variable is a regular expression.
  55. *
  56. * @param {*} variable Variable to check.
  57. * @returns {Boolean}
  58. */
  59. export function isRegExp(variable) {
  60. return Object.prototype.toString.call(variable) === '[object RegExp]';
  61. }