From 8c3e9657a07758e72a6a92c3e0a9544cc2f1bb4d Mon Sep 17 00:00:00 2001 From: crisbeto Date: Tue, 21 Aug 2018 22:42:33 +0200 Subject: [PATCH] refactor(grid-list): remove unnecessary coercion functions Removes a couple of functions that either duplicate functionality from `cdk/coercion` or are so simple that they can be inlined instead. This change is non-breaking, because the functions were never exported through the public API. --- src/lib/grid-list/BUILD.bazel | 1 + src/lib/grid-list/grid-list-measure.ts | 23 ----------------------- src/lib/grid-list/grid-list.ts | 11 ++++------- src/lib/grid-list/grid-tile.ts | 6 +++--- 4 files changed, 8 insertions(+), 33 deletions(-) delete mode 100644 src/lib/grid-list/grid-list-measure.ts diff --git a/src/lib/grid-list/BUILD.bazel b/src/lib/grid-list/BUILD.bazel index a88f611d22b3..d4b2ac756fc1 100644 --- a/src/lib/grid-list/BUILD.bazel +++ b/src/lib/grid-list/BUILD.bazel @@ -11,6 +11,7 @@ ng_module( deps = [ "//src/lib/core", "//src/cdk/bidi", + "//src/cdk/coercion", ], tsconfig = "//src/lib:tsconfig-build.json", ) diff --git a/src/lib/grid-list/grid-list-measure.ts b/src/lib/grid-list/grid-list-measure.ts deleted file mode 100644 index 56b8f8f98611..000000000000 --- a/src/lib/grid-list/grid-list-measure.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @license - * Copyright Google LLC All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.io/license - */ - -/** - * Converts values into strings. Falsy values become empty strings. - * @docs-private - */ -export function coerceToString(value: string | number): string { - return `${value || ''}`; -} - -/** - * Converts a value that might be a string into a number. - * @docs-private - */ -export function coerceToNumber(value: string | number): number { - return typeof value === 'string' ? parseInt(value, 10) : value; -} diff --git a/src/lib/grid-list/grid-list.ts b/src/lib/grid-list/grid-list.ts index a0f872dab54f..e2a7100f13e7 100644 --- a/src/lib/grid-list/grid-list.ts +++ b/src/lib/grid-list/grid-list.ts @@ -22,10 +22,7 @@ import {MatGridTile} from './grid-tile'; import {TileCoordinator} from './tile-coordinator'; import {TileStyler, FitTileStyler, RatioTileStyler, FixedTileStyler} from './tile-styler'; import {Directionality} from '@angular/cdk/bidi'; -import { - coerceToString, - coerceToNumber, -} from './grid-list-measure'; +import {coerceNumberProperty} from '@angular/cdk/coercion'; // TODO(kara): Conditional (responsive) column count / row size. @@ -72,17 +69,17 @@ export class MatGridList implements OnInit, AfterContentChecked { /** Amount of columns in the grid list. */ @Input() get cols(): number { return this._cols; } - set cols(value: number) { this._cols = coerceToNumber(value); } + set cols(value: number) { this._cols = Math.round(coerceNumberProperty(value)); } /** Size of the grid list's gutter in pixels. */ @Input() get gutterSize(): string { return this._gutter; } - set gutterSize(value: string) { this._gutter = coerceToString(value); } + set gutterSize(value: string) { this._gutter = `${value || ''}`; } /** Set internal representation of row height from the user-provided value. */ @Input() set rowHeight(value: string | number) { - const newValue = coerceToString(value); + const newValue = `${value || ''}`; if (newValue !== this._rowHeight) { this._rowHeight = newValue; diff --git a/src/lib/grid-list/grid-tile.ts b/src/lib/grid-list/grid-tile.ts index 87bc53abfc29..8cc34fcfde62 100644 --- a/src/lib/grid-list/grid-tile.ts +++ b/src/lib/grid-list/grid-tile.ts @@ -18,7 +18,7 @@ import { ChangeDetectionStrategy, } from '@angular/core'; import {MatLine, MatLineSetter} from '@angular/material/core'; -import {coerceToNumber} from './grid-list-measure'; +import {coerceNumberProperty} from '@angular/cdk/coercion'; @Component({ moduleId: module.id, @@ -41,12 +41,12 @@ export class MatGridTile { /** Amount of rows that the grid tile takes up. */ @Input() get rowspan(): number { return this._rowspan; } - set rowspan(value: number) { this._rowspan = coerceToNumber(value); } + set rowspan(value: number) { this._rowspan = Math.round(coerceNumberProperty(value)); } /** Amount of columns that the grid tile takes up. */ @Input() get colspan(): number { return this._colspan; } - set colspan(value: number) { this._colspan = coerceToNumber(value); } + set colspan(value: number) { this._colspan = Math.round(coerceNumberProperty(value)); } /** * Sets the style of the grid-tile element. Needs to be set manually to avoid