tomap.js 793 B

1234567891011121314151617181920212223242526272829
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module utils/tomap
  7. */
  8. import objectToMap from './objecttomap';
  9. import isIterable from './isiterable';
  10. /**
  11. * Transforms object or iterable to map. Iterable needs to be in the format acceptable by the `Map` constructor.
  12. *
  13. * map = toMap( { 'foo': 1, 'bar': 2 } );
  14. * map = toMap( [ [ 'foo', 1 ], [ 'bar', 2 ] ] );
  15. * map = toMap( anotherMap );
  16. *
  17. * @param {Object|Iterable} data Object or iterable to transform.
  18. * @returns {Map} Map created from data.
  19. */
  20. export default function toMap( data ) {
  21. if ( isIterable( data ) ) {
  22. return new Map( data );
  23. } else {
  24. return objectToMap( data );
  25. }
  26. }