diff --git a/src/models/block.js b/src/models/block.js index a4560b541..f91549396 100644 --- a/src/models/block.js +++ b/src/models/block.js @@ -109,9 +109,7 @@ class Block extends new Record(DEFAULTS) { */ get text() { - return this.nodes - .map(node => node.text) - .join('') + return this.getText() } } diff --git a/src/models/document.js b/src/models/document.js index 4c9a36535..f3f663af8 100644 --- a/src/models/document.js +++ b/src/models/document.js @@ -83,9 +83,7 @@ class Document extends new Record(DEFAULTS) { */ get text() { - return this.nodes - .map(node => node.text) - .join('') + return this.getText() } } diff --git a/src/models/inline.js b/src/models/inline.js index 4ffa22ed1..0c8e8bebe 100644 --- a/src/models/inline.js +++ b/src/models/inline.js @@ -109,9 +109,7 @@ class Inline extends new Record(DEFAULTS) { */ get text() { - return this.nodes - .map(node => node.text) - .join('') + return this.getText() } } diff --git a/src/models/node.js b/src/models/node.js index a1da75608..8a17364ed 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -29,13 +29,24 @@ const Node = { getKeys() { const keys = [] - this.filterDescendants(desc => { + this.forEachDescendant(desc => { keys.push(desc.key) }) return Set(keys) }, + /** + * Get the concatenated text `string` of all child nodes. + * + * @return {String} text + */ + + getText() { + return this.nodes + .reduce((result, node) => result + node.text, '') + }, + /** * Assert that a node has a child by `key` and return it. * @@ -148,18 +159,31 @@ const Node = { */ findDescendantDeep(iterator) { - let descendantFound = null + let found - const found = this.nodes.find(node => { - if (node.kind != 'text') { - descendantFound = node.findDescendantDeep(iterator) - return descendantFound || iterator(node) + this.forEachDescendant(node => { + if (iterator(node)) { + found = node + return false } - - return iterator(node) ? node : null }) - return descendantFound || found + return found + }, + + /** + * Recursively iterate over all descendant nodes with `iterator`. + * + * @param {Function} iterator + */ + + forEachDescendant(iterator) { + return this.nodes.forEach((child, i, nodes) => { + if (iterator(child, i, nodes) === false) { + return false + } + if (child.kind != 'text') child.forEachDescendant(iterator) + }) }, /** @@ -170,11 +194,13 @@ const Node = { */ filterDescendants(iterator) { - return this.nodes.reduce((matches, child, i, nodes) => { - if (iterator(child, i, nodes)) matches = matches.push(child) - if (child.kind != 'text') matches = matches.concat(child.filterDescendants(iterator)) - return matches - }, Block.createList()) + const matches = [] + + this.forEachDescendant((child, i, nodes) => { + if (iterator(child, i, nodes)) matches.push(child) + }) + + return List(matches) }, /** @@ -924,7 +950,7 @@ const Node = { return node.kind == 'text' ? texts.push(node) : texts.concat(node.getTexts()) - }, Block.createList()) + }, List()) }, /** @@ -1298,6 +1324,7 @@ const Node = { */ memoize(Node, [ + 'getText', 'getAncestors', 'getBlocks', 'getBlocksAtRange', diff --git a/src/models/text.js b/src/models/text.js index 343e24a9a..5e821f900 100644 --- a/src/models/text.js +++ b/src/models/text.js @@ -112,8 +112,7 @@ class Text extends new Record(DEFAULTS) { get text() { return this.characters - .map(char => char.text) - .join('') + .reduce((result, char) => result + char.text, '') } /**