Skip to content

Commit

Permalink
feat: initial version
Browse files Browse the repository at this point in the history
first version done! 100% coverage
  • Loading branch information
Tyler Schroeder committed Jan 26, 2020
0 parents commit 14dc2d1
Show file tree
Hide file tree
Showing 24 changed files with 16,358 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#root = true

[*]
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100
indent_size = 2

[*.md]
trim_trailing_whitespace = false
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules
coverage
.nyc_output
.DS_Store
*.log
.vscode
.idea
dist
compiled
.awcache
.rpt2_cache
docs
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js

node_js:
- lts/*
script: npm run test:prod && npm run build
notifications:
email: false
node_js:
- '10'
before_script:
- git version
after_success:
- npm run report-coverage
- npm run semantic-release
- npm run deploy-docs
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2017 Tyler Schroeder <tschroeder@surgeforward.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# guillotine-packer

[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![Travis](https://img.shields.io/travis/tyschroed/guillotine-packer)](https://travis-ci.org/tyschroed/guillotine-packer)
[![Coverage Status](https://coveralls.io/repos/github/tyschroed/guillotine-packer/badge.svg?branch=master)](https://coveralls.io/github/tyschroed/guillotine-packer?branch=master)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

## What is this?

This is a simple little utility to parse free text dimensions (i.e. 2ft, 1 1/2in, 6", 3cm) into their numerical values. It supports imperial and metric values, including shorthand equivalents. It even handles the pesky curly quotes that newer releases of iOS love to use!

## Getting Started

### Install

```bash
npm install guillotine-packer
```

### Usage

```javascript
import { parseDimension, units } from 'guillotine-packer'

parseDimension(`3ft`)
// -> 36

parseDimension(`1' 6"`)
// -> 18

// specify units output value should be in
parseDimension(`24"`, { outputUnits: units.ft })
// -> 2

// specify the assumed units if none are provided
parseDimension(`2`, { defaultUnits: units.ft })
// -> 24
```

### Options

The second parameter is an optional options object, with the following properties:

| Option | Default | Description |
| ------------ | -------- | ---------------------------------------------------- |
| defaultUnits | units.in | If no units provided, parser will assume these units |
| outputUnits | units.in | Dimensions that output will be converted to |

For both options, valid values are one of:

```typescript
export enum units {
in = 'in',
ft = 'ft',
mm = 'mm',
m = 'm',
cm = 'cm'
}
```

:beers:
247 changes: 247 additions & 0 deletions __tests__/guillotine-packer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
import { packer, SortStrategy, SplitStrategy, SelectionStrategy } from '../src/guillotine-packer'
import { Item } from '../src/types'

test('pack item requiring rotation', () => {
const items = [
{
name: 'test',
width: 30,
height: 40
} as Item
]

const result = packer({
binHeight: 30,
binWidth: 40,
items
})

expect(result).toMatchInlineSnapshot(`
Array [
Array [
Object {
"bin": 1,
"height": 30,
"item": Object {
"name": "test",
},
"width": 40,
"x": 0,
"y": 0,
},
],
]
`)
})

test('pack a single', () => {
const items = [
{
name: 'test',
width: 20,
height: 20
} as Item,
{
name: 'test2',
width: 15,
height: 5
} as Item
]

const result = packer({
binHeight: 30,
binWidth: 40,
items
})
expect(result).toMatchInlineSnapshot(`
Array [
Array [
Object {
"bin": 1,
"height": 5,
"item": Object {
"name": "test2",
},
"width": 15,
"x": 0,
"y": 0,
},
Object {
"bin": 1,
"height": 20,
"item": Object {
"name": "test",
},
"width": 20,
"x": 0,
"y": 5,
},
],
]
`)
})

test('should rotate items if it results in more efficent packing', () => {
const result = packer(
{
binHeight: 40,
binWidth: 80,
items: [
{
name: '40x20',
width: 40,
height: 20
} as Item,
{
name: '40x20',
width: 40,
height: 20
} as Item
]
},
{
kerfSize: 2,
sortStrategy: SortStrategy.Area,
splitStrategy: SplitStrategy.ShortAxisSplit,
selectionStrategy: SelectionStrategy.BEST_AREA_FIT
}
)
expect(result).toHaveLength(1)
})

test('should not rotate items if allow rotation is disabled', () => {
const result = packer(
{
binHeight: 40,
binWidth: 80,
items: [
{
name: '40x20',
width: 40,
height: 20
} as Item,
{
name: '40x20',
width: 40,
height: 20
} as Item
]
},
{
kerfSize: 2,
sortStrategy: SortStrategy.Area,
splitStrategy: SplitStrategy.ShortAxisSplit,
selectionStrategy: SelectionStrategy.BEST_AREA_FIT,
allowRotation: false
}
)
expect(result).toHaveLength(2)
})

test('pack two', () => {
const result = packer({
binHeight: 30,
binWidth: 30,
items: [
{
name: 'test2',
width: 20,
height: 20
} as Item,
{
name: 'test',
width: 20,
height: 20
} as Item
]
})
expect(result).toMatchInlineSnapshot(`
Array [
Array [
Object {
"bin": 1,
"height": 20,
"item": Object {
"name": "test2",
},
"width": 20,
"x": 0,
"y": 0,
},
Object {
"bin": 1,
"height": 20,
"item": Object {
"name": "test",
},
"width": 20,
"x": 20,
"y": 0,
},
],
]
`)
})

test('create kerfs if provided', () => {
const result = packer(
{
binHeight: 30,
binWidth: 30,
items: [
{
name: 'test',
width: 20,
height: 20
} as Item,
{
name: 'kerfed offcut',
width: 5,
height: 5
} as Item
]
},
{ kerfSize: 2 }
)
expect(result).toMatchInlineSnapshot(`
Array [
Array [
Object {
"bin": 1,
"height": 5,
"item": Object {
"name": "kerfed offcut",
},
"width": 5,
"x": 0,
"y": 0,
},
Object {
"bin": 1,
"height": 20,
"item": Object {
"name": "test",
},
"width": 20,
"x": 0,
"y": 7,
},
],
]
`)
})

test('throw error if item too large for bin', () => {
const invalidItem = () =>
packer({
binHeight: 30,
binWidth: 30,
items: [
{
width: 40,
height: 40
}
]
})
expect(invalidItem).toThrowError('exceeds bin dimensions')
})
Loading

0 comments on commit 14dc2d1

Please sign in to comment.