Skip to content

Commit

Permalink
Use default export for Typescript (#90)
Browse files Browse the repository at this point in the history
* Use default export for Typescript
  • Loading branch information
mondeja authored May 7, 2022
1 parent 62a008a commit fdef74a
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 40 deletions.
1 change: 1 addition & 0 deletions dist/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./index.js").default;
7 changes: 7 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export declare type BBox = [minX: number, minY: number, maxX: number, maxY: number];
/**
* Compute bounding boxes of SVG paths.
* @param {String} d SVG path for which their bounding box will be computed.
* @returns {BBox}
*/
export default function svgPathBbox(d: string): BBox;
20 changes: 12 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict"; /* istanbul ignore next */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var svgpath_1 = __importDefault(require("svgpath"));
"use strict";
exports.__esModule = true;
var svgPath = require("svgpath");
// Precision for consider cubic polynom as quadratic one
var CBEZIER_MINMAX_EPSILON = 0.00000001;
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L89
Expand Down Expand Up @@ -64,9 +62,14 @@ function minmaxC(A) {
}
return [min, max];
}
module.exports = function svgPathBbox(d) {
/**
* Compute bounding boxes of SVG paths.
* @param {String} d SVG path for which their bounding box will be computed.
* @returns {BBox}
*/
function svgPathBbox(d) {
var min = [Infinity, Infinity], max = [-Infinity, -Infinity];
(0, svgpath_1["default"])(d)
svgPath(d)
.abs()
.unarc()
.unshort()
Expand Down Expand Up @@ -143,4 +146,5 @@ module.exports = function svgPathBbox(d) {
}
}, true);
return [min[0], min[1], max[0], max[1]];
};
}
exports["default"] = svgPathBbox;
2 changes: 1 addition & 1 deletion examples/common.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const svgPathBbox = require("../dist");
const svgPathBbox = require("../dist/cjs");

console.log(svgPathBbox("M0 0H3V6Z"));
2 changes: 1 addition & 1 deletion examples/esm/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import svgPathBbox from "../../dist/index.js";
import svgPathBbox from "../../dist/cjs.js";

console.log(svgPathBbox("M0 0H3V6Z"));
4 changes: 1 addition & 3 deletions examples/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// from root folder: `ts-node examples/typescript.ts`

import svgPathBbox from "../src";
import type { BBox } from "../src/BBox";
import type { BBox } from "../src";

type CasesTuple = Array<[string, BBox]>;

Expand Down
21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
{
"name": "svg-path-bbox",
"version": "1.1.0",
"version": "1.2.0",
"description": "Compute bounding boxes of SVG paths.",
"keywords": [
"svg",
"path",
"bbox"
],
"main": "dist/index.js",
"browser": "dist/index.js",
"module": "dist/index.js",
"types": "src/index.d.ts",
"main": "dist/cjs.js",
"browser": "dist/cjs.js",
"module": "dist/cjs.js",
"types": "dist/index.d.ts",
"bin": {
"svg-path-bbox": "src/cli.js"
},
"scripts": {
"coveralls": "cat ./tests/coverage/lcov.info | coveralls",
"prebuild": "run-s lint:fix format",
"build": "run-s build:index clean build:index:covfix",
"build:index": "tsc src/index.ts --outDir dist --esModuleInterop",
"build:index:covfix": "node -e 'require(\"shelljs\").sed(\"-i\", \"\\\"use strict\\\";\", \"\\\"use strict\\\"; /* istanbul ignore next */\", \"dist/index.js\")'",
"build": "run-s build:ts build:cjs",
"build:ts": "tsc",
"build:cjs": "node scripts/build-cjs.js",
"examples": "run-s example:cjs example:esm example:ts",
"example:cjs": "node examples/common.js",
"example:esm": "node examples/esm/index.js",
"example:ts": "ts-node examples/typescript.ts",
"example:ts": "cd examples && ts-node typescript.ts",
"lint": "eslint '{src,examples}/*.ts'",
"lint:fix": "npm run lint -- --fix",
"format": "prettier -w .",
"format:check": "prettier .",
"test": "jest tests",
"clean": "rimraf *.tgz dist/BBox.js",
"prepare": "is-ci || husky install"
},
"author": {
Expand All @@ -53,8 +52,6 @@
"jest": "28.0.3",
"npm-run-all": "4.1.5",
"prettier": "2.6.2",
"rimraf": "3.0.2",
"shelljs": "0.8.5",
"simple-icons": "6.20.0",
"svg-path-bounding-box": "1.0.4",
"ts-node": "10.7.0",
Expand Down
4 changes: 4 additions & 0 deletions scripts/build-cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require("fs").writeFileSync(
"dist/cjs.js",
'module.exports = require("./index.js").default;'
);
1 change: 0 additions & 1 deletion src/BBox.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if (require.main === module) {
process.exit(1);
}

const svgPathBbox = require("../dist");
const svgPathBbox = require("../dist/cjs");
for (let a = 0; a < args.length; a++) {
console.log(svgPathBbox(args[a]).join(" "));
}
Expand Down
7 changes: 0 additions & 7 deletions src/index.d.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

import svgPath from "svgpath";
import type { BBox } from "./BBox";
import * as svgPath from "svgpath";

type minMax = [min: number, max: number];
export type BBox = [minX: number, minY: number, maxX: number, maxY: number];

// Precision for consider cubic polynom as quadratic one
const CBEZIER_MINMAX_EPSILON = 0.00000001;
Expand Down Expand Up @@ -86,7 +86,7 @@ function minmaxC(A: [number, number, number, number]): minMax {
* @param {String} d SVG path for which their bounding box will be computed.
* @returns {BBox}
*/
export = function svgPathBbox(d: string): BBox {
export default function svgPathBbox(d: string): BBox {
const min = [Infinity, Infinity],
max = [-Infinity, -Infinity];
svgPath(d)
Expand Down Expand Up @@ -168,4 +168,4 @@ export = function svgPathBbox(d: string): BBox {
}
}, true);
return [min[0], min[1], max[0], max[1]];
};
}
2 changes: 1 addition & 1 deletion tests/bbox.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const svgPathBbox = require("../dist");
const svgPathBbox = require("../dist/cjs");

const svgPathBboxLinealCases = [
// Mz
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"moduleResolution": "node",
"strict": true,
"module": "commonjs",
"esModuleInterop": true
"declaration": true,
"outDir": "dist"
},
"include": ["./src/index.ts"],
"exclude": ["./node_modules"]
Expand Down

0 comments on commit fdef74a

Please sign in to comment.