Skip to content

Commit

Permalink
Only named exports (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja authored Jun 6, 2024
1 parent cb81720 commit b086a98
Show file tree
Hide file tree
Showing 25 changed files with 266 additions and 1,467 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- name: Install
run: npm ci
run: npm ci --ignore-scripts --no-audit --no-fund
- name: Build
run: npm run build
- name: Test
run: npm test
run: |
npx puppeteer browsers install chrome
npm test
- name: Coverage
uses: coverallsapp/github-action@v2
with:
Expand All @@ -44,7 +46,7 @@ jobs:
with:
node-version: 20.x
- name: Install
run: npm ci
run: npm ci --ignore-scripts --no-audit --no-fund
- name: Lint
run: npm run lint

Expand All @@ -63,11 +65,13 @@ jobs:
with:
node-version: 20.x
- name: Install dependencies
run: npm ci
run: npm ci --ignore-scripts --no-audit --no-fund
- name: Build
run: npm run build
- name: Test
run: npm test
run: |
npx puppeteer browsers install chrome
npm test
- name: Deploy to NPM
uses: JS-DevTools/npm-publish@v3
with:
Expand Down
5 changes: 3 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

set -e

run-s build test
git add .
npm run build
npm t
git add -u
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*
!LICENSE
!dist/
!src/
!package.json
!CHANGELOG.md
!README.md
!tsconfig.json
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package-lock=true
save-exact=true
save-dev=true
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# CHANGELOG

## [Unreleased]
## [2.0.0] - 2024-06-06

- No more default export. Now you have to import `svgPathBbox` function
directly. Use `import { svgPathBbox } from "svg-path-bbox";` instead of
`import svgPathBbox from "svg-path-bbox";`.

## [1.2.6] - 2024-03-14

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ npm install svg-path-bbox
### Usage

```javascript
> import svgPathBbox from "svg-path-bbox";
> import { svgPathBbox } from "svg-path-bbox";
> svgPathBbox("M5 10l2 3z")
[ 5, 10, 7, 13 ]
> svgPathBbox("M5 10c3 0 3 3 0 3z")
Expand All @@ -49,7 +49,7 @@ $ svg-path-bbox "M5 10c3 0 3 3 0 3z" "M2 8m5 5z"
### Typescript usage

```typescript
import svgPathBbox from "svg-path-bbox";
import { svgPathBbox } from "svg-path-bbox";
import type { BBox } from "svg-path-bbox";

const cases: [string, BBox][] = [["M0 0H3V6Z", [0, 0, 3, 6]]];
Expand Down
164 changes: 164 additions & 0 deletions dist/es2015/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
"use strict";
// WARNING: This file is autogenerated, edit lib/index.template.ts
import svgPath from "svgpath";
// Precision for consider cubic polynom as quadratic one
const CBEZIER_MINMAX_EPSILON = 0.00000001;
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L89
function minmaxQ(A) {
const min = Math.min(A[0], A[2]), max = Math.max(A[0], A[2]);
if (A[1] >= A[0] ? A[2] >= A[1] : A[2] <= A[1]) {
// if no extremum in ]0,1[
return [min, max];
}
// check if the extremum E is min or max
const E = (A[0] * A[2] - A[1] * A[1]) / (A[0] - 2 * A[1] + A[2]);
return E < min ? [E, max] : [min, E];
}
// https://github.com/kpym/SVGPathy/blob/acd1a50c626b36d81969f6e98e8602e128ba4302/lib/box.js#L127
function minmaxC(A) {
const K = A[0] - 3 * A[1] + 3 * A[2] - A[3];
// if the polynomial is (almost) quadratic and not cubic
if (Math.abs(K) < CBEZIER_MINMAX_EPSILON) {
if (A[0] === A[3] && A[0] === A[1]) {
// no curve, point targeting same location
return [A[0], A[3]];
}
return minmaxQ([
A[0],
-0.5 * A[0] + 1.5 * A[1],
A[0] - 3 * A[1] + 3 * A[2],
]);
}
// the reduced discriminant of the derivative
const T = -A[0] * A[2] +
A[0] * A[3] -
A[1] * A[2] -
A[1] * A[3] +
A[1] * A[1] +
A[2] * A[2];
// if the polynomial is monotone in [0,1]
if (T <= 0) {
return [Math.min(A[0], A[3]), Math.max(A[0], A[3])];
}
const S = Math.sqrt(T);
// potential extrema
let min = Math.min(A[0], A[3]), max = Math.max(A[0], A[3]);
const L = A[0] - 2 * A[1] + A[2];
// check local extrema
for (let R = (L + S) / K, i = 1; i <= 2; R = (L - S) / K, i++) {
if (R > 0 && R < 1) {
// if the extrema is for R in [0,1]
const Q = A[0] * (1 - R) * (1 - R) * (1 - R) +
A[1] * 3 * (1 - R) * (1 - R) * R +
A[2] * 3 * (1 - R) * R * R +
A[3] * R * R * R;
if (Q < min) {
min = Q;
}
if (Q > max) {
max = Q;
}
}
}
return [min, max];
}
/**
* Compute bounding boxes of SVG paths.
* @param {String} d SVG path for which their bounding box will be computed.
* @returns {BBox}
*/
export function svgPathBbox(d) {
const min = [Infinity, Infinity], max = [-Infinity, -Infinity];
svgPath(d)
.abs()
.unarc()
.unshort()
.iterate((seg, _, x, y) => {
switch (seg[0]) {
// The next cases are ordered based on simple-icons data
case "M": {
if (min[0] > seg[1]) {
min[0] = seg[1];
}
if (min[1] > seg[2]) {
min[1] = seg[2];
}
if (max[0] < seg[1]) {
max[0] = seg[1];
}
if (max[1] < seg[2]) {
max[1] = seg[2];
}
break;
}
case "H": {
if (min[0] > seg[1]) {
min[0] = seg[1];
}
if (max[0] < seg[1]) {
max[0] = seg[1];
}
break;
}
case "V": {
if (min[1] > seg[1]) {
min[1] = seg[1];
}
if (max[1] < seg[1]) {
max[1] = seg[1];
}
break;
}
case "L": {
if (min[0] > seg[1]) {
min[0] = seg[1];
}
if (min[1] > seg[2]) {
min[1] = seg[2];
}
if (max[0] < seg[1]) {
max[0] = seg[1];
}
if (max[1] < seg[2]) {
max[1] = seg[2];
}
break;
}
case "C": {
const cxMinMax = minmaxC([x, seg[1], seg[3], seg[5]]);
if (min[0] > cxMinMax[0]) {
min[0] = cxMinMax[0];
}
if (max[0] < cxMinMax[1]) {
max[0] = cxMinMax[1];
}
const cyMinMax = minmaxC([y, seg[2], seg[4], seg[6]]);
if (min[1] > cyMinMax[0]) {
min[1] = cyMinMax[0];
}
if (max[1] < cyMinMax[1]) {
max[1] = cyMinMax[1];
}
break;
}
case "Q": {
const qxMinMax = minmaxQ([x, seg[1], seg[3]]);
if (min[0] > qxMinMax[0]) {
min[0] = qxMinMax[0];
}
if (max[0] < qxMinMax[1]) {
max[0] = qxMinMax[1];
}
const qyMinMax = minmaxQ([y, seg[2], seg[4]]);
if (min[1] > qyMinMax[0]) {
min[1] = qyMinMax[0];
}
if (max[1] < qyMinMax[1]) {
max[1] = qyMinMax[1];
}
break;
}
}
}, true);
return [min[0], min[1], max[0], max[1]];
}
3 changes: 2 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.svgPathBbox = void 0;
// WARNING: This file is autogenerated, edit lib/index.template.ts
var svgPath = require("svgpath");
// Precision for consider cubic polynom as quadratic one
Expand Down Expand Up @@ -163,4 +164,4 @@ function svgPathBbox(d) {
}, true);
return [min[0], min[1], max[0], max[1]];
}
module.exports = svgPathBbox;
exports.svgPathBbox = svgPathBbox;
2 changes: 0 additions & 2 deletions dist/index.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion examples/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const util = require("util")
const svgPathBbox = require("../dist/index.js");
const { svgPathBbox } = require("../dist/index.js");

const bbox = svgPathBbox("M0 0H3V6Z");
process.stdout.write(`${util.inspect(bbox)}\n`)
2 changes: 1 addition & 1 deletion examples/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { inspect } from "node:util";
import svgPathBbox from "../dist/index.mjs";
import { svgPathBbox } from "../dist/es2015/index.mjs";

const bbox = svgPathBbox("M0 0H3V6Z");
process.stdout.write(`${inspect(bbox)}\n`)
3 changes: 2 additions & 1 deletion examples/typescript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { inspect } from "node:util";
import svgPathBbox from "../src";
import * as process from "node:process";
import { svgPathBbox } from "../src";
import type { BBox } from "../src";

const cases: [string, BBox][] = [["M0 0H3V6Z", [0, 0, 3, 6]]];
Expand Down
2 changes: 1 addition & 1 deletion lib/index.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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 default function svgPathBbox(d: string): BBox {
export function svgPathBbox(d: string): BBox {
const min = [Infinity, Infinity],
max = [-Infinity, -Infinity];
svgPath(d)
Expand Down
Loading

0 comments on commit b086a98

Please sign in to comment.