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

@hugsmidjan/getclevercropbox

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hugsmidjan/getclevercropbox - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

src/getCleverCropBox-tests-core.json

8

CHANGELOG.md

@@ -7,2 +7,10 @@ # Change Log

## 1.2.0
_2019-10-05_
- feat: Silently relax/remove all `isInt()` checks for inputs
- fix: Correct function mis-naming in development log message
- chore: Remove unused file cruft from package - improve build scripts
## 1.1.0

@@ -9,0 +17,0 @@

141

dist/getCleverCropBox.js

@@ -5,54 +5,99 @@ 'use strict';

var isNum = function (v) { return typeof v === 'number'; };
var isInt = function (v) { return isNum(v) && Math.round(v) === v; };
var isNumOrNull = function (v) { return v == null || typeof v === 'number'; };
// const isNullOrBool = (v) => v == null || typeof v === 'number';
// NOTE: we don't really care about integer checking
var isInt = isNum; // (v) => isNum(v) && Math.round(v) === v;
var isIntOrNull = isNumOrNull; // (v) => v == null || isInt(v);
// NOTE: `isValidTypes()` is not really needed for applications written in TypeScript.
// eslint-disable-next-line complexity
var isValid = function (name, o) {
var ret = false;
if (name === 'origSize') {
ret /*true &&*/ =
isInt(o.width) && o.width > 0 && isInt(o.height) && o.height > 0 && true;
} else if (name === 'cropInfo') {
ret /*true &&*/ =
(o.width || o.height) &&
(o.width == null || (isInt(o.width) && o.width > 0)) &&
(o.height == null || (isInt(o.height) && o.height > 0)) &&
(o.zoom == null || (isInt(o.zoom) && o.zoom >= 100 && o.zoom < 1000)) &&
//(o.quality==null || (isInt(o.quality) && o.quality>=0 && o.quality<=100)) &&
// one or none should be defined (We may use ! becuase they're all > 0)
!!o.minRatioX === !!o.minRatioY &&
!!o.maxRatioX === !!o.maxRatioY &&
!!o.minRatioX === !!o.maxRatioX &&
(o.minRatioX == null || (isNum(o.minRatioX) && o.minRatioX > 0)) &&
(o.minRatioY == null || (isNum(o.minRatioY) && o.minRatioY > 0)) &&
(o.maxRatioX == null || (isNum(o.maxRatioX) && o.maxRatioX > 0)) &&
(o.maxRatioY == null || (isNum(o.maxRatioY) && o.maxRatioY > 0)) &&
(!o.minRatioX || o.minRatioX / o.minRatioY <= o.maxRatioX / o.maxRatioY) &&
(o.snapTo == null || typeof o.snapTo === 'boolean') &&
true;
} else if (name === 'focalPoint') {
ret /*true &&*/ =
isInt(o.fx) &&
o.fx >= 0 &&
o.fx <= 100 &&
isInt(o.fy) &&
o.fy >= 0 &&
o.fy <= 100 &&
// one or none should be defined
(o.r1x != null) === (o.r1y != null) &&
(o.r2x != null) === (o.r2y != null) &&
(o.r1x != null) === (o.r2x != null) &&
(o.r1x == null || (isInt(o.r1x) && o.r1x >= 0 && o.r1x <= 100)) &&
(o.r1y == null || (isInt(o.r1y) && o.r1y >= 0 && o.r1y <= 100)) &&
(o.r2x == null || (isInt(o.r2x) && o.r2x >= 0 && o.r2x <= 100)) &&
(o.r2y == null || (isInt(o.r2y) && o.r2y >= 0 && o.r2y <= 100)) &&
(o.r1x == null ||
(o.fx >= o.r1x && o.fx <= o.r2x && o.fy >= o.r1y && o.fy <= o.r2y)) &&
true;
var isValidTypes = function (o, c, f) {
var valid = true;
if (!(isInt(o.width) && isInt(o.height))) {
valid = false;
}
return ret;
if (
!(
isIntOrNull(c.width) &&
isIntOrNull(c.height) &&
isNumOrNull(c.zoom) &&
// all or none should be defined
((!c.minRatioX && !c.minRatioY && !c.maxRatioX && !c.maxRatioY) ||
(isNum(c.minRatioX) &&
isNum(c.minRatioY) &&
isNum(c.maxRatioX) &&
isNum(c.maxRatioY)))
)
) {
valid = false;
}
if (
f &&
!(
isNum(f.fx) &&
isNum(f.fy) &&
// all or none should be defined
((!f.r1x && !f.r1y && !f.r2x && !f.r2y) ||
(isNum(f.r1x) && isNum(f.r1y) && isNum(f.r2x) && isNum(f.r2y)))
)
) {
valid = false;
}
return valid;
};
// eslint-disable-next-line complexity
var isValidValues = function (o, c, f) {
var valid = true;
if (!(o.width > 0 && o.height > 0)) {
valid = false;
}
if (
!(
(c.width || c.height) &&
(c.width == null || c.width > 0) &&
(c.height == null || c.height > 0) &&
(c.zoom == null || (c.zoom >= 100 && c.zoom < 1000)) &&
//(c.quality==null || (isNum(c.quality) && c.quality>=0 && c.quality<=100)) &&
// all or none are defined
(!c.minRatioX || // NOTE: We may use ! becuase they're all > 0
(c.minRatioX > 0 &&
c.minRatioY > 0 &&
c.maxRatioX > 0 &&
c.maxRatioY > 0 &&
// minRatio is smaller than maxRatio
c.minRatioX / c.minRatioY <= c.maxRatioX / c.maxRatioY))
)
) {
valid = false;
}
if (
f &&
!(
f.fx >= 0 &&
f.fx <= 100 &&
f.fy >= 0 &&
f.fy <= 100 &&
// all or none are defined
(f.r1x == null ||
(f.r1x >= 0 &&
f.r1x <= 100 &&
f.r1y >= 0 &&
f.r1y <= 100 &&
f.r2x >= 0 &&
f.r2x <= 100 &&
f.r2y >= 0 &&
f.r2y <= 100 &&
// Focus point is within the focus rectangle
(f.fx >= f.r1x && f.fx <= f.r2x && f.fy >= f.r1y && f.fy <= f.r2y)))
)
) {
valid = false;
}
return valid;
};
// ***************************************

@@ -87,7 +132,5 @@ // The juicy stuff:

var getCleverCropBox = function (origSize, cropInfo, focalPoint) {
var invalid;
if (
(!isValid('origSize', origSize) && (invalid = origSize)) ||
(!isValid('cropInfo', cropInfo) && (invalid = cropInfo)) ||
(focalPoint && !isValid('focalPoint', focalPoint) && (invalid = focalPoint))
!isValidTypes(origSize, cropInfo, focalPoint) ||
!isValidValues(origSize, cropInfo, focalPoint)
) {

@@ -94,0 +137,0 @@ return null;

{
"name": "@hugsmidjan/getclevercropbox",
"version": "1.1.0",
"main": "dist/getCleverCropBox.js",
"module": "src/getCleverCropBox.js",
"types": "src/getCleverCropBox.d.ts",
"version": "1.2.0",
"main": "dist/getCleverCropBox.js",
"module": "src/getCleverCropBox.js",
"types": "src/getCleverCropBox.d.ts",
"repository": "git@github.com:hugsmidjan/getCleverCropBox.git",
"author": "Hugsmiðjan ehf. (www.hugsmidjan.is)",
"license": "UNLICENCED",
"scripts": {
"prepublish": "yarn run build",
"build": "NODE_ENV=production gulp build && yarn run test",
"dev": "gulp dev",
"test": "ospec 'tests/**/*.js'",
"license": "UNLICENCED",
"scripts": {
"prepublish": "yarn run build",
"build": "NODE_ENV=production gulp build && yarn run test",
"dev": "gulp dev",
"test": "ospec 'tests/**/*.js'",
"test:dev": "yarn run test && onchange 'tests/**' -f add,change -- yarn run test"
},
},
"devDependencies": {
"@hugsmidjan/gulp-rollup": "^0.3.12",
"del": "^5.1.0",
"gulp": "^4.0.2",

@@ -20,0 +21,0 @@ "onchange": "^6.1.0",

/* global process */
const isDev = process.env.NODE_ENV === 'development';
const isNum = (v) => typeof v === 'number';
const isInt = (v) => isNum(v) && Math.round(v) === v;
const isNumOrNull = (v) => v == null || typeof v === 'number';
// const isNullOrBool = (v) => v == null || typeof v === 'number';
// NOTE: we don't really care about integer checking
const isInt = isNum; // (v) => isNum(v) && Math.round(v) === v;
const isIntOrNull = isNumOrNull; // (v) => v == null || isInt(v);
const logInvalid = isDev
? (name, object) =>
console.error('Invalid getCleverCropBox() input `' + name + '`', object)
: () => {};
// NOTE: `isValidTypes()` is not really needed for applications written in TypeScript.
// eslint-disable-next-line complexity
const isValid = (name, o) => {
let ret = false;
if (name === 'origSize') {
ret /*true &&*/ =
isInt(o.width) && o.width > 0 && isInt(o.height) && o.height > 0 && true;
} else if (name === 'cropInfo') {
ret /*true &&*/ =
(o.width || o.height) &&
(o.width == null || (isInt(o.width) && o.width > 0)) &&
(o.height == null || (isInt(o.height) && o.height > 0)) &&
(o.zoom == null || (isInt(o.zoom) && o.zoom >= 100 && o.zoom < 1000)) &&
//(o.quality==null || (isInt(o.quality) && o.quality>=0 && o.quality<=100)) &&
// one or none should be defined (We may use ! becuase they're all > 0)
!!o.minRatioX === !!o.minRatioY &&
!!o.maxRatioX === !!o.maxRatioY &&
!!o.minRatioX === !!o.maxRatioX &&
(o.minRatioX == null || (isNum(o.minRatioX) && o.minRatioX > 0)) &&
(o.minRatioY == null || (isNum(o.minRatioY) && o.minRatioY > 0)) &&
(o.maxRatioX == null || (isNum(o.maxRatioX) && o.maxRatioX > 0)) &&
(o.maxRatioY == null || (isNum(o.maxRatioY) && o.maxRatioY > 0)) &&
(!o.minRatioX || o.minRatioX / o.minRatioY <= o.maxRatioX / o.maxRatioY) &&
(o.snapTo == null || typeof o.snapTo === 'boolean') &&
true;
} else if (name === 'focalPoint') {
ret /*true &&*/ =
isInt(o.fx) &&
o.fx >= 0 &&
o.fx <= 100 &&
isInt(o.fy) &&
o.fy >= 0 &&
o.fy <= 100 &&
// one or none should be defined
(o.r1x != null) === (o.r1y != null) &&
(o.r2x != null) === (o.r2y != null) &&
(o.r1x != null) === (o.r2x != null) &&
(o.r1x == null || (isInt(o.r1x) && o.r1x >= 0 && o.r1x <= 100)) &&
(o.r1y == null || (isInt(o.r1y) && o.r1y >= 0 && o.r1y <= 100)) &&
(o.r2x == null || (isInt(o.r2x) && o.r2x >= 0 && o.r2x <= 100)) &&
(o.r2y == null || (isInt(o.r2y) && o.r2y >= 0 && o.r2y <= 100)) &&
(o.r1x == null ||
(o.fx >= o.r1x && o.fx <= o.r2x && o.fy >= o.r1y && o.fy <= o.r2y)) &&
true;
const isValidTypes = (o, c, f) => {
let valid = true;
if (!(isInt(o.width) && isInt(o.height))) {
valid = false;
isDev && logInvalid('origSize (type)', o);
}
return ret;
if (
!(
isIntOrNull(c.width) &&
isIntOrNull(c.height) &&
isNumOrNull(c.zoom) &&
// all or none should be defined
((!c.minRatioX && !c.minRatioY && !c.maxRatioX && !c.maxRatioY) ||
(isNum(c.minRatioX) &&
isNum(c.minRatioY) &&
isNum(c.maxRatioX) &&
isNum(c.maxRatioY)))
)
) {
valid = false;
isDev && logInvalid('cropInfo (type)', c);
}
if (
f &&
!(
isNum(f.fx) &&
isNum(f.fy) &&
// all or none should be defined
((!f.r1x && !f.r1y && !f.r2x && !f.r2y) ||
(isNum(f.r1x) && isNum(f.r1y) && isNum(f.r2x) && isNum(f.r2y)))
)
) {
valid = false;
isDev && logInvalid('focalPoint (type)', f);
}
return valid;
};
// eslint-disable-next-line complexity
const isValidValues = (o, c, f) => {
let valid = true;
if (!(o.width > 0 && o.height > 0)) {
valid = false;
isDev && logInvalid('origSize', o);
}
if (
!(
(c.width || c.height) &&
(c.width == null || c.width > 0) &&
(c.height == null || c.height > 0) &&
(c.zoom == null || (c.zoom >= 100 && c.zoom < 1000)) &&
//(c.quality==null || (isNum(c.quality) && c.quality>=0 && c.quality<=100)) &&
// all or none are defined
(!c.minRatioX || // NOTE: We may use ! becuase they're all > 0
(c.minRatioX > 0 &&
c.minRatioY > 0 &&
c.maxRatioX > 0 &&
c.maxRatioY > 0 &&
// minRatio is smaller than maxRatio
c.minRatioX / c.minRatioY <= c.maxRatioX / c.maxRatioY))
)
) {
valid = false;
isDev && logInvalid('cropInfo', c);
}
if (
f &&
!(
f.fx >= 0 &&
f.fx <= 100 &&
f.fy >= 0 &&
f.fy <= 100 &&
// all or none are defined
(f.r1x == null ||
(f.r1x >= 0 &&
f.r1x <= 100 &&
f.r1y >= 0 &&
f.r1y <= 100 &&
f.r2x >= 0 &&
f.r2x <= 100 &&
f.r2y >= 0 &&
f.r2y <= 100 &&
// Focus point is within the focus rectangle
(f.fx >= f.r1x && f.fx <= f.r2x && f.fy >= f.r1y && f.fy <= f.r2y)))
)
) {
valid = false;
isDev && logInvalid('focalPoint', f);
}
return valid;
};
// ***************************************

@@ -85,9 +141,6 @@ // The juicy stuff:

const getCleverCropBox = (origSize, cropInfo, focalPoint) => {
let invalid;
if (
(!isValid('origSize', origSize) && (invalid = origSize)) ||
(!isValid('cropInfo', cropInfo) && (invalid = cropInfo)) ||
(focalPoint && !isValid('focalPoint', focalPoint) && (invalid = focalPoint))
!isValidTypes(origSize, cropInfo, focalPoint) ||
!isValidValues(origSize, cropInfo, focalPoint)
) {
isDev && console.info('invalid determineCropBox params', invalid);
return null;

@@ -94,0 +147,0 @@ }

# TODO
- Consider relaxing input validation when NODE_ENV==='production'
- Write README file

@@ -4,0 +5,0 @@ - Convert to TypeScript and autogenerate the `.d.ts` file

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