New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

numeric-quantity

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

numeric-quantity - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

bower.json

54

index.js
module.exports = function(qty) {
var finalResult = -1;
var badResult = -1;
var finalResult = badResult;

@@ -58,11 +59,33 @@ // Resolve any unicode vulgar fractions

*/
var re = /^\s*(\d+)(\.\d+|(\s+\d*\s*)?\s*\/\s*\d+)?\s*$/;
var re = /^\s*(\d*)(\.\d+|(\s+\d*\s*)?\s*\/\s*\d+)?\s*$/;
var ar = re.exec(sQty);
// if the regex fails, return -1
if ( ar == null ) {
return finalResult;
// If the regex fails, give up
if (!ar) {
return badResult;
}
// Store the capture groups so we don't have to access the array
// elements over and over
var captureGroup1 = ar[1];
var captureGroup2 = ar[2];
// The regex can pass and still capture nothing in the relevant groups,
// which means it failed for our purposes
if (!captureGroup1 && !captureGroup2) {
return badResult;
}
// Numerify capture group 1
if (!captureGroup1 && captureGroup2 && captureGroup2.search(/^\./) !== -1) {
finalResult = 0;
} else {
finalResult = parseInt( captureGroup1 );
}
if(isNaN(finalResult)) {
return badResult;
}
var fractionArray;

@@ -72,22 +95,19 @@ var numerator = 0;

// Numerify capture section [1]
finalResult = parseInt( ar[1] );
// If capture section [2] is null, then we're dealing with an integer
// If capture group 2 is null, then we're dealing with an integer
// and there is nothing left to process
if ( ar[2] == null ) {
if ( !captureGroup2 ) {
return finalResult;
}
if ( ar[2].search(/^\./) !== -1 ) {
if ( captureGroup2.search(/^\./) !== -1 ) {
// If first char is "." it's a decimal so just trim to 3 decimal places
numerator = parseFloat( ar[2] );
numerator = parseFloat( captureGroup2 );
finalResult += Math.round(numerator * 1000) / 1000;
} else if ( ar[2].search(/^\s*\//) != -1 ) {
} else if ( captureGroup2.search(/^\s*\//) !== -1 ) {
// If the first non-space char is "/" it's a pure fraction (e.g. "1/2")
numerator = parseInt( ar[1] );
denominator = parseInt( ar[2].replace("/", "") );
numerator = parseInt( captureGroup1 );
denominator = parseInt( captureGroup2.replace("/", "") );
finalResult = Math.round((numerator * 1000) / denominator) / 1000;

@@ -98,3 +118,3 @@

// Otherwise it's a mixed fraction (e.g. "1 2/3")
fractionArray = ar[2].split("/");
fractionArray = captureGroup2.split("/");
numerator = parseInt( fractionArray[0] );

@@ -107,2 +127,2 @@ denominator = parseInt( fractionArray[1] );

return finalResult;
}
};
{
"name": "numeric-quantity",
"version": "0.1.2",
"version": "0.2.0",
"description": "Convert integer plus fraction into a decimal value",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -11,3 +11,3 @@ /**

this.attempt = attempt;
};
}

@@ -22,3 +22,3 @@ Tester.prototype.is = function(test) {

console.log(!!passes ? "pass" : "FAIL: '" + this.attempt + "' is not '" + test + "'");
}
};

@@ -30,17 +30,21 @@ function assert(attempt) {

var badResult = -1;
// Text
assert(nq("NaN")).is(-1);
assert(nq("")).is(-1);
assert(nq("NaN")).is(badResult);
assert(nq("NaN.25")).is(badResult);
assert(nq("NaN 1/4")).is(badResult);
assert(nq("")).is(badResult);
// Invalid numbers
assert(nq("/1")).is(-1);
assert(nq("/0")).is(-1);
assert(nq("/0.5")).is(-1);
assert(nq(".9")).is(-1);
assert(nq("0.0.0")).is(-1);
assert(nq("/1")).is(badResult);
assert(nq("/0")).is(badResult);
assert(nq("/0.5")).is(badResult);
assert(nq("0.0.0")).is(badResult);
// Whole numbers
assert(nq("1")).is(1);
// TODO?: don't allow leading zeroes on whole numbers
// assert(nq("010")).is(-1);
// assert(nq("010")).is(badResult);
assert(nq("100")).is(100);
// Decimals
assert(nq(".9")).is(0.9);
assert(nq("1.1")).is(1.1);

@@ -96,2 +100,2 @@ // Halves

process.exit(testCount - passCount ? 1 : 0);
process.exit(testCount - passCount ? 1 : 0);
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