Skip to content

Fix #82 - Added Record.has(key) #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2016
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