Skip to content

Commit cf1ae47

Browse files
committed
[delta] delta.append & test coverage
1 parent 8caa8d1 commit cf1ae47

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed

delta/delta.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -896,8 +896,8 @@ export const $anyOp = s.$union($insertOp, $deleteOp, $textOp, $modifyOp)
896896
/**
897897
* @template {string} [NodeName=any]
898898
* @template {{[k:string|number]:any}} [Attrs={}]
899-
* @template {fingerprintTrait.Fingerprintable|never} [Children=never]
900-
* @template {string|never} [Text=never]
899+
* @template {fingerprintTrait.Fingerprintable} [Children=never]
900+
* @template {string} [Text=never]
901901
* @template {s.Schema<Delta<any,any,any,any,any>>|null} [Schema=any]
902902
*/
903903
export class Delta {
@@ -1155,8 +1155,8 @@ const modDeltaCheck = d => {
11551155
/**
11561156
* @template {string} [NodeName=any]
11571157
* @template {{[key:string|number]:any}} [Attrs={}]
1158-
* @template {fingerprintTrait.Fingerprintable|never} [Children=never]
1159-
* @template {string|never} [Text=never]
1158+
* @template {fingerprintTrait.Fingerprintable} [Children=never]
1159+
* @template {string} [Text=never]
11601160
* @template {s.Schema<Delta<any,any,any,any,any>>|null} [Schema=any]
11611161
* @extends {Delta<NodeName,Attrs,Children,Text,Schema>}
11621162
*/
@@ -1734,8 +1734,32 @@ export class DeltaBuilder extends Delta {
17341734
console.info('method rebaseOnInverse unimplemented')
17351735
return this
17361736
}
1737+
1738+
/**
1739+
* Append child ops from one op to the other.
1740+
*
1741+
* delta.create().insert('a').append(delta.create().insert('b')) // => insert "ab"
1742+
*
1743+
* @template {DeltaAny} OtherDelta
1744+
* @param {OtherDelta} other
1745+
* @return {CastToDelta<OtherDelta> extends Delta<any,any,infer OtherChildren,infer OtherText,any> ? DeltaBuilder<NodeName,Attrs,Children|OtherChildren,Text|OtherText,Schema> : never}
1746+
*/
1747+
append (other) {
1748+
// @todo Investigate. Above is a typescript issue. It is necessary to cast OtherDelta to a Delta first before
1749+
// inferring type, otherwise Children will contain Text.
1750+
for (const child of other.children) {
1751+
list.pushEnd(this.children, child.clone())
1752+
}
1753+
// @ts-ignore
1754+
return this
1755+
}
17371756
}
17381757

1758+
/**
1759+
* @template {DeltaAny} D
1760+
* @typedef {D extends DeltaBuilder<infer N,infer Attrs,infer Children,infer Text,infer Schema> ? Delta<N,Attrs,Children,Text,Schema> : D} CastToDelta
1761+
*/
1762+
17391763
/**
17401764
* @template {string} NodeName
17411765
* @template {{ [key: string|number]: any }} [Attrs={}]
@@ -1813,9 +1837,9 @@ export class $Delta extends s.Schema {
18131837
* @template {{ [k:string]:any }} [Formats={[k:string]:any}]
18141838
* @param {object} opts
18151839
* @param {NodeNameSchema?} [opts.name]
1816-
* @param {AttrsSchema?} [opts.attrs]
1817-
* @param {ChildrenSchema?} [opts.children]
1818-
* @param {HasText} [opts.text]
1840+
* @param {AttrsSchema?} [opts.attrs] What key-value pairs are included.
1841+
* @param {ChildrenSchema?} [opts.children] The type of content in `insertOp`
1842+
* @param {HasText} [opts.text] Whether this delta contains text using `textOp`
18191843
* @param {Formats} [opts.formats]
18201844
* @param {Recursive} [opts.recursive]
18211845
* @return {[s.Unwrap<s.ReadSchema<NodeNameSchema>>,s.Unwrap<s.ReadSchema<AttrsSchema>>,s.Unwrap<s.ReadSchema<ChildrenSchema>>] extends [infer NodeName, infer Attrs, infer Children] ? s.Schema<Delta<
@@ -2132,7 +2156,6 @@ export const diff = (d1, d2) => {
21322156
* @param {DeltaBuilderAny} d
21332157
* @param {ChildrenOpAny[]} opsIs
21342158
* @param {ChildrenOpAny[]} opsShould
2135-
*
21362159
*/
21372160
const diffAndApply = (d, opsIs, opsShould) => {
21382161
// @todo unoptimized implementation. Convert content to array and diff that based on
@@ -2160,7 +2183,7 @@ export const diff = (d1, d2) => {
21602183
cd.insert = shouldContent.slice(cd.index + adj, cd.index + adj + cd.insert.length)
21612184
adj += cd.remove.length - cd.insert.length
21622185
}
2163-
for (let i = 0, lastIndex = 0, currIndexOffset2 = 0; i < cdiff.length; i++) {
2186+
for (let i = 0, lastIndex = 0; i < cdiff.length; i++) {
21642187
const cd = cdiff[i]
21652188
d.retain(cd.index - lastIndex)
21662189
let cdii = 0

delta/delta.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,3 +685,15 @@ export const testRepeatRandomMapDiff = tc => {
685685
d1.apply(d)
686686
t.compare(d1, d2)
687687
}
688+
689+
/**
690+
* @param {t.TestCase} tc
691+
*/
692+
export const testDeltaAppend = tc => {
693+
const $d = delta.$delta({ children: s.$number, text: true })
694+
const other = delta.create().insert('b').insert([1, 2])
695+
const _d = delta.create().insert('a')
696+
const d = _d.append(other)
697+
$d.expect(d)
698+
}
699+

list.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { id } from './function.js'
22
import * as error from './error.js'
33
import * as equalityTrait from './trait/equality.js'
4+
import * as f from './function.js'
45

56
export class ListNode {
67
constructor () {
@@ -39,13 +40,26 @@ export class List {
3940
}
4041
}
4142

43+
toArray () {
44+
return map(this, f.id)
45+
}
46+
4247
/**
4348
* @param {function(N):any} f
4449
*/
4550
forEach (f) {
4651
forEach(this, f)
4752
}
4853

54+
/**
55+
* @template M
56+
* @param {function(N):M} f
57+
* @return {Array<M>}
58+
*/
59+
map (f) {
60+
return map(this, f)
61+
}
62+
4963
/**
5064
* @param {List<any>} other
5165
*/

list.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ export const testSelectivePop = _tc => {
8787
list.insertBetween(l, l.start, l.end, middleNode)
8888
list.replace(l, q3, new QueueItem(4))
8989
t.compare(list.map(l, n => n.v), [1, 2, 4])
90+
t.compare(l.map(n => n.v), [1, 2, 4])
9091
t.compare(list.toArray(l).map(n => n.v), [1, 2, 4])
92+
t.compare(l.toArray().map(n => n.v), [1, 2, 4])
9193
{
9294
let cnt = 0
9395
list.forEach(l, () => cnt++)

schema.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export const testSchemas = _tc => {
9090
const z = s.$instanceOf(Base)
9191
t.assert(z.validate(new Base()))
9292
t.assert(z.validate(new BetterBase()))
93+
t.assert(s.$instanceOf(Base, o => /** @type {any} */ (o).y != null).validate(new BetterBase()))
9394
// @ts-expect-error
9495
t.assert(!z.validate(4))
9596
t.assert(!s.$instanceOf(BetterBetterBase).validate(new BetterBase()))

0 commit comments

Comments
 (0)