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

hadron-type-checker

Package Overview
Dependencies
Maintainers
1
Versions
497
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hadron-type-checker - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

108

lib/type-checker.js

@@ -6,2 +6,3 @@ 'use strict';

const isString = require('lodash.isstring');
const isNumber = require('lodash.isnumber');
const has = require('lodash.has');

@@ -14,2 +15,3 @@ const find = require('lodash.find');

const MaxKey = bson.MaxKey;
const Long = bson.Long;

@@ -36,2 +38,22 @@ /**

/**
* The max int 32 value.
*/
const BSON_INT32_MAX = 0x7FFFFFFF;
/**
* The min int 32 value.
*/
const BSON_INT32_MIN = -0x80000000;
/**
* The max long value.
*/
const JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000);
/**
* The min long value.
*/
const JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000);
function toDate(object) {

@@ -41,15 +63,15 @@ return new Date(object);

function toMinKey(object) {
function toMinKey() {
return new MinKey();
}
function toMaxKey(object) {
function toMaxKey() {
return new MaxKey();
}
function toUndefined(object) {
function toUndefined() {
return undefined;
}
function toNull(object) {
function toNull() {
return null;

@@ -83,3 +105,5 @@ }

const CASTERS = {
'Number': toNumber,
'Int32': toNumber,
'Int64': toNumber,
'Double': toNumber,
'Date': toDate,

@@ -94,4 +118,7 @@ 'MinKey': toMinKey,

'Array': toArray
}
};
/**
* A test that returns the types is passing.
*/
class Test {

@@ -102,7 +129,36 @@ constructor(tester, types) {

}
};
}
/**
* Checks if a string is an int32.
*/
class Int32Check {
test(string) {
if (/^-?\d+$/.test(string)) {
var value = toNumber(string);
return value >= BSON_INT32_MIN && value <= BSON_INT32_MAX;
}
return false;
}
}
/**
* Checks if a string is an int64.
*/
class Int64Check {
test(string) {
if (/^-?\d+$/.test(string)) {
var value = toNumber(string);
return value >= JS_INT_MIN_LONG && value <= JS_INT_MAX_LONG;
}
return false;
}
}
/**
* Checks if a string is a date.
*/
class DateCheck {
test(string) {
var date = Date.parse(string)
var date = Date.parse(string);
return date ? true : false;

@@ -112,2 +168,6 @@ }

const INT32_CHECK = new Int32Check();
const INT64_CHECK = new Int64Check();
const DATE_CHECK = new DateCheck();
/**

@@ -117,5 +177,6 @@ * The various string tests.

const STRING_TESTS = [
new Test(/^$/, [ 'String', 'Null', 'Undefined', 'MinKey', 'MaxKey', 'Object', 'Array' ]),
new Test(/^-?\d+$/, [ 'String', 'Number', 'Object', 'Array' ]),
new Test(/^-?(\d*\.)?\d+$/, [ 'String', 'Number', 'Object', 'Array' ]),
new Test(/^$/, [ 'String', 'Null', 'Undefined', 'MinKey', 'MaxKey', 'Object', 'Array' ]),
new Test(INT32_CHECK, [ 'String', 'Int32', 'Object', 'Array' ]),
new Test(INT64_CHECK, [ 'String', 'Int64', 'Object', 'Array' ]),
new Test(/^-?(\d*\.)?\d+$/, [ 'String', 'Double', 'Object', 'Array' ]),
new Test(/^(null)$/, [ 'String', 'Null', 'Object', 'Array' ]),

@@ -125,6 +186,23 @@ new Test(/^(undefined)$/, [ 'String', 'Undefined', 'Object', 'Array' ]),

new Test(/^\/(.*)\/$/, [ 'String', 'BSONRegExp', 'Object', 'Array' ]),
new Test(new DateCheck(), [ 'String', 'Date', 'Object', 'Array' ])
new Test(DATE_CHECK, [ 'String', 'Date', 'Object', 'Array' ])
];
/**
* Gets the BSON type for a JS number.
*
* @param {Number} number - The number.
*
* @returns {String} The BSON type.
*/
function numberToBsonType(number) {
var string = toString(number);
if (INT32_CHECK.test(string)) {
return 'Int32';
} else if (INT64_CHECK.test(string)) {
return 'Int64';
}
return 'Double';
}
/**
* Checks the types of objects and returns them as readable strings.

@@ -159,2 +237,5 @@ */

type(object) {
if (isNumber(object)) {
return numberToBsonType(object);
}
if (isPlainObject(object)) {

@@ -182,5 +263,4 @@ return OBJECT;

return this._stringTypes(object);
} else {
return [ this.type(object), 'String', 'Object', 'Array' ];
}
return [ this.type(object), 'String', 'Object', 'Array' ];
}

@@ -187,0 +267,0 @@

7

package.json

@@ -7,3 +7,3 @@ {

"homepage": "https://github.com/mongodb-js/hadron-type-checker",
"version": "0.3.0",
"version": "0.4.0",
"repository": {

@@ -20,3 +20,4 @@ "type": "git",

"fmt": "mongodb-js-fmt ./*.js ./test/*.js",
"test": "mocha --recursive"
"test": "mocha --recursive",
"check": "mongodb-js-precommit"
},

@@ -29,5 +30,5 @@ "precommit": [

"lodash.find": "^4.4.0",
"lodash.foreach": "^4.3.0",
"lodash.has": "^4.4.0",
"lodash.isarray": "^4.0.0",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.4",

@@ -34,0 +35,0 @@ "lodash.isstring": "^4.0.1",

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