@lp-labs/svg-transform-matrix 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Libin Prasanth
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,61 @@
1
- # Temporary Holding Version
1
+ # @lp-labs/svg-transform-matrix
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Bounded SVG transform-list parser and affine point application for standard 2D transform functions.
4
+
5
+ Original, standalone MIT TypeScript implementation with **zero runtime or peer dependencies**. ESM/CommonJS exports and separate declarations; Node18+ and compatible ES2022 browsers. No service, filesystem, network, clock or browser globals accessed on import. Compiler/formatter dependencies are development-only.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @lp-labs/svg-transform-matrix
11
+ ```
12
+
13
+ ## Working example
14
+
15
+ ```js
16
+ import { parseTransform, transformPoint } from '@lp-labs/svg-transform-matrix';
17
+ const matrix = parseTransform('translate(10 5) scale(2)');
18
+ const point = transformPoint(matrix, { x: 3, y: 4 });
19
+ if (point.x !== 16 || point.y !== 13) throw new Error('Unexpected transformed point');
20
+ ```
21
+
22
+ CommonJS: `const api = require('@lp-labs/svg-transform-matrix')`.
23
+
24
+ ## Behavior, customization and limits
25
+
26
+ Parses matrix, translate, scale, rotate (optionally around a center), skewX and skewY with finite numeric arguments. The resulting [a,b,c,d,e,f] matrix uses SVG column-vector transform-list composition; no transform-origin or DOM/CSS integration is included.
27
+
28
+ Use the explicit function arguments and public types below to customize the documented behavior. There are no hidden environment settings or sibling-library requirements. Array inputs must be dense; sparse entries are invalid. Inputs are validated before a successful result is returned. Outputs do not mutate caller-owned inputs. Hostile proxies/getters and concurrent mutation are outside the contract.
29
+
30
+ ## Errors and integration
31
+
32
+ Invalid data never produces an empty success-shaped substitute. TypeError reports incompatible runtime types/nonfinite numbers; RangeError reports unsupported ranges, invalid syntax, inconsistent states and exceeded budgets. Functions that explicitly return null, false or empty arrays describe those legitimate outcomes above. Errors propagate to callers; UI applications should catch at their boundary and visibly notify users. Numerical calculations use JavaScript IEEE754 unless the API explicitly uses bigint; compare approximate numerical results with application-appropriate tolerances.
33
+
34
+ ## Complete public API
35
+
36
+ ```ts
37
+ export type Matrix = readonly [number, number, number, number, number, number];
38
+ export declare function parseTransform(text: string): Matrix;
39
+ export declare function transformPoint(
40
+ matrix: Matrix,
41
+ point: {
42
+ readonly x: number;
43
+ readonly y: number;
44
+ },
45
+ ): {
46
+ x: number;
47
+ y: number;
48
+ };
49
+ ```
50
+
51
+ ## Performance and portability
52
+
53
+ Complexity and resource ceilings are specified above. Buffered synchronous algorithms do not create workers or timers; callers can move expensive work to their own worker. Limits are enforced, not silently truncated. No runtime imports from any other library. No compatibility or completeness claims beyond the stated scope.
54
+
55
+ ## Support
56
+
57
+ If this library is useful to you, consider supporting its maintenance with a [donation via PayPal](https://www.paypal.me/LibinPrasanth).
58
+
59
+ ## Author
60
+
61
+ Created by [Libin Prasanth](https://www.linkedin.com/in/libinprasanth/). Connect on LinkedIn for updates and discussion.
@@ -0,0 +1,9 @@
1
+ export type Matrix = readonly [number, number, number, number, number, number];
2
+ export declare function parseTransform(text: string): Matrix;
3
+ export declare function transformPoint(matrix: Matrix, point: {
4
+ readonly x: number;
5
+ readonly y: number;
6
+ }): {
7
+ x: number;
8
+ y: number;
9
+ };
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseTransform = parseTransform;
4
+ exports.transformPoint = transformPoint;
5
+ function multiply(x, y) {
6
+ const [a, b, c, d, e, f] = x, [g, h, i, j, k, l] = y;
7
+ return [
8
+ a * g + c * h,
9
+ b * g + d * h,
10
+ a * i + c * j,
11
+ b * i + d * j,
12
+ a * k + c * l + e,
13
+ b * k + d * l + f,
14
+ ];
15
+ }
16
+ function parseTransform(text) {
17
+ if (typeof text !== 'string' || text.length > 4096)
18
+ throw new TypeError('Expected bounded transform text');
19
+ const pattern = /([A-Za-z]+)\s*\(([^()]*)\)/g;
20
+ let cursor = 0, m, result = [1, 0, 0, 1, 0, 0], found = false;
21
+ while ((m = pattern.exec(text)) !== null) {
22
+ if (text.slice(cursor, m.index).replace(/[\s,]/g, '') !== '')
23
+ throw new TypeError('Malformed transform list');
24
+ cursor = pattern.lastIndex;
25
+ found = true;
26
+ const args = m[2].trim()
27
+ ? m[2]
28
+ .trim()
29
+ .split(/[\s,]+/)
30
+ .map(Number)
31
+ : [];
32
+ if (args.some((v) => !Number.isFinite(v)))
33
+ throw new TypeError('Transform arguments must be finite');
34
+ let op;
35
+ const name = m[1];
36
+ if (name === 'matrix' && args.length === 6)
37
+ op = [args[0], args[1], args[2], args[3], args[4], args[5]];
38
+ else if (name === 'translate' && (args.length === 1 || args.length === 2))
39
+ op = [1, 0, 0, 1, args[0], args[1] ?? 0];
40
+ else if (name === 'scale' && (args.length === 1 || args.length === 2))
41
+ op = [args[0], 0, 0, args[1] ?? args[0], 0, 0];
42
+ else if (name === 'rotate' && (args.length === 1 || args.length === 3)) {
43
+ const r = (args[0] * Math.PI) / 180, c = Math.cos(r), s = Math.sin(r), rot = [c, s, -s, c, 0, 0];
44
+ op =
45
+ args.length === 1
46
+ ? rot
47
+ : multiply(multiply([1, 0, 0, 1, args[1], args[2]], rot), [
48
+ 1,
49
+ 0,
50
+ 0,
51
+ 1,
52
+ -args[1],
53
+ -args[2],
54
+ ]);
55
+ }
56
+ else if ((name === 'skewX' || name === 'skewY') && args.length === 1) {
57
+ const t = Math.tan((args[0] * Math.PI) / 180);
58
+ op = name === 'skewX' ? [1, 0, t, 1, 0, 0] : [1, t, 0, 1, 0, 0];
59
+ }
60
+ else
61
+ throw new TypeError('Unsupported transform or argument count');
62
+ result = multiply(result, op);
63
+ }
64
+ if (text.slice(cursor).replace(/[\s,]/g, '') !== '' || !found)
65
+ throw new TypeError('Malformed or empty transform list');
66
+ return result;
67
+ }
68
+ function transformPoint(matrix, point) {
69
+ if (matrix.length !== 6 ||
70
+ [...matrix, point.x, point.y].some((v) => typeof v !== 'number' || !Number.isFinite(v)))
71
+ throw new TypeError('Expected finite matrix and point');
72
+ const [a, b, c, d, e, f] = matrix;
73
+ return { x: a * point.x + c * point.y + e, y: b * point.x + d * point.y + f };
74
+ }
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,9 @@
1
+ export type Matrix = readonly [number, number, number, number, number, number];
2
+ export declare function parseTransform(text: string): Matrix;
3
+ export declare function transformPoint(matrix: Matrix, point: {
4
+ readonly x: number;
5
+ readonly y: number;
6
+ }): {
7
+ x: number;
8
+ y: number;
9
+ };
@@ -0,0 +1,70 @@
1
+ function multiply(x, y) {
2
+ const [a, b, c, d, e, f] = x, [g, h, i, j, k, l] = y;
3
+ return [
4
+ a * g + c * h,
5
+ b * g + d * h,
6
+ a * i + c * j,
7
+ b * i + d * j,
8
+ a * k + c * l + e,
9
+ b * k + d * l + f,
10
+ ];
11
+ }
12
+ export function parseTransform(text) {
13
+ if (typeof text !== 'string' || text.length > 4096)
14
+ throw new TypeError('Expected bounded transform text');
15
+ const pattern = /([A-Za-z]+)\s*\(([^()]*)\)/g;
16
+ let cursor = 0, m, result = [1, 0, 0, 1, 0, 0], found = false;
17
+ while ((m = pattern.exec(text)) !== null) {
18
+ if (text.slice(cursor, m.index).replace(/[\s,]/g, '') !== '')
19
+ throw new TypeError('Malformed transform list');
20
+ cursor = pattern.lastIndex;
21
+ found = true;
22
+ const args = m[2].trim()
23
+ ? m[2]
24
+ .trim()
25
+ .split(/[\s,]+/)
26
+ .map(Number)
27
+ : [];
28
+ if (args.some((v) => !Number.isFinite(v)))
29
+ throw new TypeError('Transform arguments must be finite');
30
+ let op;
31
+ const name = m[1];
32
+ if (name === 'matrix' && args.length === 6)
33
+ op = [args[0], args[1], args[2], args[3], args[4], args[5]];
34
+ else if (name === 'translate' && (args.length === 1 || args.length === 2))
35
+ op = [1, 0, 0, 1, args[0], args[1] ?? 0];
36
+ else if (name === 'scale' && (args.length === 1 || args.length === 2))
37
+ op = [args[0], 0, 0, args[1] ?? args[0], 0, 0];
38
+ else if (name === 'rotate' && (args.length === 1 || args.length === 3)) {
39
+ const r = (args[0] * Math.PI) / 180, c = Math.cos(r), s = Math.sin(r), rot = [c, s, -s, c, 0, 0];
40
+ op =
41
+ args.length === 1
42
+ ? rot
43
+ : multiply(multiply([1, 0, 0, 1, args[1], args[2]], rot), [
44
+ 1,
45
+ 0,
46
+ 0,
47
+ 1,
48
+ -args[1],
49
+ -args[2],
50
+ ]);
51
+ }
52
+ else if ((name === 'skewX' || name === 'skewY') && args.length === 1) {
53
+ const t = Math.tan((args[0] * Math.PI) / 180);
54
+ op = name === 'skewX' ? [1, 0, t, 1, 0, 0] : [1, t, 0, 1, 0, 0];
55
+ }
56
+ else
57
+ throw new TypeError('Unsupported transform or argument count');
58
+ result = multiply(result, op);
59
+ }
60
+ if (text.slice(cursor).replace(/[\s,]/g, '') !== '' || !found)
61
+ throw new TypeError('Malformed or empty transform list');
62
+ return result;
63
+ }
64
+ export function transformPoint(matrix, point) {
65
+ if (matrix.length !== 6 ||
66
+ [...matrix, point.x, point.y].some((v) => typeof v !== 'number' || !Number.isFinite(v)))
67
+ throw new TypeError('Expected finite matrix and point');
68
+ const [a, b, c, d, e, f] = matrix;
69
+ return { x: a * point.x + c * point.y + e, y: b * point.x + d * point.y + f };
70
+ }
package/package.json CHANGED
@@ -1,6 +1,63 @@
1
1
  {
2
2
  "name": "@lp-labs/svg-transform-matrix",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Bounded SVG transform-list parser and affine point application for standard 2D transform functions.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/cjs/index.js",
8
+ "module": "./dist/esm/index.js",
9
+ "types": "./dist/esm/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "import": {
13
+ "types": "./dist/esm/index.d.ts",
14
+ "default": "./dist/esm/index.js"
15
+ },
16
+ "require": {
17
+ "types": "./dist/cjs/index.d.ts",
18
+ "default": "./dist/cjs/index.js"
19
+ }
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "sideEffects": false,
29
+ "engines": {
30
+ "node": ">=18"
31
+ },
32
+ "scripts": {
33
+ "build": "node scripts/build.mjs",
34
+ "typecheck": "npm run build && node scripts/check-types.mjs",
35
+ "test": "npm run build && node --test tests/*.test.mjs",
36
+ "format": "prettier --write src scripts tests *.json README.md",
37
+ "format:check": "prettier --check src scripts tests *.json README.md",
38
+ "prepack": "npm run build"
39
+ },
40
+ "devDependencies": {
41
+ "typescript": "^5.9.3",
42
+ "prettier": "^3.6.2"
43
+ },
44
+ "prettier": {
45
+ "singleQuote": true,
46
+ "semi": true,
47
+ "trailingComma": "all",
48
+ "printWidth": 100,
49
+ "endOfLine": "lf"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public",
53
+ "registry": "https://registry.npmjs.org/"
54
+ },
55
+ "author": {
56
+ "name": "Libin Prasanth",
57
+ "url": "https://www.linkedin.com/in/libinprasanth/"
58
+ },
59
+ "funding": {
60
+ "type": "paypal",
61
+ "url": "https://www.paypal.me/LibinPrasanth"
62
+ }
63
+ }