[Pkg-javascript-commits] [ltx] 11/25: stringify (#104)
Jonas Smedegaard
dr at jones.dk
Tue Jan 10 18:21:47 UTC 2017
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository ltx.
commit e544c8265356e7bd6ed583a7961d72cc18e44600
Author: Sonny Piers <sonny at fastmail.net>
Date: Tue Dec 6 20:59:14 2016 +0100
stringify (#104)
---
.travis.yml | 2 --
index.js | 5 +++++
lib/stringify.js | 32 ++++++++++++++++++++++++++++++++
test/stringify-test.js | 39 +++++++++++++++++++++++++++++++++++++++
4 files changed, 76 insertions(+), 2 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 6a2a8e2..9ee1d94 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -6,8 +6,6 @@ env:
- CXX="g++-4.8"
node_js:
- - '0.10'
- - '0.12'
- '4'
- '5'
- '6'
diff --git a/index.js b/index.js
index 0552ca2..ca39d0b 100644
--- a/index.js
+++ b/index.js
@@ -9,6 +9,8 @@ var createElement = require('./lib/createElement')
var tag = require('./lib/tag')
var tagString = require('./lib/tagString')
var is = require('./lib/is')
+var clone = require('./lib/clone')
+var stringify = require('./lib/stringify')
exports = module.exports = tag
@@ -23,6 +25,7 @@ exports.isNode = is.isNode
exports.isElement = is.isElement
exports.isText = is.isText
+exports.clone = clone
exports.createElement = createElement
exports.escapeXML = escape.escapeXML
@@ -35,3 +38,5 @@ exports.parse = parse
exports.tag = tag
exports.tagString = tagString
+
+exports.stringify = stringify
diff --git a/lib/stringify.js b/lib/stringify.js
new file mode 100644
index 0000000..cdba6ea
--- /dev/null
+++ b/lib/stringify.js
@@ -0,0 +1,32 @@
+'use strict'
+
+function stringify (el, indent, level) {
+ if (typeof indent === 'number') indent = ' '.repeat(indent)
+ if (!level) level = 1
+ var s = ''
+ s += '<' + el.name
+
+ Object.keys(el.attrs).forEach((k) => {
+ s += ' ' + k + '=' + '"' + el.attrs[k] + '"'
+ })
+
+ if (el.children.length) {
+ s += '>'
+ el.children.forEach((child, i) => {
+ if (indent) s += '\n' + indent.repeat(level)
+ if (typeof child === 'string') {
+ s += child
+ } else {
+ s += stringify(child, indent, level + 1)
+ }
+ })
+ if (indent) s += '\n' + indent.repeat(level - 1)
+ s += '</' + el.name + '>'
+ } else {
+ s += '/>'
+ }
+
+ return s
+}
+
+module.exports = stringify
diff --git a/test/stringify-test.js b/test/stringify-test.js
new file mode 100644
index 0000000..a85d5ec
--- /dev/null
+++ b/test/stringify-test.js
@@ -0,0 +1,39 @@
+'use strict'
+
+var vows = require('vows')
+var assert = require('assert')
+var ltx = require('..')
+var stringify = require('../lib/stringify')
+
+vows.describe('stringify').addBatch({
+ 'is exported correctly': function () {
+ assert.equal(ltx.stringify, stringify)
+ },
+ 'returns the same result than .toString()': function () {
+ const el = ltx`
+ <foo bar="foo">
+ text
+ <child foo="bar">
+ text
+ <self-closing/>
+ </child>
+ </foo>
+ `
+ assert.equal(el.toString(), stringify(el))
+ },
+ 'indents correctly': function () {
+ const el = ltx`<foo><bar hello="world">text<self/></bar></foo>`
+
+ const expected = [
+ '<foo>',
+ ' <bar hello="world">',
+ ' text',
+ ' <self/>',
+ ' </bar>',
+ '</foo>'
+ ].join('\n')
+
+ assert.equal(stringify(el, 2), expected)
+ assert.equal(stringify(el, ' '), expected)
+ }
+}).export(module)
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git
More information about the Pkg-javascript-commits
mailing list