Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

adjust-engine

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adjust-engine - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

lib/expression.js

7

History.md
2.0.0 / 2015-08-24
==================
* BREAKING: changed offset calculation to move object, but not affect the height or width
* better defaults
* supports flipping offsets.
1.0.1 / 2015-08-19

@@ -3,0 +10,0 @@ ==================

78

index.js

@@ -5,3 +5,5 @@ /**

var expression = require('./lib/expression')
var assign = require('object-assign')
var mirror = require('./lib/mirror')

@@ -36,8 +38,7 @@ /**

options.attachment = options.attachment || 'center middle'
options.target = options.target || options.attachment
options.offset = options.offset || {}
var attachment = expression(options.attachment)
var offset = assign(offsets, options.offset)
var target = expression(options.target)
var offset = assign({}, offsets, options.offset)
var target = options.target ? expression(options.target) : mirror(attachment)

@@ -58,8 +59,6 @@ return function adjust(attachment_position, target_position, viewport_position) {

// update the position with the offsets
var left = target_position.left + offset_x + offset.left
var top = target_position.top + offset_y + offset.top
var height = height - offset.top - offset.bottom
var width = width - offset.left - offset.right
var bottom = top + height + offset.bottom
var right = left + width + offset.right
var left = target_position.left + offset_x + offset.left - offset.right
var top = target_position.top + offset_y + offset.top - offset.bottom
var bottom = top + height + offset.bottom - offset.top
var right = left + width + offset.right - offset.left

@@ -71,8 +70,10 @@ // check if we need to flip

if (left < viewport_position.left && target_position.right + width <= viewport_position.right) {
left = target_position.right
right = left + width
// flip right
left = target_position.right + offset.right - offset.left
right = left + width + offset.left - offset.right
} else
if (right > viewport_position.right && target_position.left - width >= viewport_position.left) {
right = target_position.left
left = right - width
// flip left
right = target_position.left + offset.left - offset.right
left = right - width + offset.right - offset.left
}

@@ -83,8 +84,10 @@

if (top < viewport_position.top && target_position.bottom + height <= viewport_position.bottom) {
top = target_position.bottom
bottom = top + height
// flip bottom
top = target_position.bottom + offset.bottom - offset.top
bottom = top + height + offset.top - offset.bottom
} else
if (bottom > viewport_position.bottom && target_position.top - height >= viewport_position.top) {
bottom = target_position.top
top = bottom - height
// flip top
bottom = target_position.top + offset.top - offset.bottom
top = bottom - height + offset.bottom - offset.top
}

@@ -103,42 +106,1 @@ }

}
/**
* Parse the expression
*
* @param {String} expr
* @return {Object}
*/
function expression(expr) {
var tokens = expr.split(/\s+/)
var out = {}
tokens.forEach(function(token, i) {
switch (token) {
case 'center': return out.x = 0.5
case 'middle': return out.y = 0.5
case 'bottom': return out.y = 1
case 'right': return out.x = 1
case 'left': return out.x = 0
case 'top': return out.y = 0
default:
return i % 2
? out.y = percentage(token)
: out.x = percentage(token)
}
})
return out
}
/**
* To percentage
*
* @param {String} val
* @return {Number}
*/
function percentage (val) {
var float = parseFloat(val)
return isNaN(float) ? 0 : float / 100
}
{
"name": "adjust-engine",
"version": "1.0.1",
"version": "2.0.0",
"description": "adjust an element relative to another element",

@@ -5,0 +5,0 @@ "keywords": [

# adjust-engine
Position an element relative to another element. This repository is just
the calculation engine behind the adjustments.
Position an element relative to another element. This repository is the workhorse behind [adjust](https://github.com/matthewmueller/adjust).

@@ -7,0 +6,0 @@ ## Installation

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc