Utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * A collection of small utility functions that help with dom manipulation, adding listeners,
  3. * promises and other good things.
  4. *
  5. * @module Utils
  6. */
  7. /**
  8. * Runs a waterfall of promises: calls each task, passing the result
  9. * from the previous one as an argument. The first task is run with an empty array.
  10. *
  11. * @memberof Utils
  12. * @param {array} methods of Promises to run waterfall on
  13. * @return {Promise} of the final task
  14. */
  15. function promiseWaterfall (methods) {
  16. const [resolvedPromise, ...tasks] = methods
  17. const finalTaskPromise = tasks.reduce((prevTaskPromise, task) => {
  18. return prevTaskPromise.then(task)
  19. }, resolvedPromise([])) // initial value
  20. return finalTaskPromise
  21. }
  22. /**
  23. * Adds multiple listeners to to a DOM element
  24. * Equvalent to jQuery’s `$form.on('drag dragstart dragend dragover dragenter dragleave drop')`.
  25. *
  26. * @memberof Utils
  27. * @param {String} el selector
  28. * @param {String} events to add, like `drag dragstart dragend dragover dragenter dragleave drop`
  29. * @param {requestCallback} cb
  30. * @return {String}
  31. */
  32. // function addListenerMulti (el, events, cb) {
  33. // const eventsArray = events.split(' ')
  34. // for (let event in eventsArray) {
  35. // el.addEventListener(eventsArray[event], cb, false)
  36. // }
  37. // }
  38. /**
  39. * Shallow flatten nested arrays.
  40. */
  41. function flatten (arr) {
  42. return [].concat.apply([], arr)
  43. }
  44. /**
  45. * `querySelectorAll` that returns a normal array instead of fileList
  46. */
  47. function qsa (selector, context) {
  48. return Array.prototype.slice.call((context || document).querySelectorAll(selector) || [])
  49. }
  50. /**
  51. * Partition array by a grouping function.
  52. * @param {[type]} array Input array
  53. * @param {[type]} groupingFn Grouping function
  54. * @return {[type]} Array of arrays
  55. */
  56. function groupBy (array, groupingFn) {
  57. return array.reduce((result, item) => {
  58. let key = groupingFn(item)
  59. let xs = result.get(key) || []
  60. xs.push(item)
  61. result.set(key, xs)
  62. return result
  63. }, new Map())
  64. }
  65. /**
  66. * Tests if every array element passes predicate
  67. * @param {Array} array Input array
  68. * @param {Object} predicateFn Predicate
  69. * @return {bool} Every element pass
  70. */
  71. function every (array, predicateFn) {
  72. return array.reduce((result, item) => {
  73. if (!result) {
  74. return false
  75. }
  76. return predicateFn(item)
  77. }, true)
  78. }
  79. /**
  80. * Takes a fileName and turns it into fileID, by converting to lowercase,
  81. * removing extra characters and adding unix timestamp
  82. *
  83. * @param {String} fileName
  84. *
  85. */
  86. function generateFileID (fileName) {
  87. let fileID = fileName.toLowerCase()
  88. fileID = fileID.replace(/[^A-Z0-9]/ig, '')
  89. fileID = fileID + Date.now()
  90. return fileID
  91. }
  92. function extend (...objs) {
  93. return Object.assign.apply(this, [{}].concat(objs))
  94. }
  95. /**
  96. * Takes function or class, returns its name.
  97. * Because IE doesn’t support `constructor.name`.
  98. * https://gist.github.com/dfkaye/6384439, http://stackoverflow.com/a/15714445
  99. *
  100. * @param {Object} fn — function
  101. *
  102. */
  103. function getFnName (fn) {
  104. var f = typeof fn === 'function'
  105. var s = f && ((fn.name && ['', fn.name]) || fn.toString().match(/function ([^\(]+)/))
  106. return (!f && 'not a function') || (s && s[1] || 'anonymous')
  107. }
  108. export default {
  109. promiseWaterfall,
  110. generateFileID,
  111. getFnName,
  112. // addListenerMulti,
  113. every,
  114. flatten,
  115. groupBy,
  116. qsa,
  117. extend
  118. }