* Returns created object. * @private * @template FactoryData * @template FallbackCreatedObject * @param {string} type type * @param {FactoryData} data factory data * @param {Omit<StatsFactoryContext, "type">} baseContext context used as base * @returns {CreatedObject<FactoryData, Fallb
(type, data, baseContext)
| 271 | * @returns {CreatedObject<FactoryData, FallbackCreatedObject>} created object |
| 272 | */ |
| 273 | _create(type, data, baseContext) { |
| 274 | const context = /** @type {StatsFactoryContext} */ ({ |
| 275 | ...baseContext, |
| 276 | type, |
| 277 | [type]: data |
| 278 | }); |
| 279 | if (Array.isArray(data)) { |
| 280 | // run filter on unsorted items |
| 281 | const items = this._forEachLevelFilter( |
| 282 | this.hooks.filter, |
| 283 | this._caches.filter, |
| 284 | type, |
| 285 | data, |
| 286 | (h, r, idx, i) => h.call(r, context, idx, i), |
| 287 | true |
| 288 | ); |
| 289 | |
| 290 | // sort items |
| 291 | /** @type {Comparator[]} */ |
| 292 | const comparators = []; |
| 293 | this._forEachLevel(this.hooks.sort, this._caches.sort, type, (h) => |
| 294 | h.call(comparators, context) |
| 295 | ); |
| 296 | if (comparators.length > 0) { |
| 297 | items.sort( |
| 298 | // @ts-expect-error number of arguments is correct |
| 299 | concatComparators(...comparators, keepOriginalOrder(items)) |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | // run filter on sorted items |
| 304 | const items2 = this._forEachLevelFilter( |
| 305 | this.hooks.filterSorted, |
| 306 | this._caches.filterSorted, |
| 307 | type, |
| 308 | items, |
| 309 | (h, r, idx, i) => h.call(r, context, idx, i), |
| 310 | false |
| 311 | ); |
| 312 | |
| 313 | // for each item |
| 314 | let resultItems = items2.map((item, i) => { |
| 315 | /** @type {StatsFactoryContext} */ |
| 316 | const itemContext = { |
| 317 | ...context, |
| 318 | _index: i |
| 319 | }; |
| 320 | |
| 321 | // run getItemName |
| 322 | const itemName = this._forEachLevel( |
| 323 | this.hooks.getItemName, |
| 324 | this._caches.getItemName, |
| 325 | `${type}[]`, |
| 326 | (h) => h.call(item, itemContext) |
| 327 | ); |
| 328 | if (itemName) itemContext[itemName] = item; |
| 329 | const innerType = itemName ? `${type}[].${itemName}` : `${type}[]`; |
| 330 |
no test coverage detected