* Creates row at the bottom of the data array. * * @param {number} [index] Physical index of the row before which the new row will be inserted. * @param {number} [amount=1] An amount of rows to add. * @param {object} [options] Additional options for created rows. * @param {string} [op
(index: number | undefined, amount = 1,
{ source, mode = 'above' }: { source?: string; mode?: string } = {})
| 355 | * @returns {number} Returns number of created rows. |
| 356 | */ |
| 357 | createRow(index: number | undefined, amount = 1, |
| 358 | { source, mode = 'above' }: { source?: string; mode?: string } = {}) { |
| 359 | const sourceRowsCount = this.hot!.countSourceRows(); |
| 360 | let physicalRowIndex = sourceRowsCount; |
| 361 | let numberOfCreatedRows = 0; |
| 362 | let rowIndex = index; |
| 363 | |
| 364 | if (typeof rowIndex !== 'number' || rowIndex >= sourceRowsCount) { |
| 365 | rowIndex = sourceRowsCount; |
| 366 | } |
| 367 | |
| 368 | if (rowIndex < this.hot!.countRows()) { |
| 369 | physicalRowIndex = this.hot!.toPhysicalRow(rowIndex); |
| 370 | } |
| 371 | |
| 372 | const continueProcess = this.hot!.runHooks('beforeCreateRow', rowIndex, amount, source); |
| 373 | |
| 374 | if (continueProcess === false || physicalRowIndex === null) { |
| 375 | return { |
| 376 | delta: 0, |
| 377 | }; |
| 378 | } |
| 379 | |
| 380 | const maxRows = this.tableMeta.maxRows; |
| 381 | const columnCount = (this.getSchema() as unknown[]).length; |
| 382 | const rowsToAdd = []; |
| 383 | |
| 384 | while (numberOfCreatedRows < amount && sourceRowsCount + numberOfCreatedRows < (maxRows ?? Infinity)) { |
| 385 | let row = null; |
| 386 | |
| 387 | if (this.hot!.dataType === 'array') { |
| 388 | if (this.tableMeta.dataSchema) { |
| 389 | // Clone template array |
| 390 | row = deepClone(this.getSchema()); |
| 391 | |
| 392 | } else { |
| 393 | row = []; |
| 394 | /* eslint-disable no-loop-func */ |
| 395 | rangeEach(columnCount - 1, () => row.push(null)); |
| 396 | } |
| 397 | |
| 398 | } else if (this.hot!.dataType === 'function') { |
| 399 | const dataSchemaFn = this.tableMeta.dataSchema; |
| 400 | |
| 401 | if (typeof dataSchemaFn !== 'function') { |
| 402 | throwWithCause('`dataSchema` must be a function when `dataType` is `function`'); |
| 403 | } |
| 404 | row = (dataSchemaFn as (rowIndex: number) => unknown)(rowIndex + numberOfCreatedRows); |
| 405 | |
| 406 | } else { |
| 407 | row = {}; |
| 408 | deepExtend(row, this.getSchema() as Record<string, unknown>); |
| 409 | } |
| 410 | |
| 411 | rowsToAdd.push(row); |
| 412 | |
| 413 | numberOfCreatedRows += 1; |
| 414 | } |
nothing calls this directly
no test coverage detected