Dashboard sipadu mbip
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.

utils.js.flow 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // @flow
  2. import { type Ref } from "./RefTypes";
  3. /**
  4. * Takes an argument and if it's an array, returns the first item in the array,
  5. * otherwise returns the argument. Used for Preact compatibility.
  6. */
  7. export const unwrapArray = (arg: *): * => (Array.isArray(arg) ? arg[0] : arg);
  8. /**
  9. * Takes a maybe-undefined function and arbitrary args and invokes the function
  10. * only if it is defined.
  11. */
  12. export const safeInvoke = (fn: ?Function, ...args: *) => {
  13. if (typeof fn === "function") {
  14. return fn(...args);
  15. }
  16. }
  17. /**
  18. * Does a shallow equality check of two objects by comparing the reference
  19. * equality of each value.
  20. */
  21. export const shallowEqual = (objA: { [key: string]: any}, objB: { [key: string]: any}): boolean => {
  22. var aKeys = Object.keys(objA);
  23. var bKeys = Object.keys(objB);
  24. if (bKeys.length !== aKeys.length) {
  25. return false;
  26. }
  27. for (var i = 0; i < bKeys.length; i++) {
  28. var key = aKeys[i];
  29. if (objA[key] !== objB[key]) {
  30. return false;
  31. }
  32. }
  33. return true;
  34. }
  35. /**
  36. * Sets a ref using either a ref callback or a ref object
  37. */
  38. export const setRef = (ref: ?Ref, node: ?HTMLElement) => {
  39. // if its a function call it
  40. if (typeof ref === "function") {
  41. return safeInvoke(ref, node);
  42. }
  43. // otherwise we should treat it as a ref object
  44. else if (ref != null) {
  45. ref.current = node;
  46. }
  47. }