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.

40 lines
1.1 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import keys from "./keys.js";
  3. /**
  4. * Iterate over an input `object`, calling a provided function `fn` for each
  5. * key and value in the object.
  6. *
  7. * `fn` receives three argument: *(value, key, obj)*.
  8. *
  9. * @func
  10. * @memberOf R
  11. * @since v0.23.0
  12. * @category Object
  13. * @sig ((a, String, StrMap a) -> Any) -> StrMap a -> StrMap a
  14. * @param {Function} fn The function to invoke. Receives three argument, `value`, `key`, `obj`.
  15. * @param {Object} obj The object to iterate over.
  16. * @return {Object} The original object.
  17. * @example
  18. *
  19. * const printKeyConcatValue = (value, key) => console.log(key + ':' + value);
  20. * R.forEachObjIndexed(printKeyConcatValue, {x: 1, y: 2}); //=> {x: 1, y: 2}
  21. * // logs x:1
  22. * // logs y:2
  23. * @symb R.forEachObjIndexed(f, {x: a, y: b}) = {x: a, y: b}
  24. */
  25. var forEachObjIndexed =
  26. /*#__PURE__*/
  27. _curry2(function forEachObjIndexed(fn, obj) {
  28. var keyList = keys(obj);
  29. var idx = 0;
  30. while (idx < keyList.length) {
  31. var key = keyList[idx];
  32. fn(obj[key], key, obj);
  33. idx += 1;
  34. }
  35. return obj;
  36. });
  37. export default forEachObjIndexed;