Skip to content

Commit bf55a01

Browse files
authored
Code refactoring (#143)
* code refactoring * lint fix * rid off mapValues * remove mapValues * english comments
1 parent 5d77104 commit bf55a01

2 files changed

Lines changed: 237 additions & 307 deletions

File tree

src/facets.js

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { map, mapValues, clone, keys } from 'lodash-es';
1+
import { map, keys } from 'lodash-es';
22
import FastBitSet from 'fastbitset';
33
import {
44
facets_ids,
@@ -71,64 +71,50 @@ export class Facets {
7171
return this._items_map[_id];
7272
}
7373

74-
/*
75-
*
76-
* ids is optional only when there is query
77-
*/
78-
search(input, data) {
74+
search(input, data = {}) {
7975
const config = this.config;
80-
data = data || Object.create(null);
81-
82-
// consider removing clone
83-
const temp_facet = clone(this.facets);
76+
const temp_facet = {};
8477

8578
temp_facet.not_ids = facets_ids(
86-
temp_facet['bits_data'],
79+
this.facets.bits_data,
8780
input.not_filters
8881
);
8982

90-
let temp_data;
91-
9283
const filters = input_to_facet_filters(input, config);
93-
temp_data = matrix(this.facets, filters);
84+
let temp_data = matrix(this.facets, filters);
9485

9586
if (input.filters_query) {
96-
const filters = parse_boolean_query(input.filters_query);
97-
temp_data = filters_matrix(temp_data, filters);
87+
const filtersQuery = parse_boolean_query(input.filters_query);
88+
temp_data = filters_matrix(temp_data, filtersQuery);
9889
}
9990

100-
temp_facet['bits_data_temp'] = temp_data['bits_data_temp'];
101-
102-
mapValues(temp_facet['bits_data_temp'], function (values, key) {
103-
mapValues(
104-
temp_facet['bits_data_temp'][key],
105-
function (facet_indexes, key2) {
106-
if (data.query_ids) {
107-
temp_facet['bits_data_temp'][key][key2] =
108-
data.query_ids.new_intersection(
109-
temp_facet['bits_data_temp'][key][key2]
110-
);
111-
}
112-
113-
if (data.test) {
114-
temp_facet['data'][key][key2] =
115-
temp_facet['bits_data_temp'][key][key2].array();
116-
}
91+
temp_facet.bits_data_temp = temp_data.bits_data_temp;
92+
const bitsDataTemp = temp_facet.bits_data_temp;
93+
94+
if (data.query_ids) {
95+
for (const key in bitsDataTemp) {
96+
for (const key2 in bitsDataTemp[key]) {
97+
bitsDataTemp[key][key2] = data.query_ids.new_intersection(
98+
bitsDataTemp[key][key2]
99+
);
117100
}
118-
);
119-
});
101+
}
102+
}
120103

121-
/**
122-
* calculating ids (for a list of items)
123-
* facets ids is faster and filter ids because filter ids makes union each to each filters
124-
* filter ids needs to be used if there is filters query
125-
*/
126-
if (input.filters_query) {
127-
temp_facet.ids = filters_ids(temp_facet['bits_data_temp']);
128-
} else {
129-
temp_facet.ids = facets_ids(temp_facet['bits_data_temp'], input.filters);
104+
if (data.test) {
105+
temp_facet.data = {};
106+
for (const key in bitsDataTemp) {
107+
temp_facet.data[key] = {};
108+
for (const key2 in bitsDataTemp[key]) {
109+
temp_facet.data[key][key2] = bitsDataTemp[key][key2].array();
110+
}
111+
}
130112
}
131113

114+
temp_facet.ids = input.filters_query
115+
? filters_ids(bitsDataTemp)
116+
: facets_ids(bitsDataTemp, input.filters);
117+
132118
return temp_facet;
133119
}
134120
}

0 commit comments

Comments
 (0)