Constrain a smaller box inside a larger box
npm install constrain-to-box
{width: 100, height: 100})
{width: 40, height: 40, x: 80, y: 30})
{width: 40, height: 40, x: 60, y: 30})
javascript
import constrain from 'constrain-to-box'
const constrainCSS = constrain.config({x: 'left', y: 'top'/, width: 'width', height: 'height'/})
const inner = {width: 50, height: 50, left: 655, top: 655}
const outer = {width: 500, height: 500, left: 200, top: 200}
const result = constrainCSS(outer, inner)
assert.deepEqual(result, {width: 50, height: 50, left: 650, top: 650})
// Returns the original if no changes were made, otherwise a new object
// (does not modify passed in values)
const wasModified = result !== inner
`
$3
I wrote this code for various projects many times, now it's easy!
$3
Mocha
`
npm install
npm test
``