[Pkg-javascript-commits] [ltx] 406/469: Element equality
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:03:32 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository ltx.
commit ce8ac36ce10421b9dd8e60c170046815d63b446d
Author: Sonny Piers <sonny at fastmail.net>
Date: Tue Nov 3 14:34:33 2015 +0100
Element equality
---
.travis.yml | 1 +
index.js | 1 +
lib/Element.js | 53 ++++++++++++++++++++++++++
test/element-test.js | 2 +-
test/equality-test.js | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 158 insertions(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index 2bd48c4..7653c21 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -11,6 +11,7 @@ node_js:
- '4.0'
- '4.1'
- '4.2'
+ - '5.0'
- 'stable'
addons:
diff --git a/index.js b/index.js
index bf462ab..963b22f 100644
--- a/index.js
+++ b/index.js
@@ -9,6 +9,7 @@ var Element = require('./lib/Element')
* Element
*/
exports.Element = Element
+exports.equals = Element.equals
exports.createElement = Element.createElement
/**
diff --git a/lib/Element.js b/lib/Element.js
index f410380..d90845f 100644
--- a/lib/Element.js
+++ b/lib/Element.js
@@ -379,6 +379,43 @@ Element.prototype.write = function (writer) {
}
}
+Element.prototype.nameEquals = function (el) {
+ return this.name === el.name
+}
+
+Element.prototype.attrsEquals = function (el) {
+ var attrs = this.attrs
+ var keys = Object.keys(attrs)
+ var length = keys.length
+ if (length !== Object.keys(el.attrs).length) return false
+ for (var i = 0, l = length; i < l; i++) {
+ if (attrs[keys[i]] !== el.attrs[keys[i]]) return false
+ }
+ return true
+}
+
+Element.prototype.childrenEquals = function (el) {
+ var children = this.children
+ var length = children.length
+ if (length !== el.children.length) return false
+ for (var i = 0, l = length; i < l; i++) {
+ var child = children[i]
+ if (typeof child === 'string') {
+ if (child !== el.children[i]) return false
+ } else {
+ if (!child.equals(el.children[i])) return false
+ }
+ }
+ return true
+}
+
+Element.prototype.equals = function (el) {
+ if (!this.nameEquals(el)) return false
+ if (!this.attrsEquals(el)) return false
+ if (!this.childrenEquals(el)) return false
+ return true
+}
+
Element.createElement = function (name, attrs /*, child1, child2, ...*/) {
var el = new Element(name, attrs)
@@ -390,4 +427,20 @@ Element.createElement = function (name, attrs /*, child1, child2, ...*/) {
return el
}
+Element.nameEqual = function (a, b) {
+ return a.nameEquals(b)
+}
+
+Element.attrsEqual = function (a, b) {
+ return a.attrsEquals(b)
+}
+
+Element.childrenEqual = function (a, b) {
+ return a.childrenEquals(b)
+}
+
+Element.equal = function (a, b) {
+ return a.equals(b)
+}
+
module.exports = Element
diff --git a/test/element-test.js b/test/element-test.js
index 1e65961..d7cb135 100644
--- a/test/element-test.js
+++ b/test/element-test.js
@@ -5,7 +5,7 @@ var assert = require('assert')
var ltx = require('..')
var Element = require('../lib/Element')
-vows.describe('ltx').addBatch({
+vows.describe('Element').addBatch({
'new element': {
"doesn't reference original attrs object": function () {
var o = { foo: 'bar' }
diff --git a/test/equality-test.js b/test/equality-test.js
new file mode 100644
index 0000000..5f7a1a8
--- /dev/null
+++ b/test/equality-test.js
@@ -0,0 +1,102 @@
+'use strict'
+
+var vows = require('vows')
+var assert = require('assert')
+var Element = require('../lib/Element')
+
+vows.describe('equality').addBatch({
+ 'nameEquals': {
+ 'it returns true if elements name are equal': function () {
+ var a = new Element('foo')
+ var b = new Element('foo')
+ assert.equal(a.nameEquals(b), true)
+
+ var c = new Element('foo:bar')
+ var d = new Element('foo:bar')
+ assert.equal(c.nameEquals(d), true)
+ },
+ 'it returns false if elements name differ': function () {
+ var a = new Element('foo')
+ var b = new Element('bar')
+ assert.equal(a.nameEquals(b), false)
+
+ var c = new Element('foo:bar')
+ var d = new Element('bar:bar')
+ assert.equal(c.nameEquals(d), false)
+
+ var e = new Element('foo:bar')
+ var f = new Element('foo:foo')
+ assert.equal(e.nameEquals(f), false)
+ }
+ },
+ 'attrsEquals': {
+ 'it returns true if elements attributes are equal': function () {
+ var a = new Element('foo', {a: 'b', b: 'c'})
+ var b = new Element('foo', {a: 'b', b: 'c'})
+ assert.equal(a.attrsEquals(b), true)
+
+ var c = new Element('foo', {a: 'b', b: 'c'})
+ var d = new Element('foo', {b: 'c', a: 'b'})
+ assert.equal(c.attrsEquals(d), true)
+ },
+ 'it returns false if elements attributes differ': function () {
+ var a = new Element('foo', {a: 'b'})
+ var b = new Element('foo')
+ assert.equal(a.attrsEquals(b), false)
+
+ var c = new Element('foo')
+ var d = new Element('foo', {a: 'b'})
+ assert.equal(c.attrsEquals(d), false)
+
+ var e = new Element('foo', {b: 'a'})
+ var f = new Element('foo', {a: 'b'})
+ assert.equal(e.attrsEquals(f), false)
+ }
+ },
+ 'childrenEquals': {
+ 'it returns true if elements children are equal': function () {
+ var a = new Element('foo').c('bar').up().c('foo').root()
+ assert.equal(a.childrenEquals(a), true)
+ var b = new Element('foo').c('bar').up().c('foo').root()
+ assert.equal(a.childrenEquals(b), true)
+ },
+ 'it returns false if elements children name differ': function () {
+ var a = new Element('foo').c('bar').root()
+ var b = new Element('foo').c('foo').root()
+ assert.equal(a.childrenEquals(b), false)
+ },
+ 'it returns false if elements children attrs differ': function () {
+ var a = new Element('foo').c('foo', {foo: 'bar'}).root()
+ var b = new Element('foo').c('foo', {bar: 'foo'}).root()
+ assert.equal(a.childrenEquals(b), false)
+ },
+ 'it returns false if elements children order differ': function () {
+ var a = new Element('foo').c('foo').up().c('bar').root()
+ var b = new Element('foo').c('bar').up().c('foo').root()
+ assert.equal(a.childrenEquals(b), false)
+ }
+ },
+ 'equals': {
+ 'it returns true if elements are equal': function () {
+ var a = new Element('a', {foo: 'bar'}).c('hello').root()
+ assert.equal(a.equals(a), true)
+ var b = new Element('a', {foo: 'bar'}).c('hello').root()
+ assert.equal(a.equals(b), true)
+ },
+ 'it returns false if elements name differ': function () {
+ var a = new Element('foo')
+ var b = new Element('bar')
+ assert.equal(a.equals(b), false)
+ },
+ 'it returns false if elements attrs differ': function () {
+ var a = new Element('foo', {foo: 'bar'})
+ var b = new Element('foo')
+ assert.equal(a.equals(b), false)
+ },
+ 'it returns false if elements children differ': function () {
+ var a = new Element('foo').c('foo').root()
+ var b = new Element('foo').c('bar').root()
+ assert.equal(a.equals(b), false)
+ }
+ }
+}).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