diff --git a/README.md b/README.md index b683c535..aa388d6e 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ height="60px" />](https://input.com/) [![JourneyApps](https://github.com/journeyapps.png?size=60)](https://github.com/journeyapps) [![Adam Brunnmeier](https://github.com/adabru.png?size=60)](https://github.com/adabru) [![Nathanael Anderson](https://github.com/NathanaelA.png?size=60)](https://github.com/NathanaelA) +[![Gremloon](https://github.com/gremloon.png?size=60)](https://github.com/gremloon) Sponsorship also comes with special perks! [![Become a Sponsor](https://img.shields.io/static/v1?label=Become%20a%20Sponsor&message=%E2%9D%A4&logo=GitHub&style=flat&color=d42f2d)](https://github.com/sponsors/dmonad) diff --git a/src/types/YMap.js b/src/types/YMap.js index aa6ebedc..b3d7fd95 100644 --- a/src/types/YMap.js +++ b/src/types/YMap.js @@ -45,13 +45,23 @@ export class YMapEvent extends YEvent { * @implements {Iterable} */ export class YMap extends AbstractType { - constructor () { + /** + * + * @param {Iterable=} entries - an optional iterable to initialize the YMap + */ + constructor (entries) { super() /** * @type {Map?} * @private */ - this._prelimContent = new Map() + this._prelimContent = null + + if (entries === undefined) { + this._prelimContent = new Map() + } else { + this._prelimContent = new Map(entries) + } } /** diff --git a/tests/y-map.tests.js b/tests/y-map.tests.js index 00d01172..1809af6b 100644 --- a/tests/y-map.tests.js +++ b/tests/y-map.tests.js @@ -8,6 +8,33 @@ import * as Y from '../src/index.js' import * as t from 'lib0/testing.js' import * as prng from 'lib0/prng.js' +/** + * @param {t.TestCase} tc + */ +export const testMapHavingIterableAsConstructorParamTests = tc => { + const { map0 } = init(tc, { users: 1 }) + + const m1 = new Y.Map(Object.entries({ number: 1, string: 'hello' })) + map0.set('m1', m1) + t.assert(m1.get('number') === 1) + t.assert(m1.get('string') === 'hello') + + const m2 = new Y.Map([ + ['object', { x: 1 }], + ['boolean', true] + ]) + map0.set('m2', m2) + t.assert(m2.get('object').x === 1) + t.assert(m2.get('boolean') === true) + + const m3 = new Y.Map([...m1, ...m2]) + map0.set('m3', m3) + t.assert(m3.get('number') === 1) + t.assert(m3.get('string') === 'hello') + t.assert(m3.get('object').x === 1) + t.assert(m3.get('boolean') === true) +} + /** * @param {t.TestCase} tc */