File tree 2 files changed +27
-0
lines changed
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -100,6 +100,22 @@ class Record {
100
100
101
101
return this . _fields [ index ] ;
102
102
}
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
+ }
103
119
}
104
120
105
121
export { Record }
Original file line number Diff line number Diff line change @@ -30,6 +30,17 @@ describe('Record', function() {
30
30
expect ( record . get ( "name" ) ) . toEqual ( "Bob" ) ;
31
31
} ) ;
32
32
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
+
33
44
it ( 'should give helpful error on no such key' , function ( ) {
34
45
// Given
35
46
var record = new Record ( [ "name" ] , [ "Bob" ] ) ;
You can’t perform that action at this time.
0 commit comments