Front end of the Slack clone application.

state.js 941B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // API
  2. module.exports = state;
  3. /**
  4. * Creates initial state object
  5. * for iteration over list
  6. *
  7. * @param {array|object} list - list to iterate over
  8. * @param {function|null} sortMethod - function to use for keys sort,
  9. * or `null` to keep them as is
  10. * @returns {object} - initial state object
  11. */
  12. function state(list, sortMethod)
  13. {
  14. var isNamedList = !Array.isArray(list)
  15. , initState =
  16. {
  17. index : 0,
  18. keyedList: isNamedList || sortMethod ? Object.keys(list) : null,
  19. jobs : {},
  20. results : isNamedList ? {} : [],
  21. size : isNamedList ? Object.keys(list).length : list.length
  22. }
  23. ;
  24. if (sortMethod)
  25. {
  26. // sort array keys based on it's values
  27. // sort object's keys just on own merit
  28. initState.keyedList.sort(isNamedList ? sortMethod : function(a, b)
  29. {
  30. return sortMethod(list[a], list[b]);
  31. });
  32. }
  33. return initState;
  34. }