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.0.1 to 0.1.0

122

lib/type-checker.js

@@ -5,3 +5,10 @@ 'use strict';

const isArray = require('lodash.isarray');
const isString = require('lodash.isstring');
const has = require('lodash.has');
const find = require('lodash.find');
const toNumber = require('lodash.tonumber');
const toString = require('lodash.tostring');
const bson = require('bson');
const MinKey = bson.MinKey;
const MaxKey = bson.MaxKey;

@@ -28,3 +35,79 @@ /**

function toDate(object) {
return new Date(object);
}
function toMinKey(object) {
return new MinKey();
}
function toMaxKey(object) {
return new MaxKey();
}
function toUndefined(object) {
return undefined;
}
function toNull(object) {
return null;
}
function toBoolean(object) {
if (object.toLowerCase() === 'true') {
return true;
}
return false;
}
function toObject(object) {
if (isPlainObject(object)) {
return object;
}
return {};
}
function toArray(object) {
if (isArray(object)) {
return object;
}
return [ object ];
}
/**
* The functions to cast to a type.
*/
const CASTERS = {
'Number': toNumber,
'Date': toDate,
'MinKey': toMinKey,
'MaxKey': toMaxKey,
'Undefined': toUndefined,
'Null': toNull,
'Boolean': toBoolean,
'String': toString,
'Object': toObject,
'Array': toArray
}
class Test {
constructor(regex, types) {
this.regex = regex;
this.types = types;
}
};
/**
* The various string tests.
*/
const STRING_TESTS = [
new Test(/^$/, [ 'String', 'MinKey', 'MaxKey', 'Object', 'Array' ]),
new Test(/^-?\d+$/, [ 'String', 'Number', 'Object', 'Array' ]),
new Test(/^-?(\d*\.)?\d+$/, [ 'String', 'Number', 'Object', 'Array' ]),
new Test(/^(null)$/, [ 'String', 'Null', 'Object', 'Array' ]),
new Test(/^(undefined)$/, [ 'String', 'Undefined', 'Object', 'Array' ]),
new Test(/^(true|false)$/, [ 'String', 'Boolean', 'Object', 'Array' ])
];
/**
* Checks the types of objects and returns them as readable strings.

@@ -35,2 +118,19 @@ */

/**
* 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 Object]' ? '' : result;
}
/**
* Get the type for the object.

@@ -54,4 +154,26 @@ *

}
/**
* Get a list of types the object can be cast to.
*
* @param {Object} - The object.
*
* @returns {Array} The available types.
*/
castableTypes(object) {
if (isString(object)) {
return this._stringTypes(object);
} else {
return [ this.type(object), 'String', 'Object', 'Array' ];
}
}
_stringTypes(string) {
var passing = find(STRING_TESTS, (test) => {
return test.regex.test(string);
});
return passing ? passing.types : [ 'String', 'Object', 'Array' ];
}
}
module.exports = new TypeChecker();

12

package.json

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

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

@@ -26,5 +26,13 @@ "type": "git",

"dependencies": {
"bson": "^0.4.23",
"lodash.find": "^4.4.0",
"lodash.foreach": "^4.3.0",
"lodash.has": "^4.4.0",
"lodash.isarray": "^4.0.0",
"lodash.isplainobject": "^4.0.4"
"lodash.isplainobject": "^4.0.4",
"lodash.isstring": "^4.0.1",
"lodash.toarray": "^4.2.4",
"lodash.tonumber": "^4.0.2",
"lodash.toplainobject": "^4.0.4",
"lodash.tostring": "^4.1.3"
},

@@ -31,0 +39,0 @@ "devDependencies": {

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