hadron-type-checker
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -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(); |
@@ -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": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15843
155
11
166
+ Addedbson@^0.4.23
+ Addedlodash.find@^4.4.0
+ Addedlodash.foreach@^4.3.0
+ Addedlodash.isstring@^4.0.1
+ Addedlodash.toarray@^4.2.4
+ Addedlodash.tonumber@^4.0.2
+ Addedlodash.toplainobject@^4.0.4
+ Addedlodash.tostring@^4.1.3
+ Addedbson@0.4.23(transitive)
+ Addedlodash.find@4.6.0(transitive)
+ Addedlodash.foreach@4.5.0(transitive)
+ Addedlodash.isstring@4.0.1(transitive)
+ Addedlodash.toarray@4.4.0(transitive)
+ Addedlodash.tonumber@4.0.3(transitive)
+ Addedlodash.toplainobject@4.2.0(transitive)
+ Addedlodash.tostring@4.1.4(transitive)