Skip to content

Commit cd118e0

Browse files
authored
Merge pull request #83 from legraphista/fix-82/record-has-method
Fix #82 - Added Record.has(key)
2 parents 9849824 + 3ef98cb commit cd118e0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/v1/record.js

+16
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,22 @@ class Record {
100100

101101
return this._fields[index];
102102
}
103+
104+
/**
105+
* Check if a value from this record, either by index or by field key, exists.
106+
*
107+
* @param {string|Number} key Field key, or the index of the field.
108+
* @returns {boolean}
109+
*/
110+
has( key ) {
111+
// if key is a number, we check if it is in the _fields array
112+
if( typeof key === "number" ) {
113+
return ( key >= 0 && key < this._fields.length );
114+
}
115+
116+
// if it's not a number, we check _fieldLookup dictionary directly
117+
return this._fieldLookup[key] !== undefined;
118+
}
103119
}
104120

105121
export {Record}

test/v1/record.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ describe('Record', function() {
3030
expect(record.get("name")).toEqual("Bob");
3131
});
3232

33+
it('should allow checking if fields exist', function() {
34+
// Given
35+
var record = new Record( ["name"], ["Bob"] );
36+
37+
// When & Then
38+
expect(record.has("name")).toEqual(true);
39+
expect(record.has("invalid key")).toEqual(false);
40+
expect(record.has(0)).toEqual(true);
41+
expect(record.has(1)).toEqual(false);
42+
});
43+
3344
it('should give helpful error on no such key', function() {
3445
// Given
3546
var record = new Record( ["name"], ["Bob"] );

0 commit comments

Comments
 (0)