Skip to content

Commit

Permalink
ltx.is (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jun 7, 2016
1 parent 56365a0 commit b22dcbe
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 29 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,3 @@ addons:
packages:
- g++-4.8
- gcc-4.8

before_install:
- npm install -g browserify standard
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ npm run benchmark
## Test

```
npm install -g standard browserify
npm install
npm test
```

## Licence

MIT
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Element = require('./lib/Element')
var equal = require('./lib/equal')
var createElement = require('./lib/createElement')
var tag = require('./lib/tag')
var is = require('./lib/is')

exports = module.exports = tag

Expand All @@ -17,6 +18,10 @@ exports.nameEqual = equal.name
exports.attrsEqual = equal.attrs
exports.childrenEqual = equal.children

exports.isNode = is.isNode
exports.isElement = is.isElement
exports.isText = is.isText

exports.createElement = createElement

exports.escapeXML = escape.escapeXML
Expand Down
7 changes: 3 additions & 4 deletions lib/createElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ var Element = require('./Element')
module.exports = function createElement (name, attrs /*, child1, child2, ...*/) {
var el = new Element(name, attrs)

var children = Array.prototype.slice.call(arguments, 2)
for (var i = 2; i < arguments.length; i++) {
el.cnode(arguments[i])
}

children.forEach(function (child) {
el.cnode(child)
})
return el
}
15 changes: 15 additions & 0 deletions lib/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

var Element = require('./Element')

module.exports.isNode = function is (el) {
return el instanceof Element || typeof el === 'string'
}

module.exports.isElement = function isElement (el) {
return el instanceof Element
}

module.exports.isText = function isText (el) {
return typeof el === 'string'
}
7 changes: 3 additions & 4 deletions lib/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ var parse = require('./parse')

module.exports = function tag (/* [literals], ...substitutions */) {
var literals = arguments[0]
var substitutions = Array.prototype.slice.call(arguments, 1)

var str = ''

for (var i = 0; i < substitutions.length; i++) {
str += literals[i]
str += escape(substitutions[i])
for (var i = 1; i < arguments.length; i++) {
str += literals[i - 1]
str += escape(arguments[i])
}
str += literals[literals.length - 1]

Expand Down
14 changes: 2 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@
"repository": "github:node-xmpp/ltx",
"homepage": "http://github.com/node-xmpp/ltx",
"bugs": "http://github.com/node-xmpp/ltx/issues",
"maintainers": [
{
"name": "Astro",
"email": "astro@spaceboyz.net",
"web": "http://spaceboyz.net/~astro/"
},
{
"name": "Lloyd Watkin",
"email": "lloyd@evilprofessor.co.uk",
"web": "http://www.evilprofessor.co.uk"
}
],
"contributors": [
"Stephan Maka",
"Will Fife",
Expand All @@ -42,11 +30,13 @@
},
"devDependencies": {
"benchmark": "^2.1.0",
"browserify": "^13.0.1",
"libxmljs": "^0.18.0",
"microtime": "^2.0.0",
"node-expat": "^2.3.13",
"node-xml": "^1.0.2",
"sax": "^1.1.5",
"standard": "^7.1.2",
"vows": "^0.8.1"
}
}
52 changes: 52 additions & 0 deletions test/is-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

var vows = require('vows')
var assert = require('assert')
var ltx = require('..')
var is = require('../lib/is')
var Element = ltx.Element

vows.describe('isNode').addBatch({
'isNode': {
'exported correctly': function () {
assert.equal(ltx.isNode, is.isNode)
},
'returns true for Element': function () {
assert.strictEqual(is.isNode(new Element()), true)
},
'returns true for strings': function () {
assert.strictEqual(is.isNode('string'), true)
},
'returns false for anything else': function () {
[123, null, undefined, {}, [], true].forEach(function (value) {
assert.strictEqual(is.isNode(value), false)
})
}
},
'isElement': {
'exported correctly': function () {
assert.equal(ltx.isElement, is.isElement)
},
'returns true for Element': function () {
assert.strictEqual(is.isElement(new Element()), true)
},
'returns false for anything else': function () {
[123, null, undefined, {}, 'string', [], true].forEach(function (value) {
assert.strictEqual(is.isElement(value), false)
})
}
},
'isText': {
'exported correctly': function () {
assert.equal(ltx.isText, is.isText)
},
'returns true for strings': function () {
assert.strictEqual(is.isText('foo'), true)
},
'returns false for anything else': function () {
[123, null, undefined, {}, Element, [], true].forEach(function (value) {
assert.strictEqual(is.isText(value), false)
})
}
}
}).export(module)
11 changes: 10 additions & 1 deletion test/tag-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ vows.describe('tag').addBatch({
// var r = tag`<foo>${'bar'}</foo>`
var r = tag(['<foo>', '</foo>'], 'bar')
assert(r instanceof Element)
var c = ltx.createElement('foo').t('bar')
var c = new Element('foo').t('bar')
assert(c.equals(r))
assert(r.equals(c))
assert.strictEqual(r.toString(), c.toString())
},
'multiple substitutions': function () {
// var r = tag`<foo a="${'b'}">${'bar'}</foo>`
var r = tag(['<foo a="', '">', '</foo>'], 'b', 'bar')
assert(r instanceof Element)
var c = new Element('foo', {a: 'b'}).t('bar')
assert(c.equals(r))
assert(r.equals(c))
assert.strictEqual(r.toString(), c.toString())
Expand Down

0 comments on commit b22dcbe

Please sign in to comment.