Skip to content

Commit a035d10

Browse files
author
Brian Vaughn
committed
Inlined a few variables to reduce this.* references
1 parent 201d732 commit a035d10

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

source/Search.js

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Search {
2525
/**
2626
* Array containing either a property name or a path (list of property names) to a nested value
2727
*/
28-
searchableFields : Array<string|Array<string>>;
28+
_searchableFields : Array<string|Array<string>>;
2929

3030
_searchIndex : ISearchIndex;
3131
_tokenizer : ITokenizer;
@@ -46,7 +46,7 @@ export class Search {
4646
this._tokenizer = new SimpleTokenizer();
4747

4848
this._documents = [];
49-
this.searchableFields = [];
49+
this._searchableFields = [];
5050
}
5151

5252
/**
@@ -128,7 +128,7 @@ export class Search {
128128
*/
129129
addDocuments(documents : Array<Object>) : void {
130130
this._documents = this._documents.concat(documents);
131-
this.indexDocuments_(documents, this.searchableFields);
131+
this.indexDocuments_(documents, this._searchableFields);
132132
}
133133

134134
/**
@@ -137,7 +137,7 @@ export class Search {
137137
* @param field Searchable field or field path. Pass a string to index a top-level field and an array of strings for nested fields.
138138
*/
139139
addIndex(field : string|Array<string>) {
140-
this.searchableFields.push(field);
140+
this._searchableFields.push(field);
141141
this.indexDocuments_(this._documents, [field]);
142142
}
143143

@@ -154,24 +154,30 @@ export class Search {
154154

155155
/**
156156
* @param documents
157-
* @param searchableFields Array containing property names and paths (lists of property names) to nested values
157+
* @param _searchableFields Array containing property names and paths (lists of property names) to nested values
158158
* @private
159159
*/
160-
indexDocuments_(documents : Array<Object>, searchableFields : Array<string|Array<string>>) : void {
160+
indexDocuments_(documents : Array<Object>, _searchableFields : Array<string|Array<string>>) : void {
161161
this._initialized = true;
162162

163+
var indexStrategy = this._indexStrategy;
164+
var sanitizer = this._sanitizer;
165+
var searchIndex = this._searchIndex;
166+
var tokenizer = this._tokenizer;
167+
var uidFieldName = this._uidFieldName;
168+
163169
for (var di = 0, numDocuments = documents.length; di < numDocuments; di++) {
164-
var document : Object = documents[di];
165-
var uid : string = document[this._uidFieldName];
170+
var doc = documents[di];
171+
var uid = doc[uidFieldName];
166172

167-
for (var sfi = 0, numSearchableFields = searchableFields.length; sfi < numSearchableFields; sfi++) {
168-
var fieldValue : any;
169-
var searchableField : string|Array<string> = searchableFields[sfi];
173+
for (var sfi = 0, numSearchableFields = _searchableFields.length; sfi < numSearchableFields; sfi++) {
174+
var fieldValue;
175+
var searchableField = _searchableFields[sfi];
170176

171177
if (searchableField instanceof Array) {
172-
fieldValue = Search.getNestedFieldValue(document, searchableField);
178+
fieldValue = getNestedFieldValue(doc, searchableField);
173179
} else {
174-
fieldValue = document[searchableField];
180+
fieldValue = doc[searchableField];
175181
}
176182

177183
if (
@@ -183,45 +189,45 @@ export class Search {
183189
}
184190

185191
if (typeof fieldValue === 'string') {
186-
var fieldTokens : Array<string> = this._tokenizer.tokenize(this._sanitizer.sanitize(fieldValue));
192+
var fieldTokens = tokenizer.tokenize(sanitizer.sanitize(fieldValue));
187193

188194
for (var fti = 0, numFieldValues = fieldTokens.length; fti < numFieldValues; fti++) {
189-
var fieldToken : string = fieldTokens[fti];
190-
var expandedTokens : Array<string> = this._indexStrategy.expandToken(fieldToken);
195+
var fieldToken = fieldTokens[fti];
196+
var expandedTokens = indexStrategy.expandToken(fieldToken);
191197

192198
for (var eti = 0, nummExpandedTokens = expandedTokens.length; eti < nummExpandedTokens; eti++) {
193199
var expandedToken = expandedTokens[eti];
194200

195-
this._searchIndex.indexDocument(expandedToken, uid, document);
201+
searchIndex.indexDocument(expandedToken, uid, doc);
196202
}
197203
}
198204
}
199205
}
200206
}
201207
}
208+
}
202209

203-
/**
204-
* Find and return a nested object value.
205-
*
206-
* @param object to crawl
207-
* @param path Property path
208-
* @returns {any}
209-
*/
210-
static getNestedFieldValue(object : Object, path : Array<string>) {
211-
path = path || [];
212-
object = object || {};
210+
/**
211+
* Find and return a nested object value.
212+
*
213+
* @param object to crawl
214+
* @param path Property path
215+
* @returns {any}
216+
*/
217+
function getNestedFieldValue(object : Object, path : Array<string>) {
218+
path = path || [];
219+
object = object || {};
213220

214-
var value = object;
221+
var value = object;
215222

216-
// walk down the property path
217-
for (var i = 0; i < path.length; i++) {
218-
value = value[path[i]];
223+
// walk down the property path
224+
for (var i = 0; i < path.length; i++) {
225+
value = value[path[i]];
219226

220-
if (value == null) {
221-
return null;
222-
}
227+
if (value == null) {
228+
return null;
223229
}
224-
225-
return value;
226230
}
231+
232+
return value;
227233
}

0 commit comments

Comments
 (0)