@@ -85,6 +85,32 @@ module.exports = {
8585 } ) ;
8686 callback ( 1 , matches ) ;
8787 } ,
88+ /*
89+ * searching the pokemon details by closest description
90+ */
91+ getClosestDescription : function ( description , data , callback ) {
92+ description = description . toLowerCase ( ) ;
93+
94+ /*Filter the records that match the first 3 characters of the query*/
95+ let data_clone = _ . filter ( data , function ( datum ) {
96+ let datumDescription = datum . description . toLowerCase ( ) ;
97+ return datum . description . length > 8 && description . substring ( 0 , 7 ) === datumDescription . substring ( 0 , 7 ) ;
98+ } ) ;
99+ /*Loop through the data and compute the distance between the query and each name value in the data*/
100+ data_clone . forEach ( function ( pokemon ) {
101+ let pokemonDescription = pokemon . description . toLowerCase ( ) ;
102+ pokemon . distance = this . getStrDistance ( description , pokemonDescription ) ;
103+ } . bind ( this ) ) ;
104+
105+ /*Sort the data in ascending order of distance*/
106+ data_clone = _ . sortBy ( data_clone , "distance" ) ;
107+ /*Select the first 3 data records (3 records that are closest to the query).
108+ * Remove the "distance" */
109+ let matches = _ . map ( _ . first ( data_clone , 3 ) , function ( match ) {
110+ return match ; //_.omit(match, "distance");
111+ } ) ;
112+ callback ( 1 , matches ) ;
113+ } ,
88114 /*
89115 * searching the pokemonIcon details by Id
90116 */
@@ -128,6 +154,26 @@ module.exports = {
128154 }
129155 } . bind ( this ) ) ;
130156 } ,
157+ /*
158+ * get the pokemon details of particular pokemon based on description
159+ */
160+ getByDescription : function ( description , callback ) {
161+ this . get ( { 'description' : new RegExp ( '^.*' + description + '.*$' , 'i' ) } , function ( status , response ) {
162+ if ( status === 1 && _ . isEmpty ( response ) && description . length > 8 ) {
163+ this . getAll ( function ( status , response ) {
164+ if ( status === 1 ) {
165+ this . getClosestDescription ( description , response , function ( status , response ) {
166+ callback ( status , response ) ;
167+ } ) ;
168+ } else {
169+ callback ( status , response ) ;
170+ }
171+ } . bind ( this ) ) ;
172+ } else {
173+ callback ( status , response ) ;
174+ }
175+ } . bind ( this ) ) ;
176+ } ,
131177 /*
132178 * get the pokemon details of particular pokemon based on type
133179 */
0 commit comments