Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ag-grid-community/src/agWidgets/agList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class AgList<
} else {
const currentIdx = listItems.indexOf(highlightedItem);
let nextPos = currentIdx + (isDown ? 1 : -1);
nextPos = Math.min(Math.max(nextPos, 0), listItems.length - 1);
nextPos = Math.max(0, Math.min(nextPos, listItems.length - 1));
itemToHighlight = listItems[nextPos];
}
this.highlightItem(itemToHighlight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
SizeColumnsToContentStrategy,
} from '../interfaces/autoSize';
import { MIN_CENTER_VIEWPORT_WIDTH } from '../pinnedColumns/pinnedColumnService';
import { _clamp } from '../utils/number';
import { _warn } from '../validation/logging';
import { TouchListener } from '../widgets/touchListener';

Expand Down Expand Up @@ -446,7 +447,7 @@ export class ColumnAutosizeService extends BeanStub implements NamedBean {
const maxOverride = widthOverride?.maxWidth ?? params?.defaultMaxWidth ?? Infinity;

const colWidth = column.getActualWidth();
const targetWidth = Math.max(Math.min(colWidth, maxOverride), minOverride);
const targetWidth = _clamp(colWidth, minOverride, maxOverride);

// NOTE: we assign values to `this.actualWidth` of each column without firing events
// for this reason we need to manually dispatch resize events after the resize has been done for each column.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { ColKey } from '../entities/colDef';
import type { ColumnEventType } from '../events';
import type { HeaderCellCtrl, IHeaderCellComp } from '../headerRendering/cells/column/headerCellCtrl';
import type { IHeaderGroupCellComp } from '../headerRendering/cells/columnGroup/headerGroupCellCtrl';
import { _clamp } from '../utils/number';
import { _error } from '../validation/logging';
import { GroupResizeFeature } from './groupResizeFeature';
import { ResizeFeature } from './resizeFeature';
Expand Down Expand Up @@ -239,7 +240,7 @@ export class ColumnResizeService extends BeanStub implements NamedBean {
const minWidth = column.getMinWidth();
const maxWidth = column.getMaxWidth();

const newWidth = Math.min(Math.max(actualWidth + delta, minWidth), maxWidth);
const newWidth = _clamp(actualWidth + delta, minWidth, maxWidth);

this.setColumnWidths([{ key: column, newWidth }], shiftKey, true, 'uiColumnResized');
}
Expand Down
3 changes: 2 additions & 1 deletion packages/ag-grid-community/src/columns/columnFlexService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BeanStub } from '../context/beanStub';
import type { AgColumn } from '../entities/agColumn';
import type { ColumnEventType } from '../events';
import type { ColumnDelayRenderService } from '../rendering/columnDelayRenderService';
import { _clamp } from '../utils/number';
import { dispatchColumnResizedEvent } from './columnEventUtils';

type FlexItem = {
Expand Down Expand Up @@ -158,7 +159,7 @@ export class ColumnFlexService extends BeanStub implements NamedBean {
}

const unclampedSize = item.targetSize;
const clampedSize = Math.min(Math.max(unclampedSize, item.min), item.max);
const clampedSize = _clamp(unclampedSize, item.min, item.max);

totalViolation += clampedSize - unclampedSize;
item.violationType =
Expand Down
3 changes: 2 additions & 1 deletion packages/ag-grid-community/src/entities/agColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type { IFrameworkEventListenerService } from '../interfaces/iFrameworkEve
import type { IRowNode } from '../interfaces/iRowNode';
import type { SortDef, SortDirection, SortType } from '../interfaces/iSort';
import { _mergedEqual } from '../utils/mergeDeep';
import { _clamp } from '../utils/number';
import { _warn } from '../validation/logging';
import type { AgColumnGroup } from './agColumnGroup';
import type { AgProvidedColumnGroup } from './agProvidedColumnGroup';
Expand Down Expand Up @@ -330,7 +331,7 @@ export class AgColumn<TValue = any>

private calculateColInitialWidth(colDef: ColDef): number {
const width = colDef.width ?? colDef.initialWidth ?? 200;
return Math.max(Math.min(width, this.maxWidth), this.minWidth);
return _clamp(width, this.minWidth, this.maxWidth);
}

public isEmptyGroup(): false {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { _isDomLayout } from '../gridOptionsUtils';
import type { WithoutGridCommon } from '../interfaces/iCommon';
import type { IRowNode, VerticalScrollPosition } from '../interfaces/iRowNode';
import type { AnimationFrameService } from '../misc/animationFrameService';
import { _clamp } from '../utils/number';
import { _warn } from '../validation/logging';

const VIEWPORT = 'Viewport';
Expand Down Expand Up @@ -449,7 +450,7 @@ export class GridBodyScrollFeature extends BeanStub {

private clampHorizontalScrollPosition(scrollLeft: number): number {
const maxScrollLeft = this.getMaxHorizontalScrollLeft();
return Math.max(0, Math.min(maxScrollLeft, scrollLeft));
return _clamp(scrollLeft, 0, maxScrollLeft);
}

public setVerticalScrollPosition(vScrollPosition: number): void {
Expand Down
12 changes: 5 additions & 7 deletions packages/ag-grid-community/src/gridOptionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type { AgGridCommon, WithoutGridCommon } from './interfaces/iCommon';
import type { IRowModel, RowModelType } from './interfaces/iRowModel';
import type { IRowNode } from './interfaces/iRowNode';
import type { IServerSideRowModel } from './interfaces/iServerSideRowModel';
import { _isFiniteNumber } from './utils/number';
import { _warn } from './validation/logging';

function isRowModelType(gos: GridOptionsService, rowModelType: RowModelType): boolean {
Expand Down Expand Up @@ -115,7 +116,7 @@ export function _getRowHeightForNode(

const height = gos.getCallback('getRowHeight')!(params);

if (isNumeric(height)) {
if (_isFiniteNumber(height)) {
if (height === 0) {
_warn(23);
}
Expand All @@ -129,7 +130,8 @@ export function _getRowHeightForNode(

const gridOptionsRowHeight = gos.get('rowHeight');

const rowHeight = gridOptionsRowHeight && isNumeric(gridOptionsRowHeight) ? gridOptionsRowHeight : defaultRowHeight;
const rowHeight =
gridOptionsRowHeight && _isFiniteNumber(gridOptionsRowHeight) ? gridOptionsRowHeight : defaultRowHeight;

return { height: rowHeight, estimated: false };
}
Expand All @@ -144,7 +146,7 @@ function getMasterDetailRowHeight(gos: GridOptionsService): { height: number; es

const defaultRowHeight = gos.get('detailRowHeight');

if (isNumeric(defaultRowHeight)) {
if (_isFiniteNumber(defaultRowHeight)) {
return { height: defaultRowHeight, estimated: false };
}

Expand All @@ -170,10 +172,6 @@ export function _getRowHeightAsNumber(beans: BeanCollection): number {
return environment.getDefaultRowHeight();
}

function isNumeric(value: any): value is number {
return !isNaN(value) && typeof value === 'number' && isFinite(value);
}

// returns the dom data, or undefined if not found
export function _getDomData(gos: GridOptionsService, element: Node | null, key: string): any {
const domData = (element as any)[gos.getDomDataKey()];
Expand Down
2 changes: 1 addition & 1 deletion packages/ag-grid-community/src/main-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export {
export { _createIcon, _createIconNoSpan } from './utils/icon';
export { _consoleError, _warnOnce } from './utils/log';
export { _mergeDeep, _mergedEqual } from './utils/mergeDeep';
export { _formatNumberCommas } from './utils/number';
export { _clamp, _formatNumberCommas, _isFiniteNumber, _toFiniteNumber } from './utils/number';
export { _selectAllCells } from './utils/selection';
export { _getValueUsingDotPath } from './utils/value';
export { _errMsg, _error, _logPreInitWarn, _preInitErrMsg, _warn } from './validation/logging';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { RowPosition } from '../interfaces/iRowPosition';
import { CellCtrl } from '../rendering/cell/cellCtrl';
import { RowCtrl } from '../rendering/row/rowCtrl';
import { _focusNextGridCoreContainer, _isHeaderFocusSuppressed } from '../utils/gridFocus';
import { _clamp } from '../utils/number';

interface NavigateParams {
/** The rowIndex to vertically scroll to. */
Expand Down Expand Up @@ -326,7 +327,7 @@ export class NavigationService extends BeanStub implements NamedBean {
currentIndex += step;
}

return Math.max(0, Math.min(currentIndex, lastRowIndex));
return _clamp(currentIndex, 0, lastRowIndex);
}

private getViewportHeight(): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { BeanCollection } from '../context/context';
import type { PaginationChangedEvent } from '../events';
import type { WithoutGridCommon } from '../interfaces/iCommon';
import type { ElementParams } from '../utils/element';
import { _toFiniteNumber } from '../utils/number';
import { _warn } from '../validation/logging';
import { Component } from '../widgets/component';
import type { GridSelect } from '../widgets/gridWidgetTypes';
Expand Down Expand Up @@ -47,10 +48,10 @@ export class PageSizeSelectorComp extends Component {
return;
}

const paginationPageSize = Number(newValue);
const paginationPageSize = _toFiniteNumber(newValue);

if (
isNaN(paginationPageSize) ||
paginationPageSize == null ||
paginationPageSize < 1 ||
paginationPageSize === this.pagination.getPageSize()
) {
Expand Down
3 changes: 2 additions & 1 deletion packages/ag-grid-community/src/rendering/row/rowCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type { UserCompDetails } from '../../interfaces/iUserCompDetails';
import type { GetNoteParams } from '../../interfaces/notes';
import { calculateRowLevel } from '../../styling/rowStyleService';
import { _isStopPropagationForAgGrid } from '../../utils/gridEvent';
import { _clamp } from '../../utils/number';
import type { Component } from '../../widgets/component';
import { CellCtrl } from '../cell/cellCtrl';
import type { ICellRenderer, ICellRendererParams } from '../cellRenderers/iCellRenderer';
Expand Down Expand Up @@ -1141,7 +1142,7 @@ export class RowCtrl extends BeanStub<RowCtrlEvent> {
const minPixel = this.applyPaginationOffset(range.top, true) - 100;
const maxPixel = this.applyPaginationOffset(range.bottom, true) + 100;

return Math.min(Math.max(minPixel, rowTop), maxPixel);
return _clamp(rowTop, minPixel, maxPixel);
}

public isRowRendered() {
Expand Down
19 changes: 19 additions & 0 deletions packages/ag-grid-community/src/utils/number.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import type { LocaleTextFunc } from 'ag-stack';

/** @internal AG_GRID_INTERNAL - Not for public use. Can change / be removed at any time. */
export function _isFiniteNumber(v: unknown): v is number {
return typeof v === 'number' && Number.isFinite(v);
}

/**
* @internal AG_GRID_INTERNAL - Not for public use. Can change / be removed at any time.
* Note: `Number('')` and `Number(' ')` are `0`, so callers must pre-filter blank strings if needed.
*/
export function _toFiniteNumber(v: unknown): number | null {
const n = Number(v);
return Number.isFinite(n) ? n : null;
}

/** @internal AG_GRID_INTERNAL - Not for public use. Can change / be removed at any time. */
export function _clamp(value: number, min: number, max: number): number {
return Math.max(min, Math.min(value, max));
}

/**
* the native method number.toLocaleString(undefined, {minimumFractionDigits: 0})
* puts in decimal places in IE, so we use this method instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
IPinnedSectionCompHost,
PopupService,
} from 'ag-grid-community';
import { BeanStub } from 'ag-grid-community';
import { BeanStub, _clamp } from 'ag-grid-community';

import { Dialog } from '../widgets/dialog';
import { AdvancedFilterComp } from './advancedFilterComp';
Expand Down Expand Up @@ -176,7 +176,7 @@ export class AdvancedFilterCtrl extends BeanStub<AdvancedFilterCtrlEvent> implem
const maxWidth = Math.round(_getAbsoluteWidth(popupParent)) - 2; // assume 1 pixel border
const maxHeight = Math.round(_getAbsoluteHeight(popupParent) * 0.75) - 2;

const width = Math.min(Math.max(700, minWidth), maxWidth);
const width = _clamp(700, minWidth, maxWidth);
const height = Math.min(600, maxHeight);

return { width, height, minWidth };
Expand Down
4 changes: 3 additions & 1 deletion packages/ag-grid-enterprise/src/agStack/agPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
_setDisplayed,
} from 'ag-stack';

import { _clamp } from 'ag-grid-community';

import agPanelCSS from './agPanel.css';

export interface AgPanelPostProcessPopupParams {
Expand Down Expand Up @@ -273,7 +275,7 @@ export class AgPanel<
position = len;
}

position = Math.max(0, Math.min(position, len));
position = _clamp(position, 0, len);

button.addCss('ag-panel-title-bar-button');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
BeanCollection,
ListOption,
} from 'ag-grid-community';
import { BeanStub } from 'ag-grid-community';
import { BeanStub, _toFiniteNumber } from 'ag-grid-community';

import type { AgSliderParams } from '../../../agStack/agSlider';
import type { ColorPickerParams } from '../../widgets/colorPicker';
Expand Down Expand Up @@ -71,10 +71,7 @@ export class ChartMenuParamsFactory extends BeanStub {
max: options?.max,
},
{
parseInputValue: (value) => {
const numberValue = Number(value);
return isNaN(numberValue) ? undefined : numberValue;
},
parseInputValue: (value) => _toFiniteNumber(value) ?? undefined,
formatInputValue: (value) => {
return value == null ? '' : `${value}`;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
ColumnToolPanelState,
ComponentSelector,
} from 'ag-grid-community';
import { Component, DragSourceType, _warn, isProvidedColumnGroup } from 'ag-grid-community';
import { Component, DragSourceType, _clamp, _warn, isProvidedColumnGroup } from 'ag-grid-community';

import type { VirtualListModel } from '../agStack/iVirtualList';
import type { VirtualListDragItem } from '../agStack/iVirtualListDragFeature';
Expand Down Expand Up @@ -205,7 +205,7 @@ export class AgPrimaryColsList extends Component<AgPrimaryColsListEvent> {
movePadding = expanded ? modelItem.children.length : 0;
}

const nextItem = Math.min(Math.max(currentIndex + movePadding + diff, 0), this.displayedColsList.length - 1);
const nextItem = _clamp(currentIndex + movePadding + diff, 0, this.displayedColsList.length - 1);

this.skipRefocus = true;
moveItem(
Expand Down
3 changes: 2 additions & 1 deletion packages/ag-grid-enterprise/src/excelExport/excelCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
import {
BaseCreator,
_addGridCommonParams,
_clamp,
_getHeaderClassesFromColDef,
_getHeaderRowCount,
_warn,
Expand Down Expand Up @@ -271,7 +272,7 @@ const createExcelFileForExcel = (
const { fontSize = 11, author = 'AG Grid', activeTab = 0, customMetadata, suppressPrependAuthorToNotes } = options;

const len = data.length;
const activeTabWithinBounds = Math.max(Math.min(activeTab, len - 1), 0);
const activeTabWithinBounds = _clamp(activeTab, 0, len - 1);

createExcelXMLCoreFolderStructure(zipContainer);
createExcelXmlTables(zipContainer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ExcelImage, ExcelOOXMLTemplate, XmlElement } from 'ag-grid-community';
import { _clamp } from 'ag-grid-community';

import type { ExcelCalculatedImage, ImageAnchor, ImageBoxSize, ImageColor } from '../../assets/excelInterfaces';
import { pixelsToEMU } from '../../assets/excelUtils';
Expand Down Expand Up @@ -172,7 +173,7 @@ const getBlipFill = (image: ExcelImage, index: number) => {
let blipChildren: XmlElement[] | undefined;

if (image.transparency) {
const transparency = Math.min(Math.max(image.transparency, 0), 100);
const transparency = _clamp(image.transparency, 0, 100);
blipChildren = [
{
name: 'a:alphaModFix',
Expand Down Expand Up @@ -266,7 +267,7 @@ const getSpPr = (image: ExcelImage, imageBoxSize: ImageBoxSize) => {
const rotation = image.rotation;
xfrm.properties = {
rawMap: {
rot: Math.min(Math.max(rotation, 0), 360) * 60000,
rot: _clamp(rotation, 0, 360) * 60000,
},
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FormulaError } from '../../ast/utils';
import { _isFiniteNumber } from 'ag-grid-community';

// Helpers for funcs below
const isFiniteNumber = (v: unknown): v is number => typeof v === 'number' && Number.isFinite(v);
import { FormulaError } from '../../ast/utils';

const MS_PER_DAY = 24 * 60 * 60 * 1000;

Expand All @@ -26,7 +25,7 @@ export function coerceFiniteNumber(fname: string, v: unknown): number {
}
throw new FormulaError(48, [fname]);
}
if (isFiniteNumber(v)) {
if (_isFiniteNumber(v)) {
return v;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/ag-grid-enterprise/src/formula/functions/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { _isFiniteNumber } from 'ag-grid-community';
import type { BeanCollection, FormulaParam, RangeParam, ValueParam } from 'ag-grid-community';

import { colIdFromIndex, colIndexFromId, rowIdFromIndex, rowIndexFromId } from '../ast/serializer';
Expand Down Expand Up @@ -77,7 +78,7 @@ function findOperatorSymbol(s: string): OperatorSymbol | null {
}

function toNumberLike(x: unknown): number | null {
if (typeof x === 'number' && Number.isFinite(x)) {
if (_isFiniteNumber(x)) {
return x;
}
if (x instanceof Date) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
_getRowAbove,
_getRowBelow,
_getRowNode,
_isFiniteNumber,
_isRowBefore,
_isSameRow,
_stopPropagationForAgGrid,
Expand Down Expand Up @@ -496,8 +497,7 @@ export class AgFillHandle extends AbstractSelectionHandle {
}

const isNumeric = (v: any) =>
(typeof v === 'number' && Number.isFinite(v)) ||
(typeof v === 'string' && /^[+-]?\d+(?:\.\d+)?$/.test(v.trim()));
_isFiniteNumber(v) || (typeof v === 'string' && /^[+-]?\d+(?:\.\d+)?$/.test(v.trim()));
const allNumbers = values.every(({ value }) => isNumeric(value));

// values should be copied in order if the alt key is pressed
Expand Down
Loading
Loading