Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/v1/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ class Record {

return this._fields[index];
}

/**
* Check if a value from this record, either by index or by field key, exists.
*
* @param {string|Number} key Field key, or the index of the field.
* @returns {boolean}
*/
has( key ) {
// if key is a number, we check if it is in the _fields array
if( typeof key === "number" ) {
return ( key >= 0 && key < this._fields.length );
}

// if it's not a number, we check _fieldLookup dictionary directly
return this._fieldLookup[key] !== undefined;
}
}

export {Record}
11 changes: 11 additions & 0 deletions test/v1/record.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ describe('Record', function() {
expect(record.get("name")).toEqual("Bob");
});

it('should allow checking if fields exist', function() {
// Given
var record = new Record( ["name"], ["Bob"] );

// When & Then
expect(record.has("name")).toEqual(true);
expect(record.has("invalid key")).toEqual(false);
expect(record.has(0)).toEqual(true);
expect(record.has(1)).toEqual(false);
});

it('should give helpful error on no such key', function() {
// Given
var record = new Record( ["name"], ["Bob"] );
Expand Down