Skip to content

Commit f941c74

Browse files
madebyherzblutbrianc
authored andcommitted
Fix Table.hasColumn for columns with custom property names (brianc#322)
There was a bug which caused Table.hasColumn to return false when passed a property name (defined via column.property) instead of a column name.
1 parent 72aff90 commit f941c74

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

lib/table.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,10 @@ Table.prototype.addColumn = function(col, options) {
126126
};
127127

128128
Table.prototype.hasColumn = function(col) {
129-
col = this.createColumn(col);
130-
131-
var cols = this.columns.filter(function(column) {
132-
return column.property === col.property || column.name === col.name;
129+
var columnName = col instanceof Column ? col.name : col;
130+
return this.columns.some(function(column) {
131+
return column.property === columnName || column.name === columnName;
133132
});
134-
135-
return cols.length > 0;
136133
};
137134

138135
Table.prototype.getColumn =

test/table-tests.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ test('hasColumn', function() {
167167
assert.equal(table.hasColumn('baz'), true);
168168
});
169169

170+
test('hasColumn with user-defined column property', function() {
171+
var table = Table.define({
172+
name: 'blah',
173+
columns: [{
174+
name: 'id',
175+
property: 'theId'
176+
}, {name: 'foo'}]
177+
});
178+
179+
assert.equal(table.hasColumn('id'), true);
180+
assert.equal(table.hasColumn('theId'), true);
181+
});
182+
170183
test('the column "from" does not overwrite the from method', function() {
171184
var table = Table.define({ name: 'foo', columns: [] });
172185
table.addColumn('from');

0 commit comments

Comments
 (0)