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

hadron-type-checker

Package Overview
Dependencies
Maintainers
31
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 4.0.3 to 4.0.4

.eslintignore

341

lib/type-checker.js
'use strict';
const isPlainObject = require('lodash.isplainobject');
const isArray = require('lodash.isarray');
const isString = require('lodash.isstring');
const isNumber = require('lodash.isnumber');
const has = require('lodash.has');
const keys = require('lodash.keys');
const without = require('lodash.without');
const toNumber = require('lodash.tonumber');
const toString = require('lodash.tostring');
const includes = require('lodash.includes');
const bson = require('bson');
const MinKey = bson.MinKey;
const MaxKey = bson.MaxKey;
const Long = bson.Long;
const Double = bson.Double;
const Int32 = bson.Int32;
const Decimal128 = bson.Decimal128;
const Binary = bson.Binary;
const BSONRegExp = bson.BSONRegExp;
const Code = bson.Code;
const Symbol = bson.Symbol;
const Timestamp = bson.Timestamp;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var isPlainObject = require('lodash.isplainobject');
var isArray = require('lodash.isarray');
var isString = require('lodash.isstring');
var isNumber = require('lodash.isnumber');
var has = require('lodash.has');
var keys = require('lodash.keys');
var without = require('lodash.without');
var toNumber = require('lodash.tonumber');
var toString = require('lodash.tostring');
var includes = require('lodash.includes');
var bson = require('bson');
var MinKey = bson.MinKey;
var MaxKey = bson.MaxKey;
var Long = bson.Long;
var Double = bson.Double;
var Int32 = bson.Int32;
var Decimal128 = bson.Decimal128;
var Binary = bson.Binary;
var BSONRegExp = bson.BSONRegExp;
var Code = bson.Code;
var _Symbol = bson.Symbol;
var Timestamp = bson.Timestamp;
/**
* The object string.
*/
const OBJECT = 'Object';
var OBJECT = 'Object';

@@ -34,3 +38,3 @@ /**

*/
const ARRAY = 'Array';
var ARRAY = 'Array';

@@ -40,3 +44,3 @@ /**

*/
const TRUE = 'true';
var TRUE = 'true';

@@ -46,3 +50,3 @@ /**

*/
const FALSE = 'false';
var FALSE = 'false';

@@ -52,11 +56,11 @@ /**

*/
const LONG = 'Long';
var LONG = 'Long';
const INT_32 = 'Int32';
const INT_64 = 'Int64';
const DOUBLE = 'Double';
const DECIMAL_128 = 'Decimal128';
const OBJECT_TYPE = '[object Object]';
const EMPTY = '';
const OBJECT_ID = 'ObjectID';
var INT_32 = 'Int32';
var INT_64 = 'Int64';
var DOUBLE = 'Double';
var DECIMAL_128 = 'Decimal128';
var OBJECT_TYPE = '[object Object]';
var EMPTY = '';
var OBJECT_ID = 'ObjectID';

@@ -66,3 +70,3 @@ /**

*/
const BSON_TYPE = '_bsontype';
var BSON_TYPE = '_bsontype';

@@ -72,3 +76,3 @@ /**

*/
const MATCH = /\[object (\w+)\]/;
var MATCH = /\[object (\w+)\]/;

@@ -78,3 +82,3 @@ /**

*/
const BSON_INT32_MAX = 0x7FFFFFFF;
var BSON_INT32_MAX = 0x7FFFFFFF;

@@ -84,6 +88,6 @@ /**

*/
const BSON_INT32_MIN = -0x80000000;
var BSON_INT32_MIN = -0x80000000;
const BSON_INT64_MAX = Math.pow(2, 63) - 1;
const BSON_INT64_MIN = -(BSON_INT64_MAX);
var BSON_INT64_MAX = Math.pow(2, 63) - 1;
var BSON_INT64_MIN = -BSON_INT64_MAX;

@@ -93,3 +97,3 @@ /**

*/
const NUMBER_REGEX = /^-?\d+$/;
var NUMBER_REGEX = /^-?\d+$/;

@@ -99,30 +103,25 @@ /**

*/
const NUMBER_TYPES = [
'Long',
'Int32',
'Double',
'Decimal128'
];
var NUMBER_TYPES = ['Long', 'Int32', 'Double', 'Decimal128'];
const toDate = (object) => {
var toDate = function toDate(object) {
return new Date(object);
};
const toMinKey = () => {
var toMinKey = function toMinKey() {
return new MinKey();
};
const toMaxKey = () => {
var toMaxKey = function toMaxKey() {
return new MaxKey();
};
const toUndefined = () => {
var toUndefined = function toUndefined() {
return undefined;
};
const toNull = () => {
var toNull = function toNull() {
return null;
};
const toBoolean = (object) => {
var toBoolean = function toBoolean(object) {
if (isString(object)) {

@@ -134,3 +133,3 @@ if (object.toLowerCase() === TRUE) {

}
throw new Error(`'${object}' is not a valid boolean string`);
throw new Error('\'' + object + '\' is not a valid boolean string');
}

@@ -143,3 +142,3 @@ if (object) {

const toObject = (object) => {
var toObject = function toObject(object) {
if (isPlainObject(object)) {

@@ -151,3 +150,3 @@ return object;

const toArray = (object) => {
var toArray = function toArray(object) {
if (isArray(object)) {

@@ -159,30 +158,30 @@ return object;

}
return [ object ];
return [object];
};
const toInt32 = (object) => {
var toInt32 = function toInt32(object) {
if (object === '-' || object === '') {
throw new Error(`Value '${object}' is not a valid Int32 value`);
throw new Error('Value \'' + object + '\' is not a valid Int32 value');
}
const number = toNumber(object);
var number = toNumber(object);
if (number >= BSON_INT32_MIN && number <= BSON_INT32_MAX) {
return new Int32(number);
}
throw new Error(`Value ${number} is outside the valid Int32 range`);
throw new Error('Value ' + number + ' is outside the valid Int32 range');
};
const toInt64 = (object) => {
var toInt64 = function toInt64(object) {
if (object === '-' || object === '') {
throw new Error(`Value '${object}' is not a valid Int64 value`);
throw new Error('Value \'' + object + '\' is not a valid Int64 value');
}
const number = toNumber(object);
var number = toNumber(object);
if (number >= BSON_INT64_MIN && number <= BSON_INT64_MAX) {
return Long.fromNumber(number);
}
throw new Error(`Value ${number} is outside the valid Int64 range`);
throw new Error('Value ' + number + ' is outside the valid Int64 range');
};
const toDouble = (object) => {
var toDouble = function toDouble(object) {
if (object === '-' || object === '') {
throw new Error(`Value '${object}' is not a valid Double value`);
throw new Error('Value \'' + object + '\' is not a valid Double value');
}

@@ -192,7 +191,7 @@ if (isString(object) && object.endsWith('.')) {

}
const number = toNumber(object);
var number = toNumber(object);
return new Double(number);
};
const toDecimal128 = (object) => {
var toDecimal128 = function toDecimal128(object) {
/*

@@ -207,3 +206,3 @@ If converting a BSON Object, extract the value before converting to a string.

const toObjectID = (object) => {
var toObjectID = function toObjectID(object) {
if (!isString(object) || object === '') {

@@ -215,20 +214,20 @@ return new bson.ObjectID();

const toBinary = (object) => {
var toBinary = function toBinary(object) {
return new Binary(String(object), Binary.SUBTYPE_DEFAULT);
};
const toRegex = (object) => {
var toRegex = function toRegex(object) {
return new BSONRegExp(String(object));
};
const toCode = (object) => {
var toCode = function toCode(object) {
return new Code(String(object), {});
};
const toSymbol = (object) => {
return new Symbol(String(object));
var toSymbol = function toSymbol(object) {
return new _Symbol(String(object));
};
const toTimestamp = (object) => {
const number = toNumber(object);
var toTimestamp = function toTimestamp(object) {
var number = toNumber(object);
return Timestamp.fromNumber(number);

@@ -240,3 +239,3 @@ };

*/
const CASTERS = {
var CASTERS = {
'Array': toArray,

@@ -266,3 +265,3 @@ 'Binary': toBinary,

*/
const TYPES = keys(CASTERS);
var TYPES = keys(CASTERS);

@@ -272,27 +271,48 @@ /**

*/
class Int32Check {
test(string) {
if (NUMBER_REGEX.test(string)) {
var value = toNumber(string);
return value >= BSON_INT32_MIN && value <= BSON_INT32_MAX;
}
return false;
var Int32Check = function () {
function Int32Check() {
_classCallCheck(this, Int32Check);
}
}
_createClass(Int32Check, [{
key: 'test',
value: function test(string) {
if (NUMBER_REGEX.test(string)) {
var value = toNumber(string);
return value >= BSON_INT32_MIN && value <= BSON_INT32_MAX;
}
return false;
}
}]);
return Int32Check;
}();
/**
* Checks if a string is an int64.
*/
class Int64Check {
test(string) {
if (NUMBER_REGEX.test(string)) {
return Number.isSafeInteger(toNumber(string));
}
return false;
var Int64Check = function () {
function Int64Check() {
_classCallCheck(this, Int64Check);
}
}
const INT32_CHECK = new Int32Check();
const INT64_CHECK = new Int64Check();
_createClass(Int64Check, [{
key: 'test',
value: function test(string) {
if (NUMBER_REGEX.test(string)) {
return Number.isSafeInteger(toNumber(string));
}
return false;
}
}]);
return Int64Check;
}();
var INT32_CHECK = new Int32Check();
var INT64_CHECK = new Int64Check();
/**

@@ -305,3 +325,3 @@ * Gets the BSON type for a JS number.

*/
const numberToBsonType = (number) => {
var numberToBsonType = function numberToBsonType(number) {
var string = toString(number);

@@ -319,65 +339,84 @@ if (INT32_CHECK.test(string)) {

*/
class TypeChecker {
/**
* Cast the provided object to the desired type.
*
* @param {Object} object - The object to cast.
* @param {String} type - The type.
*
* @returns {Object} The cast object.
*/
cast(object, type) {
var caster = CASTERS[type];
var result = object;
if (caster) {
result = caster(object);
}
return result === OBJECT_TYPE ? EMPTY : result;
var TypeChecker = function () {
function TypeChecker() {
_classCallCheck(this, TypeChecker);
}
/**
* Get the type for the object.
*
* @param {Object} object - The object.
*
* @returns {String} The object type.
*/
type(object) {
if (isNumber(object)) {
return numberToBsonType(object);
_createClass(TypeChecker, [{
key: 'cast',
/**
* Cast the provided object to the desired type.
*
* @param {Object} object - The object to cast.
* @param {String} type - The type.
*
* @returns {Object} The cast object.
*/
value: function cast(object, type) {
var caster = CASTERS[type];
var result = object;
if (caster) {
result = caster(object);
}
return result === OBJECT_TYPE ? EMPTY : result;
}
if (isPlainObject(object)) {
return OBJECT;
}
if (isArray(object)) {
return ARRAY;
}
if (has(object, BSON_TYPE)) {
if (object._bsontype === LONG) {
return INT_64;
/**
* Get the type for the object.
*
* @param {Object} object - The object.
*
* @returns {String} The object type.
*/
}, {
key: 'type',
value: function type(object) {
if (isNumber(object)) {
return numberToBsonType(object);
}
if (object._bsontype === OBJECT_ID) {
return 'ObjectId';
if (isPlainObject(object)) {
return OBJECT;
}
return object._bsontype;
if (isArray(object)) {
return ARRAY;
}
if (has(object, BSON_TYPE)) {
if (object._bsontype === LONG) {
return INT_64;
}
if (object._bsontype === OBJECT_ID) {
return 'ObjectId';
}
return object._bsontype;
}
return Object.prototype.toString.call(object).replace(MATCH, '$1');
}
return Object.prototype.toString.call(object).replace(MATCH, '$1');
}
/**
* Get a list of types the object can be cast to.
*
* @param {Boolean} highPrecisionSupport - If Decimal128 is supported or not.
*
* @returns {Array} The available types.
*/
castableTypes(highPrecisionSupport = false) {
if (highPrecisionSupport === true) {
return TYPES;
/**
* Get a list of types the object can be cast to.
*
* @param {Boolean} highPrecisionSupport - If Decimal128 is supported or not.
*
* @returns {Array} The available types.
*/
}, {
key: 'castableTypes',
value: function castableTypes() {
var highPrecisionSupport = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
if (highPrecisionSupport === true) {
return TYPES;
}
return without(TYPES, DECIMAL_128);
}
return without(TYPES, DECIMAL_128);
}
}
}]);
module.exports = new TypeChecker();
return TypeChecker;
}();
module.exports = new TypeChecker();

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

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

@@ -21,3 +21,5 @@ "type": "git",

"test": "mocha --recursive",
"check": "mongodb-js-precommit"
"check": "mongodb-js-precommit",
"prepublish": "babel ./src --out-dir ./lib",
"pretest": "babel ./src --out-dir ./lib"
},

@@ -28,2 +30,5 @@ "precommit": [

"dependencies": {
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.5.0",
"babel-register": "^6.5.2",
"bson": "^1.0.1",

@@ -43,2 +48,3 @@ "lodash.has": "^4.4.0",

"devDependencies": {
"babel-cli": "^6.9.0",
"chai": "^3.4.1",

@@ -45,0 +51,0 @@ "eslint-config-mongodb-js": "^2.2.0",

Sorry, the diff of this file is not supported yet

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