util.js 850 B

123456789101112131415161718192021222324252627
  1. import { EMPTY_ARR } from "./constants";
  2. /**
  3. * Assign properties from `props` to `obj`
  4. * @template O, P The obj and props types
  5. * @param {O} obj The object to copy properties to
  6. * @param {P} props The object to copy properties from
  7. * @returns {O & P}
  8. */
  9. export function assign(obj, props) {
  10. // @ts-ignore We change the type of `obj` to be `O & P`
  11. for (let i in props) obj[i] = props[i];
  12. return /** @type {O & P} */ (obj);
  13. }
  14. /**
  15. * Remove a child node from its parent if attached. This is a workaround for
  16. * IE11 which doesn't support `Element.prototype.remove()`. Using this function
  17. * is smaller than including a dedicated polyfill.
  18. * @param {Node} node The node to remove
  19. */
  20. export function removeNode(node) {
  21. let parentNode = node.parentNode;
  22. if (parentNode) parentNode.removeChild(node);
  23. }
  24. export const slice = EMPTY_ARR.slice;