Installations
npm install quill-delta-es
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Min. Node Version
>= 12.0.0
Node Version
20.6.1
NPM Version
9.8.1
Score
75.3
Supply Chain
99.5
Quality
76
Maintenance
100
Vulnerability
87.3
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (99.89%)
JavaScript (0.11%)
Developer
Download Statistics
Total Downloads
5,058
Last Day
1
Last Week
17
Last Month
87
Last Year
1,506
GitHub Statistics
959 Stars
274 Commits
132 Forks
18 Watching
3 Branches
16 Contributors
Bundle Size
17.00 kB
Minified
5.95 kB
Minified + Gzipped
Package Meta Information
Latest Version
5.2.11
Package Id
quill-delta-es@5.2.11
Unpacked Size
333.84 kB
Size
72.07 kB
File Count
19
NPM Version
9.8.1
Node Version
20.6.1
Publised On
24 Nov 2023
Total Downloads
Cumulative downloads
Total Downloads
5,058
Last day
-95%
1
Compared to previous day
Last week
-48.5%
17
Compared to previous week
Last month
22.5%
87
Compared to previous month
Last year
-14.6%
1,506
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Delta ES
The differences
This is a forked version to quill-delta. There are some differences between the original version:
- This is a es version for tree shaking.
- The unit tests are rewritten using Vitest.
lodash
is removed from the dependencies. (5kb less after minified + gzipped)
Origin
Deltas are a simple, yet expressive format that can be used to describe contents and changes. The format is JSON based, and is human readable, yet easily parsible by machines. Deltas can describe any rich text document, includes all text and formatting information, without the ambiguity and complexity of HTML.
A Delta is made up of an Array of Operations, which describe changes to a document. They can be an insert
, delete
or retain
. Note operations do not take an index. They always describe the change at the current index. Use retains to "keep" or "skip" certain parts of the document.
Don’t be confused by its name Delta—Deltas represents both documents and changes to documents. If you think of Deltas as the instructions from going from one document to another, the way Deltas represent a document is by expressing the instructions starting from an empty document.
Quick Example
1// Document with text "Gandalf the Grey" 2// with "Gandalf" bolded, and "Grey" in grey 3const delta = new Delta([ 4 { insert: 'Gandalf', attributes: { bold: true } }, 5 { insert: ' the ' }, 6 { insert: 'Grey', attributes: { color: '#ccc' } }, 7]); 8 9// Change intended to be applied to above: 10// Keep the first 12 characters, insert a white 'White' 11// and delete the next four characters ('Grey') 12const death = new Delta() 13 .retain(12) 14 .insert('White', { color: '#fff' }) 15 .delete(4); 16// { 17// ops: [ 18// { retain: 12 }, 19// { insert: 'White', attributes: { color: '#fff' } }, 20// { delete: 4 } 21// ] 22// } 23 24// Applying the above: 25const restored = delta.compose(death); 26// { 27// ops: [ 28// { insert: 'Gandalf', attributes: { bold: true } }, 29// { insert: ' the ' }, 30// { insert: 'White', attributes: { color: '#fff' } } 31// ] 32// }
This README describes Deltas in its general form and API functionality. Additional information on the way Quill specifically uses Deltas can be found on its own Delta docs. A walkthough of the motivation and design thinking behind Deltas are on Designing the Delta Format.
This format is suitable for Operational Transform and defines several functions to support this use case.
Contents
Operations
Construction
Documents
These methods called on or with non-document Deltas will result in undefined behavior.
Utility
Operational Transform
Operations
Insert Operation
Insert operations have an insert
key defined. A String value represents inserting text. Any other type represents inserting an embed (however only one level of object comparison will be performed for equality).
In both cases of text and embeds, an optional attributes
key can be defined with an Object to describe additonal formatting information. Formats can be changed by the retain operation.
1// Insert a bolded "Text" 2{ insert: "Text", attributes: { bold: true } } 3 4// Insert a link 5{ insert: "Google", attributes: { link: 'https://www.google.com' } } 6 7// Insert an embed 8{ 9 insert: { image: 'https://octodex.github.com/images/labtocat.png' }, 10 attributes: { alt: "Lab Octocat" } 11} 12 13// Insert another embed 14{ 15 insert: { video: 'https://www.youtube.com/watch?v=dMH0bHeiRNg' }, 16 attributes: { 17 width: 420, 18 height: 315 19 } 20}
Delete Operation
Delete operations have a Number delete
key defined representing the number of characters to delete. All embeds have a length of 1.
1// Delete the next 10 characters 2{ delete: 10 }
Retain Operation
Retain operations have a Number retain
key defined representing the number of characters to keep (other libraries might use the name keep or skip). An optional attributes
key can be defined with an Object to describe formatting changes to the character range. A value of null
in the attributes
Object represents removal of that key.
Note: It is not necessary to retain the last characters of a document as this is implied.
1// Keep the next 5 characters 2{ retain: 5 } 3 4// Keep and bold the next 5 characters 5{ retain: 5, attributes: { bold: true } } 6 7// Keep and unbold the next 5 characters 8// More specifically, remove the bold key in the attributes Object 9// in the next 5 characters 10{ retain: 5, attributes: { bold: null } }
Construction
constructor
Creates a new Delta object.
Methods
new Delta()
new Delta(ops)
new Delta(delta)
Parameters
ops
- Array of operationsdelta
- Object with anops
key set to an array of operations
Note: No validity/sanity check is performed when constructed with ops or delta. The new delta's internal ops array will also be assigned from ops or delta.ops without deep copying.
Example
1const delta = new Delta([ 2 { insert: 'Hello World' }, 3 { insert: '!', attributes: { bold: true } }, 4]); 5 6const packet = JSON.stringify(delta); 7 8const other = new Delta(JSON.parse(packet)); 9 10const chained = new Delta().insert('Hello World').insert('!', { bold: true });
insert()
Appends an insert operation. Returns this
for chainability.
Methods
insert(text, attributes)
insert(embed, attributes)
Parameters
text
- String representing text to insertembed
- Object representing embed type to insertattributes
- Optional attributes to apply
Example
1delta.insert('Text', { bold: true, color: '#ccc' }); 2delta.insert({ image: 'https://octodex.github.com/images/labtocat.png' });
delete()
Appends a delete operation. Returns this
for chainability.
Methods
delete(length)
Parameters
length
- Number of characters to delete
Example
1delta.delete(5);
retain()
Appends a retain operation. Returns this
for chainability.
Methods
retain(length, attributes)
Parameters
length
- Number of characters to retainattributes
- Optional attributes to apply
Example
1delta.retain(4).retain(5, { color: '#0c6' });
Documents
concat()
Returns a new Delta representing the concatenation of this and another document Delta's operations.
Methods
concat(other)
Parameters
other
- Document Delta to concatenate
Returns
Delta
- Concatenated document Delta
Example
1const a = new Delta().insert('Hello'); 2const b = new Delta().insert('!', { bold: true }); 3 4// { 5// ops: [ 6// { insert: 'Hello' }, 7// { insert: '!', attributes: { bold: true } } 8// ] 9// } 10const concat = a.concat(b);
diff()
Returns a Delta representing the difference between two documents. Optionally, accepts a suggested index where change took place, often representing a cursor position before change.
Methods
diff(other)
diff(other, index)
Parameters
other
- Document Delta to diff againstindex
- Suggested index where change took place
Returns
Delta
- difference between the two documents
Example
1const a = new Delta().insert('Hello'); 2const b = new Delta().insert('Hello!'); 3 4const diff = a.diff(b); // { ops: [{ retain: 5 }, { insert: '!' }] } 5// a.compose(diff) == b
eachLine()
Iterates through document Delta, calling a given function with a Delta and attributes object, representing the line segment.
Methods
eachLine(predicate, newline)
Parameters
predicate
- function to call on each line groupnewline
- newline character, defaults to\n
Example
1const delta = new Delta() 2 .insert('Hello\n\n') 3 .insert('World') 4 .insert({ image: 'octocat.png' }) 5 .insert('\n', { align: 'right' }) 6 .insert('!'); 7 8delta.eachLine((line, attributes, i) => { 9 console.log(line, attributes, i); 10 // Can return false to exit loop early 11}); 12// Should log: 13// { ops: [{ insert: 'Hello' }] }, {}, 0 14// { ops: [] }, {}, 1 15// { ops: [{ insert: 'World' }, { insert: { image: 'octocat.png' } }] }, { align: 'right' }, 2 16// { ops: [{ insert: '!' }] }, {}, 3
invert()
Returned an inverted delta that has the opposite effect of against a base document delta. That is base.compose(delta).compose(inverted) === base
.
Methods
invert(base)
Parameters
base
- Document delta to invert against
Returns
Delta
- inverted delta against the base delta
Example
1const base = new Delta().insert('Hello\n').insert('World'); 2const delta = new Delta().retain(6, { bold: true }).insert('!').delete(5); 3 4const inverted = delta.invert(base); // { ops: [ 5// { retain: 6, attributes: { bold: null } }, 6// { insert: 'World' }, 7// { delete: 1 } 8// ]} 9// base.compose(delta).compose(inverted) === base
Utility
filter()
Returns an array of operations that passes a given function.
Methods
filter(predicate)
Parameters
predicate
- Function to test each operation against. Returntrue
to keep the operation,false
otherwise.
Returns
Array
- Filtered resulting array
Example
1const delta = new Delta() 2 .insert('Hello', { bold: true }) 3 .insert({ image: 'https://octodex.github.com/images/labtocat.png' }) 4 .insert('World!'); 5 6const text = delta 7 .filter((op) => typeof op.insert === 'string') 8 .map((op) => op.insert) 9 .join('');
forEach()
Iterates through operations, calling the provided function for each operation.
Methods
forEach(predicate)
Parameters
predicate
- Function to call during iteration, passing in the current operation.
Example
1delta.forEach((op) => { 2 console.log(op); 3});
length()
Returns length of a Delta, which is the sum of the lengths of its operations.
Methods
length()
Example
1new Delta().insert('Hello').length(); // Returns 5 2 3new Delta().insert('A').retain(2).delete(1).length(); // Returns 4
map()
Returns a new array with the results of calling provided function on each operation.
Methods
map(predicate)
Parameters
predicate
- Function to call, passing in the current operation, returning an element of the new array to be returned
Returns
Array
- A new array with each element being the result of the given function.
Example
1const delta = new Delta() 2 .insert('Hello', { bold: true }) 3 .insert({ image: 'https://octodex.github.com/images/labtocat.png' }) 4 .insert('World!'); 5 6const text = delta 7 .map((op) => { 8 if (typeof op.insert === 'string') { 9 return op.insert; 10 } else { 11 return ''; 12 } 13 }) 14 .join('');
partition()
Create an array of two arrays, the first with operations that pass the given function, the other that failed.
Methods
partition(predicate)
Parameters
predicate
- Function to call, passing in the current operation, returning whether that operation passed
Returns
Array
- A new array of two Arrays, the first with passed operations, the other with failed operations
Example
1const delta = new Delta() 2 .insert('Hello', { bold: true }) 3 .insert({ image: 'https://octodex.github.com/images/labtocat.png' }) 4 .insert('World!'); 5 6const results = delta.partition((op) => typeof op.insert === 'string'); 7const passed = results[0]; // [{ insert: 'Hello', attributes: { bold: true }}, 8// { insert: 'World'}] 9const failed = results[1]; // [{ insert: { image: 'https://octodex.github.com/images/labtocat.png' }}]
reduce()
Applies given function against an accumulator and each operation to reduce to a single value.
Methods
reduce(predicate, initialValue)
Parameters
predicate
- Function to call per iteration, returning an accumulated valueinitialValue
- Initial value to pass to first call to predicate
Returns
any
- the accumulated value
Example
1const delta = new Delta().insert('Hello', { bold: true }) 2 .insert({ image: 'https://octodex.github.com/images/labtocat.png' }) 3 .insert('World!'); 4 5const length = delta.reduce((length, op) => ( 6 length + (op.insert.length || 1); 7), 0);
slice()
Returns copy of delta with subset of operations.
Methods
slice()
slice(start)
slice(start, end)
Parameters
start
- Start index of subset, defaults to 0end
- End index of subset, defaults to rest of operations
Example
1const delta = new Delta().insert('Hello', { bold: true }).insert(' World'); 2 3// { 4// ops: [ 5// { insert: 'Hello', attributes: { bold: true } }, 6// { insert: ' World' } 7// ] 8// } 9const copy = delta.slice(); 10 11// { ops: [{ insert: 'World' }] } 12const world = delta.slice(6); 13 14// { ops: [{ insert: ' ' }] } 15const space = delta.slice(5, 6);
Operational Transform
compose()
Returns a Delta that is equivalent to applying the operations of own Delta, followed by another Delta.
Methods
compose(other)
Parameters
other
- Delta to compose
Example
1const a = new Delta().insert('abc'); 2const b = new Delta().retain(1).delete(1); 3 4const composed = a.compose(b); // composed == new Delta().insert('ac');
transform()
Transform given Delta against own operations.
Methods
transform(other, priority = false)
transform(index, priority = false)
- Alias fortransformPosition
Parameters
other
- Delta to transformpriority
- Boolean used to break ties. Iftrue
, thenthis
takes priority overother
, that is, its actions are considered to happen "first."
Returns
Delta
- transformed Delta
Example
1const a = new Delta().insert('a'); 2const b = new Delta().insert('b').retain(5).insert('c'); 3 4a.transform(b, true); // new Delta().retain(1).insert('b').retain(5).insert('c'); 5a.transform(b, false); // new Delta().insert('b').retain(6).insert('c');
transformPosition()
Transform an index against the delta. Useful for representing cursor/selection positions.
Methods
transformPosition(index, priority = false)
Parameters
index
- index to transform
Returns
Number
- transformed index
Example
1const delta = new Delta().retain(5).insert('a'); 2delta.transformPosition(4); // 4 3delta.transformPosition(5); // 6
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: BSD 3-Clause "New" or "Revised" License: LICENSE:0
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:9: update your workflow using https://app.stepsecurity.io/secureworkflow/slab/delta/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/slab/delta/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/slab/delta/main.yml/main?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
Found 5/13 approved changesets -- score normalized to 3
Reason
9 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 24 are checked with a SAST tool
Score
3.1
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More