You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.7 KiB

4 years ago
  1. /**
  2. * Quick type checking
  3. * @param {*} element - anything
  4. * @param {string} type - type definition
  5. * @returns {boolean} true if the type corresponds
  6. */
  7. export function checkType(element, type) {
  8. return typeof element === type
  9. }
  10. /**
  11. * Check if an element is part of an svg
  12. * @param {HTMLElement} el - element to check
  13. * @returns {boolean} true if we are in an svg context
  14. */
  15. export function isSvg(el) {
  16. const owner = el.ownerSVGElement
  17. return !!owner || owner === null
  18. }
  19. /**
  20. * Check if an element is a template tag
  21. * @param {HTMLElement} el - element to check
  22. * @returns {boolean} true if it's a <template>
  23. */
  24. export function isTemplate(el) {
  25. return !isNil(el.content)
  26. }
  27. /**
  28. * Check that will be passed if its argument is a function
  29. * @param {*} value - value to check
  30. * @returns {boolean} - true if the value is a function
  31. */
  32. export function isFunction(value) {
  33. return checkType(value, 'function')
  34. }
  35. /**
  36. * Check if a value is a Boolean
  37. * @param {*} value - anything
  38. * @returns {boolean} true only for the value is a boolean
  39. */
  40. export function isBoolean(value) {
  41. return checkType(value, 'boolean')
  42. }
  43. /**
  44. * Check if a value is an Object
  45. * @param {*} value - anything
  46. * @returns {boolean} true only for the value is an object
  47. */
  48. export function isObject(value) {
  49. return !isNil(value) && checkType(value, 'object')
  50. }
  51. /**
  52. * Check if a value is null or undefined
  53. * @param {*} value - anything
  54. * @returns {boolean} true only for the 'undefined' and 'null' types
  55. */
  56. export function isNil(value) {
  57. return value === null || value === undefined
  58. }
  59. /**
  60. * Detect node js environements
  61. * @returns {boolean} true if the runtime is node
  62. */
  63. export function isNode() {
  64. return typeof process !== 'undefined'
  65. }