/** * Math utilities for demonstrating svelte-docinfo analysis. * @module */ /** * Add two numbers. * @param a - first number * @param b - second number * @returns the sum * @example * ```ts * add(2, 3) // 5 * ``` */ export function add(a: number, b: number): number { return a + b; } // a `const` arrow extracts like a `function` declaration, as `kind: 'function'` /** * Multiply two numbers. * @param a - first number * @param b - second number * @returns the product */ export const multiply = (a: number, b: number): number => a * b; /** * Sum a list by a numeric projection of each item. * @param items - values to sum * @param toNumber - projects each item to the number added * @returns the total, `0` for an empty list * @example * ```ts * sumBy([{ n: 1 }, { n: 2 }], (item) => item.n) // 3 * ``` */ export function sumBy<T>(items: ReadonlyArray<T>, toNumber: (item: T) => number): number { let total = 0; for (const item of items) total += toNumber(item); return total; } /** * Round a number to a fixed count of decimal places. * @param value - number to round * @param precision - decimal places to keep * @returns the rounded number * @internal shared with `Calculator.svelte`, not a stable API */ export function roundTo(value: number, precision: number): number { const factor = 10 ** precision; return Math.round(value * factor) / factor; } /** Configuration for math operations. */ export interface MathConfig { /** Decimal places kept when `round` is set. */ precision: number; /** Round results to `precision`? */ round: boolean; } /** A 2D vector. */ export interface Vector2 { x: number; y: number; } /** * Add one vector into another. * @param target - vector to mutate * @param source - vector to add * @mutates target - adds into its `x` and `y` fields rather than returning a new vector */ export function addVector(target: Vector2, source: Vector2): void { target.x += source.x; target.y += source.y; }
{ "path": "math.ts", "declarations": [ { "name": "add", "kind": "function", "docComment": "Add two numbers.", "typeSignature": "(a: number, b: number): number", "sourceLine": 16, "examples": [ "```ts\nadd(2, 3) // 5\n```" ], "alsoExportedFrom": [ "index.ts" ], "parameters": [ { "name": "a", "type": "number", "description": "first number" }, { "name": "b", "type": "number", "description": "second number" } ], "returnType": "number", "returnDescription": "the sum" }, { "name": "sumBy", "kind": "function", "docComment": "Sum a list by a numeric projection of each item.", "typeSignature": "<T>(items: readonly T[], toNumber: (item: T) => number): number", "sourceLine": 40, "genericParams": [ { "name": "T" } ], "examples": [ "```ts\nsumBy([{ n: 1 }, { n: 2 }], (item) => item.n) // 3\n```" ], "alsoExportedFrom": [ "index.ts" ], "parameters": [ { "name": "items", "type": "readonly T[]", "description": "values to sum" }, { "name": "toNumber", "type": "(item: T) => number", "description": "projects each item to the number added" } ], "returnType": "number", "returnDescription": "the total, `0` for an empty list" }, { "name": "roundTo", "kind": "function", "docComment": "Round a number to a fixed count of decimal places.", "typeSignature": "(value: number, precision: number): number", "sourceLine": 53, "internalMessage": "shared with `Calculator.svelte`, not a stable API", "parameters": [ { "name": "value", "type": "number", "description": "number to round" }, { "name": "precision", "type": "number", "description": "decimal places to keep" } ], "returnType": "number", "returnDescription": "the rounded number" }, { "name": "addVector", "kind": "function", "docComment": "Add one vector into another.", "typeSignature": "(target: Vector2, source: Vector2): void", "sourceLine": 78, "mutates": { "target": "adds into its `x` and `y` fields rather than returning a new vector" }, "parameters": [ { "name": "target", "type": "Vector2", "description": "vector to mutate" }, { "name": "source", "type": "Vector2", "description": "vector to add" } ], "returnType": "void" }, { "name": "multiply", "kind": "function", "docComment": "Multiply two numbers.", "typeSignature": "(a: number, b: number): number", "sourceLine": 28, "alsoExportedFrom": [ "index.ts" ], "parameters": [ { "name": "a", "type": "number", "description": "first number" }, { "name": "b", "type": "number", "description": "second number" } ], "returnType": "number", "returnDescription": "the product" }, { "name": "MathConfig", "kind": "interface", "docComment": "Configuration for math operations.", "typeSignature": "MathConfig", "sourceLine": 59, "alsoExportedFrom": [ "index.ts" ], "members": [ { "name": "precision", "kind": "variable", "docComment": "Decimal places kept when `round` is set.", "typeSignature": "number" }, { "name": "round", "kind": "variable", "docComment": "Round results to `precision`?", "typeSignature": "boolean" } ] }, { "name": "Vector2", "kind": "interface", "docComment": "A 2D vector.", "typeSignature": "Vector2", "sourceLine": 67, "members": [ { "name": "x", "kind": "variable", "typeSignature": "number" }, { "name": "y", "kind": "variable", "typeSignature": "number" } ] } ], "moduleComment": "Math utilities for demonstrating svelte-docinfo analysis.", "dependents": [ "Calculator.svelte", "index.ts" ] }