index.js 637 B

123456789101112131415161718192021222324252627282930
  1. 'use strict'
  2. module.exports = toString
  3. // Get the text content of a node.
  4. // Prefer the node’s plain-text fields, otherwise serialize its children,
  5. // and if the given value is an array, serialize the nodes in it.
  6. function toString(node) {
  7. return (
  8. (node &&
  9. (node.value ||
  10. node.alt ||
  11. node.title ||
  12. ('children' in node && all(node.children)) ||
  13. ('length' in node && all(node)))) ||
  14. ''
  15. )
  16. }
  17. function all(values) {
  18. var result = []
  19. var length = values.length
  20. var index = -1
  21. while (++index < length) {
  22. result[index] = toString(values[index])
  23. }
  24. return result.join('')
  25. }