Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function chaining of shared types #201

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,17 @@ necessary.
</p>
<pre>const yarray = new Y.Array()</pre>
<dl>
<b><code>insert(index:number, content:Array&lt;object|boolean|Array|string|number|Uint8Array|Y.Type&gt;)</code></b>
<b><code>insert(index:number, content:Array&lt;object|boolean|Array|string|number|Uint8Array|Y.Type&gt;):Y.Array</code></b>
<dd>
Insert content at <var>index</var>. Note that content is an array of elements.
I.e. <code>array.insert(0, [1])</code> splices the list and inserts 1 at
position 0.
</dd>
<b><code>push(Array&lt;Object|boolean|Array|string|number|Uint8Array|Y.Type&gt;)</code></b>
<b><code>push(Array&lt;Object|boolean|Array|string|number|Uint8Array|Y.Type&gt;):Y.Array</code></b>
<dd></dd>
<b><code>unshift(Array&lt;Object|boolean|Array|string|number|Uint8Array|Y.Type&gt;)</code></b>
<b><code>unshift(Array&lt;Object|boolean|Array|string|number|Uint8Array|Y.Type&gt;):Y.Array</code></b>
<dd></dd>
<b><code>delete(index:number, length:number)</code></b>
<b><code>delete(index:number, length:number):Y.Array</code></b>
<dd></dd>
<b><code>get(index:number)</code></b>
<dd></dd>
Expand Down Expand Up @@ -292,9 +292,9 @@ or any of its children.
<dl>
<b><code>get(key:string):object|boolean|string|number|Uint8Array|Y.Type</code></b>
<dd></dd>
<b><code>set(key:string, value:object|boolean|string|number|Uint8Array|Y.Type)</code></b>
<b><code>set(key:string, value:object|boolean|string|number|Uint8Array|Y.Type):Y.Map</code></b>
<dd></dd>
<b><code>delete(key:string)</code></b>
<b><code>delete(key:string):Y.Map</code></b>
<dd></dd>
<b><code>has(key:string):boolean</code></b>
<dd></dd>
Expand Down Expand Up @@ -367,14 +367,14 @@ YTextEvents compute changes as deltas.
</p>
<pre>const ytext = new Y.Text()</pre>
<dl>
<b><code>insert(index:number, content:string, [formattingAttributes:Object&lt;string,string&gt;])</code></b>
<b><code>insert(index:number, content:string, [formattingAttributes:Object&lt;string,string&gt;]):Y.Text</code></b>
<dd>
Insert a string at <var>index</var> and assign formatting attributes to it.
<pre>ytext.insert(0, 'bold text', { bold: true })</pre>
</dd>
<b><code>delete(index:number, length:number)</code></b>
<b><code>delete(index:number, length:number):Y.Text</code></b>
<dd></dd>
<b><code>format(index:number, length:number, formattingAttributes:Object&lt;string,string&gt;)</code></b>
<b><code>format(index:number, length:number, formattingAttributes:Object&lt;string,string&gt;):Y.Text</code></b>
<dd>Assign formatting attributes to a range in the text</dd>
<b><code>applyDelta(delta)</code></b>
<dd>See <a href="https://quilljs.com/docs/delta/">Quill Delta</a></dd>
Expand Down Expand Up @@ -421,9 +421,9 @@ or any of its children.
</p>
<pre><code>const yxml = new Y.XmlFragment()</code></pre>
<dl>
<b><code>insert(index:number, content:Array&lt;Y.XmlElement|Y.XmlText&gt;)</code></b>
<b><code>insert(index:number, content:Array&lt;Y.XmlElement|Y.XmlText&gt;):Y.XmlFragment</code></b>
<dd></dd>
<b><code>delete(index:number, length:number)</code></b>
<b><code>delete(index:number, length:number):Y.XmlFragment</code></b>
<dd></dd>
<b><code>get(index:number)</code></b>
<dd></dd>
Expand Down Expand Up @@ -472,17 +472,17 @@ content and be actually XML compliant.
</p>
<pre><code>const yxml = new Y.XmlElement()</code></pre>
<dl>
<b><code>insert(index:number, content:Array&lt;Y.XmlElement|Y.XmlText&gt;)</code></b>
<b><code>insert(index:number, content:Array&lt;Y.XmlElement|Y.XmlText&gt;):Y.XmlElement</code></b>
<dd></dd>
<b><code>delete(index:number, length:number)</code></b>
<b><code>delete(index:number, length:number):Y.XmlElement</code></b>
<dd></dd>
<b><code>get(index:number)</code></b>
<dd></dd>
<b><code>length:number</code></b>
<dd></dd>
<b><code>setAttribute(attributeName:string, attributeValue:string)</code></b>
<b><code>setAttribute(attributeName:string, attributeValue:string):Y.XmlElement</code></b>
<dd></dd>
<b><code>removeAttribute(attributeName:string)</code></b>
<b><code>removeAttribute(attributeName:string):Y.XmlElement</code></b>
<dd></dd>
<b><code>getAttribute(attributeName:string):string</code></b>
<dd></dd>
Expand Down
10 changes: 8 additions & 2 deletions src/types/YArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class YArray extends AbstractType {
*
* @param {number} index The index to insert content at.
* @param {Array<T>} content The array of content
* @return {YArray<T>} Instance of the YArray
*/
insert (index, content) {
if (this.doc !== null) {
Expand All @@ -110,31 +111,35 @@ export class YArray extends AbstractType {
} else {
/** @type {Array<any>} */ (this._prelimContent).splice(index, 0, ...content)
}
return this
}

/**
* Appends content to this YArray.
*
* @param {Array<T>} content Array of content to append.
* @return {YArray<T>} Instance of the YArray
*/
push (content) {
this.insert(this.length, content)
return this.insert(this.length, content)
}

/**
* Preppends content to this YArray.
*
* @param {Array<T>} content Array of content to preppend.
* @return {YArray<T>} Instance of the YArray
*/
unshift (content) {
this.insert(0, content)
return this.insert(0, content)
}

/**
* Deletes elements starting from an index.
*
* @param {number} index Index at which to start deleting elements
* @param {number} length The number of elements to remove. Defaults to 1.
* @return {YArray<T>} Instance of the YArray
*/
delete (index, length = 1) {
if (this.doc !== null) {
Expand All @@ -144,6 +149,7 @@ export class YArray extends AbstractType {
} else {
/** @type {Array<any>} */ (this._prelimContent).splice(index, length)
}
return this
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/types/YMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class YMap extends AbstractType {
* Remove a specified element from this YMap.
*
* @param {string} key The key of the element to remove.
* @return {YMap<T>} Instance of the YMap.
*/
delete (key) {
if (this.doc !== null) {
Expand All @@ -170,13 +171,15 @@ export class YMap extends AbstractType {
} else {
/** @type {Map<string, any>} */ (this._prelimContent).delete(key)
}
return this
}

/**
* Adds or updates an element with a specified key and value.
*
* @param {string} key The key of the element to add to this YMap
* @param {T} value The value of the element to add
* @return {YMap<T>} Instance of the YMap
*/
set (key, value) {
if (this.doc !== null) {
Expand All @@ -186,7 +189,7 @@ export class YMap extends AbstractType {
} else {
/** @type {Map<string, any>} */ (this._prelimContent).set(key, value)
}
return value
return this
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/types/YText.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,11 +989,12 @@ export class YText extends AbstractType {
* @param {TextAttributes} [attributes] Optionally define some formatting
* information to apply on the inserted
* Text.
* @return {YText} Instance of the YText.
* @public
*/
insert (index, text, attributes) {
if (text.length <= 0) {
return
return this
}
const y = this.doc
if (y !== null) {
Expand All @@ -1009,6 +1010,7 @@ export class YText extends AbstractType {
} else {
/** @type {Array<function>} */ (this._pending).push(() => this.insert(index, text, attributes))
}
return this
}

/**
Expand Down Expand Up @@ -1041,12 +1043,13 @@ export class YText extends AbstractType {
*
* @param {number} index Index at which to start deleting.
* @param {number} length The number of characters to remove. Defaults to 1.
* @return {YText} Instance of the YText.
*
* @public
*/
delete (index, length) {
if (length === 0) {
return
return this
}
const y = this.doc
if (y !== null) {
Expand All @@ -1057,6 +1060,7 @@ export class YText extends AbstractType {
} else {
/** @type {Array<function>} */ (this._pending).push(() => this.delete(index, length))
}
return this
}

/**
Expand All @@ -1066,12 +1070,13 @@ export class YText extends AbstractType {
* @param {number} length The amount of characters to assign properties to.
* @param {TextAttributes} attributes Attribute information to apply on the
* text.
* @return {YText} Instance of the YText.
*
* @public
*/
format (index, length, attributes) {
if (length === 0) {
return
return this
}
const y = this.doc
if (y !== null) {
Expand All @@ -1085,6 +1090,7 @@ export class YText extends AbstractType {
} else {
/** @type {Array<function>} */ (this._pending).push(() => this.format(index, length, attributes))
}
return this
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/types/YXmlElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class YXmlElement extends YXmlFragment {
* Removes an attribute from this YXmlElement.
*
* @param {String} attributeName The attribute name that is to be removed.
* @return {YXmlElement} Instance of the YXmlElement.
*
* @public
*/
Expand All @@ -100,13 +101,15 @@ export class YXmlElement extends YXmlFragment {
} else {
/** @type {Map<string,any>} */ (this._prelimAttrs).delete(attributeName)
}
return this
}

/**
* Sets or updates an attribute.
*
* @param {String} attributeName The attribute name that is to be set.
* @param {String} attributeValue The attribute value that is to be set.
* @return {YXmlElement} Instance of the YXmlElement.
*
* @public
*/
Expand All @@ -118,6 +121,7 @@ export class YXmlElement extends YXmlFragment {
} else {
/** @type {Map<string, any>} */ (this._prelimAttrs).set(attributeName, attributeValue)
}
return this
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/types/YXmlFragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export class YXmlFragment extends AbstractType {
*
* @param {number} index The index to insert content at
* @param {Array<YXmlElement|YXmlText>} content The array of content
* @return {YXmlFragment} Instance of the YXmlFragment.
*/
insert (index, content) {
if (this.doc !== null) {
Expand All @@ -291,13 +292,15 @@ export class YXmlFragment extends AbstractType {
// @ts-ignore _prelimContent is defined because this is not yet integrated
this._prelimContent.splice(index, 0, ...content)
}
return this
}

/**
* Deletes elements starting from an index.
*
* @param {number} index Index at which to start deleting elements
* @param {number} [length=1] The number of elements to remove. Defaults to 1.
* @return {YXmlFragment} Instance of the YXmlFragment.
*/
delete (index, length = 1) {
if (this.doc !== null) {
Expand All @@ -308,6 +311,7 @@ export class YXmlFragment extends AbstractType {
// @ts-ignore _prelimContent is defined because this is not yet integrated
this._prelimContent.splice(index, length)
}
return this
}

/**
Expand Down
9 changes: 6 additions & 3 deletions tests/y-map.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export const testGetAndSetOfMapProperty = tc => {
*/
export const testYmapSetsYmap = tc => {
const { users, map0 } = init(tc, { users: 2 })
const map = map0.set('Map', new Y.Map())
const map = new Y.Map()
map0.set('Map', map)
t.assert(map0.get('Map') === map)
map.set('one', 1)
t.compare(map.get('one'), 1)
Expand All @@ -92,7 +93,8 @@ export const testYmapSetsYmap = tc => {
*/
export const testYmapSetsYarray = tc => {
const { users, map0 } = init(tc, { users: 2 })
const array = map0.set('Array', new Y.Array())
const array = new Y.Array()
map0.set('Array', array)
t.assert(array === map0.get('Array'))
array.insert(0, [1, 2, 3])
// @ts-ignore
Expand Down Expand Up @@ -191,7 +193,8 @@ export const testGetAndSetAndDeleteOfMapPropertyWithThreeConflicts = tc => {
*/
export const testObserveDeepProperties = tc => {
const { testConnector, users, map1, map2, map3 } = init(tc, { users: 4 })
const _map1 = map1.set('map', new Y.Map())
const _map1 = new Y.Map()
map1.set('map', _map1)
let calls = 0
let dmapid
map1.observeDeep(events => {
Expand Down