Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing typings of helpers, add automatic test #9570

Merged
merged 2 commits into from Sep 2, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -27,3 +27,6 @@ docs/api
*.log
*.swp
*.stackdump

# Generated
/types/tests/autogen*.ts
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -44,7 +44,7 @@
"lint-js": "eslint \"src/**/*.js\" \"test/**/*.js\" \"docs/**/*.js\"",
"lint-md": "eslint \"**/*.md\"",
"lint-tsc": "tsc",
"lint-types": "eslint \"types/**/*.ts\" && tsc -p types/tests/",
"lint-types": "eslint \"types/**/*.ts\" && node -r esm types/tests/autogen.js && tsc -p types/tests/",
"lint": "concurrently \"npm:lint-*\"",
"test": "npm run lint && cross-env NODE_ENV=test karma start --auto-watch --single-run --coverage --grep"
},
Expand All @@ -70,6 +70,7 @@
"eslint-plugin-es": "^4.1.0",
"eslint-plugin-html": "^6.1.2",
"eslint-plugin-markdown": "^2.1.0",
"esm": "^3.2.25",
"glob": "^7.1.6",
"jasmine": "^3.7.0",
"jasmine-core": "^3.7.1",
Expand Down
24 changes: 24 additions & 0 deletions types/geometric.d.ts
Expand Up @@ -11,3 +11,27 @@ export interface Point {
x: number;
y: number;
}

export type TRBL = {
top: number;
right: number;
bottom: number;
left: number;
}

export type TRBLCorners = {
topLeft: number;
topRight: number;
bottomLeft: number;
bottomRight: number;
};

export type CornerRadius = number | Partial<TRBLCorners>;

export type RoundedRect = {
x: number;
y: number;
w: number;
h: number;
radius?: CornerRadius
}
4 changes: 3 additions & 1 deletion types/helpers/helpers.canvas.d.ts
@@ -1,6 +1,6 @@
import { PointStyle } from '../index.esm';
import { Color } from '../color';
import { ChartArea } from '../geometric';
import { ChartArea, RoundedRect } from '../geometric';
import { CanvasFontSpec } from './helpers.options';

export function clearCanvas(canvas: HTMLCanvasElement, ctx?: CanvasRenderingContext2D): void;
Expand Down Expand Up @@ -97,3 +97,5 @@ export function renderText(
font: CanvasFontSpec,
opts?: RenderTextOpts
): void;

export function addRoundedRectPath(ctx: CanvasRenderingContext2D, rect: RoundedRect): void;
17 changes: 17 additions & 0 deletions types/helpers/helpers.core.d.ts
Expand Up @@ -38,13 +38,26 @@ export function isObject(value: unknown): value is AnyObject;
* @returns {boolean}
*/
export function isFinite(value: unknown): value is number;

/**
* Returns `value` if finite, else returns `defaultValue`.
* @param {*} value - The value to return if defined.
* @param {*} defaultValue - The value to return if `value` is not finite.
* @returns {*}
*/
export function finiteOrDefault(value: unknown, defaultValue: number): number;

/**
* Returns `value` if defined, else returns `defaultValue`.
* @param {*} value - The value to return if defined.
* @param {*} defaultValue - The value to return if `value` is undefined.
* @returns {*}
*/
export function valueOrDefault<T>(value: T | undefined, defaultValue: T): T;

export function toPercentage(value: number | string, dimesion: number): number;
export function toDimension(value: number | string, dimension: number): number;

/**
* Calls `fn` with the given `args` in the scope defined by `thisArg` and returns the
* value returned by `fn`. If `fn` is not a function, this method returns undefined.
Expand Down Expand Up @@ -137,4 +150,8 @@ export function mergeIf<T>(target: T, source: AnyObject[]): AnyObject;

export function resolveObjectKey(obj: AnyObject, key: string): AnyObject;

export function defined(value: unknown): boolean;

export function isFunction(value: unknown): boolean;

export function setsEqual(a: Set<unknown>, b: Set<unknown>): boolean;
1 change: 1 addition & 0 deletions types/helpers/helpers.dom.d.ts
Expand Up @@ -15,3 +15,4 @@ export function retinaScale(
forceRatio: number,
forceStyle?: boolean
): void;
export function readUsedSize(element: HTMLElement, property: 'width' | 'height'): number | undefined;
1 change: 1 addition & 0 deletions types/helpers/helpers.math.d.ts
Expand Up @@ -3,6 +3,7 @@ export function isNumber(v: unknown): boolean;
export function almostEquals(x: number, y: number, epsilon: number): boolean;
export function almostWhole(x: number, epsilon: number): number;
export function sign(x: number): number;
export function niceNum(range: number): number;
export function toRadians(degrees: number): number;
export function toDegrees(radians: number): number;
/**
Expand Down
4 changes: 4 additions & 0 deletions types/helpers/helpers.options.d.ts
@@ -1,3 +1,4 @@
import { TRBL, TRBLCorners } from '../geometric';
import { FontSpec } from '../index.esm';

export interface CanvasFontSpec extends FontSpec {
Expand All @@ -20,6 +21,9 @@ export function toFont(options: Partial<FontSpec>): CanvasFontSpec;
*/
export function toLineHeight(value: string, size: number): number;

export function toTRBL(value: number | Partial<TRBL>): TRBL;
export function toTRBLCorners(value: number | Partial<TRBLCorners>): TRBLCorners;

/**
* Converts the given value into a padding object with pre-computed width/height.
* @param {number|object} value - If a number, set the value to all TRBL component;
Expand Down
22 changes: 22 additions & 0 deletions types/tests/autogen.js
@@ -0,0 +1,22 @@
import * as fs from 'fs';
import * as path from 'path';
import * as helpers from '../../src/helpers/index.js';

let fd;

try {
const fn = path.resolve(__dirname, 'autogen_helpers.ts');
fd = fs.openSync(fn, 'w+');
fs.writeSync(fd, 'import * as helpers from \'../helpers\';\n\n');

fs.writeSync(fd, 'const testKeys = [];\n');
for (const key of Object.keys(helpers)) {
if (key[0] !== '_' && typeof helpers[key] === 'function') {
fs.writeSync(fd, `testKeys.push(helpers.${key});\n`);
}
}
} finally {
if (fd !== undefined) {
fs.closeSync(fd);
}
}