Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mzdunek93 committed Apr 22, 2020
0 parents commit 919a7cc
Show file tree
Hide file tree
Showing 25 changed files with 26,701 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
node_modules/**
19 changes: 19 additions & 0 deletions .eslintrc
@@ -0,0 +1,19 @@
{
"ignorePatterns": ["node_modules/", "*.js"],
"plugins": ["@typescript-eslint", "prettier", "tsdoc"],
"extends": [
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint"
],
"rules": {
"no-console": ["warn", { "allow": ["error"] }],
"object-shorthand": ["error", "always"],
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"eol-last": ["error", "always"],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"prettier/prettier": ["error"],
"tsdoc/syntax": "warn"
}
}
14 changes: 14 additions & 0 deletions .gitignore
@@ -0,0 +1,14 @@
# Logs
.DS_Store
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

coverage
.nyc_output
node_modules/

dist/
tmp/
14 changes: 14 additions & 0 deletions .npmignore
@@ -0,0 +1,14 @@
yarn-error.log*

coverage
.nyc_output

api/
config/
src/
test/
tmp/
.eslint*
.prettier*
ava.config.js
tsconfig.json
6 changes: 6 additions & 0 deletions .prettierrc
@@ -0,0 +1,6 @@
{
"printWidth": 100,
"singleQuote": true,
"semi": false,
"trailingComma": "none"
}
13 changes: 13 additions & 0 deletions LICENSE
@@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
85 changes: 85 additions & 0 deletions README.md
@@ -0,0 +1,85 @@
# svg-autocrop

A tool for cutting out redundant transparent space from SVG images. It returns the original SVG without any changes besides a `viewBox` attribute updated/added to be as small as possible to fit all of the opaque content.

## Functions

### autocrop

Automatically crops the SVG's viewBox so it doesn't contain redundant transparent content. For performance reasons, it's much preferable to batch requests by passing in an array of images rather than making many frequent requests with single ones. That way all of the SVGs in the array are put on the same page and screenshotted at the same time.

**Overloads:**
<details>
<summary><code class="language-typescript">(input: string, options?: <a href="#Options">Options</a>): Promise&lt;string&gt;</code></summary>

```typescript
async function autocrop(input: string, options?: Options): Promise<string>
```

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
`input` | string | A string with the input SVG |
`options?` | [Options](#Options) | Settings for the function |

**Returns:** A promise containing the SVG with the viewBox cropped
</details>

<details>
<summary><code class="language-typescript">(input: string[], options?: <a href="#Options">Options</a>): Promise&lt;string[]&gt;</code></summary>

```typescript
async function autocrop(input: string[], options?: Options): Promise<string[]>
```

**Parameters:**

Name | Type | Description |
------ | ------ | ------ |
`input` | string[] | A string array with the input SVGs |
`options?` | [Options](#Options) | Settings for the function |

**Returns:** A promise containing the SVGs with the viewBoxes cropped
</details>

___

### startBrowser

Starts the Chrome instance. Used to achieve extra performance by keeping a single browser instance instead of creating a new one for each request. The browser will keep running until the [closeBrowser](#closebrowser) function is invoked or the process emits the `exit` event. In some environments like Jest invoking [closeBrowser](#closebrowser) might be necessary to prevent memory leaks as the process doesn't close the browser on its own when exiting.

```typescript
const startBrowser: () => Promise<ChildProcess>
```

**Returns:** A promise containing the instance of the spawned child process

___

### closeBrowser

Closes the Chrome instance that was started using the [startBrowser](#startbrowser) function.

```typescript
const closeBrowser: () => void
```

## Inferfaces

### Options

Options for the main [autocrop](#autocrop) function

```typescript
interface Options {
size?: number
scale?: number
}
```

**Properties:**

• **scale** - How much to scale the viewbox after cropping. Used to prevent opaque parts of the image from being cut and/or to put extra whitespace around it. *Default:* `1.05`

• **size** - Height and width of a single bitmap in pixels. The higher the size the better the precision, but the slower the performance. *Default:* `100`
34 changes: 34 additions & 0 deletions api/svg-autocrop.api.md
@@ -0,0 +1,34 @@
## API Report File for "@mzdunek/svg-autocrop"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts

import { ChildProcess } from 'child_process';

// @beta
function autocrop(input: string[], options?: Options): Promise<string[]>;

// @beta
function autocrop(input: string, options?: Options): Promise<string>;

export { autocrop }

export default autocrop;

// @beta
export function closeBrowser(): void;

// @beta
export interface Options {
scale?: number;
size?: number;
}

// @beta
export function startBrowser(): Promise<ChildProcess>;


// (No @packageDocumentation comment for this package)

```
8 changes: 8 additions & 0 deletions ava.config.js
@@ -0,0 +1,8 @@
export default {
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
}

0 comments on commit 919a7cc

Please sign in to comment.