File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff 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
105121export { Record }
Original file line number Diff line number Diff 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" ] ) ;
You can’t perform that action at this time.
0 commit comments