toObjMap.js.flow 628 B

1234567891011121314151617181920212223242526
  1. // @flow strict
  2. import objectEntries from '../polyfills/objectEntries';
  3. import {
  4. type ObjMap,
  5. type ObjMapLike,
  6. type ReadOnlyObjMap,
  7. type ReadOnlyObjMapLike,
  8. } from './ObjMap';
  9. /* eslint-disable no-redeclare */
  10. declare function toObjMap<T>(obj: ObjMapLike<T>): ObjMap<T>;
  11. declare function toObjMap<T>(obj: ReadOnlyObjMapLike<T>): ReadOnlyObjMap<T>;
  12. export default function toObjMap(obj) {
  13. /* eslint-enable no-redeclare */
  14. if (Object.getPrototypeOf(obj) === null) {
  15. return obj;
  16. }
  17. const map = Object.create(null);
  18. for (const [key, value] of objectEntries(obj)) {
  19. map[key] = value;
  20. }
  21. return map;
  22. }