Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

transform.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = transform;
  6. var _eachOf = require('./eachOf');
  7. var _eachOf2 = _interopRequireDefault(_eachOf);
  8. var _once = require('./internal/once');
  9. var _once2 = _interopRequireDefault(_once);
  10. var _wrapAsync = require('./internal/wrapAsync');
  11. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  12. var _promiseCallback = require('./internal/promiseCallback');
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. /**
  15. * A relative of `reduce`. Takes an Object or Array, and iterates over each
  16. * element in parallel, each step potentially mutating an `accumulator` value.
  17. * The type of the accumulator defaults to the type of collection passed in.
  18. *
  19. * @name transform
  20. * @static
  21. * @memberOf module:Collections
  22. * @method
  23. * @category Collection
  24. * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
  25. * @param {*} [accumulator] - The initial state of the transform. If omitted,
  26. * it will default to an empty Object or Array, depending on the type of `coll`
  27. * @param {AsyncFunction} iteratee - A function applied to each item in the
  28. * collection that potentially modifies the accumulator.
  29. * Invoked with (accumulator, item, key, callback).
  30. * @param {Function} [callback] - A callback which is called after all the
  31. * `iteratee` functions have finished. Result is the transformed accumulator.
  32. * Invoked with (err, result).
  33. * @returns {Promise} a promise, if no callback provided
  34. * @example
  35. *
  36. * async.transform([1,2,3], function(acc, item, index, callback) {
  37. * // pointless async:
  38. * process.nextTick(function() {
  39. * acc[index] = item * 2
  40. * callback(null)
  41. * });
  42. * }, function(err, result) {
  43. * // result is now equal to [2, 4, 6]
  44. * });
  45. *
  46. * @example
  47. *
  48. * async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) {
  49. * setImmediate(function () {
  50. * obj[key] = val * 2;
  51. * callback();
  52. * })
  53. * }, function (err, result) {
  54. * // result is equal to {a: 2, b: 4, c: 6}
  55. * })
  56. */
  57. function transform(coll, accumulator, iteratee, callback) {
  58. if (arguments.length <= 3 && typeof accumulator === 'function') {
  59. callback = iteratee;
  60. iteratee = accumulator;
  61. accumulator = Array.isArray(coll) ? [] : {};
  62. }
  63. callback = (0, _once2.default)(callback || (0, _promiseCallback.promiseCallback)());
  64. var _iteratee = (0, _wrapAsync2.default)(iteratee);
  65. (0, _eachOf2.default)(coll, (v, k, cb) => {
  66. _iteratee(accumulator, v, k, cb);
  67. }, err => callback(err, accumulator));
  68. return callback[_promiseCallback.PROMISE_SYMBOL];
  69. }
  70. module.exports = exports['default'];