Comparing version 0.0.1 to 0.0.2
122
lib/index.js
const $check = require("check-types"); | ||
const base64regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/; | ||
// /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/; | ||
const $moment = require('moment'); | ||
const $numeral = require('numeral'); | ||
const conversionMatrix = { | ||
const _conversionMatrix= { | ||
"string": { | ||
@@ -211,3 +209,3 @@ "string": { | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return forceToString(val, format);} | ||
convert: function (val, format) {return TypeMagic.forceToString(val, format);} | ||
}, | ||
@@ -230,3 +228,3 @@ "number": { | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return forceToString(val, format);} | ||
convert: function (val, format) {return TypeMagic.forceToString(val, format);} | ||
} | ||
@@ -241,3 +239,3 @@ }, | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return forceToString(val, format);} | ||
convert: function (val, format) {return TypeMagic.forceToString(val, format);} | ||
} | ||
@@ -247,34 +245,68 @@ } | ||
module.exports = { | ||
convert: convert, | ||
validate: validate, | ||
getTypeName: getTypeName, | ||
forceToString: forceToString, | ||
check:$check | ||
}; | ||
function convert(val, toType, format) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
class TypeMagic{ | ||
static convert(val, toType, format) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
} | ||
let valType = TypeMagic.getTypeName(val); | ||
if (!isValidCast(valType, toType)) { | ||
throw new Error("Cannot convert " + valType + " to " + toType); | ||
} | ||
return _conversionMatrix[valType][toType].convert(val, format); | ||
} | ||
static validate(val, toType, format) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
} | ||
let valType = getTypeName(val); | ||
if (!isValidCast(valType, toType)) { | ||
throw new Error("Cannot convert " + valType + " to " + toType); | ||
let valType = getTypeName(val); | ||
if (!isValidCast(valType, toType)) { | ||
return false; | ||
} | ||
return _conversionMatrix[valType][toType].validate(val, format); | ||
} | ||
return conversionMatrix[valType][toType].convert(val, format); | ||
} | ||
function validate(val, toType, format) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
static forceToString(val, format){ | ||
return forceToString(val,format); | ||
} | ||
let valType = getTypeName(val); | ||
if (!isValidCast(valType, toType)) { | ||
return false; | ||
static getTypeName(val) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
} | ||
if ($check.string(val)) { | ||
return "string"; | ||
} | ||
if ($check.integer(val)) { | ||
return "integer"; | ||
} | ||
if ($check.number(val)) { | ||
return "number"; | ||
} | ||
if ($check.date(val)) { | ||
return "date"; | ||
} | ||
if ($check.boolean(val)) { | ||
return "boolean"; | ||
} | ||
if ($check.array(val)) { | ||
return "array"; | ||
} | ||
if ($check.object(val)) { | ||
return "object"; | ||
} | ||
return (typeof val); | ||
} | ||
return conversionMatrix[valType][toType].validate(val, format); | ||
static get check(){ | ||
return $check; | ||
} | ||
} | ||
//********************************************************************************************************************** | ||
@@ -285,3 +317,3 @@ //Private Functions | ||
function isValidCast(fromType, toType) { | ||
return (conversionMatrix[fromType] && conversionMatrix[fromType][toType]); | ||
return (_conversionMatrix[fromType] && _conversionMatrix[fromType][toType]); | ||
} | ||
@@ -317,4 +349,4 @@ | ||
} else if ($check.array(val)) { | ||
return val.map(function (doc) { | ||
return forceToString(doc,format); | ||
return val.map( (doc) =>{ | ||
return TypeMagic.forceToString(doc,format); | ||
}).join(","); | ||
@@ -329,28 +361,2 @@ } else if ($check.object(val)) { | ||
function getTypeName(val) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
} | ||
if ($check.string(val)) { | ||
return "string"; | ||
} | ||
if ($check.integer(val)) { | ||
return "integer"; | ||
} | ||
if ($check.number(val)) { | ||
return "number"; | ||
} | ||
if ($check.date(val)) { | ||
return "date"; | ||
} | ||
if ($check.boolean(val)) { | ||
return "boolean"; | ||
} | ||
if ($check.array(val)) { | ||
return "array"; | ||
} | ||
if ($check.object(val)) { | ||
return "object"; | ||
} | ||
return (typeof val); | ||
} | ||
module.exports=TypeMagic; |
{ | ||
"name": "type-magic", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Utilities for checking and converting types", | ||
@@ -10,4 +10,4 @@ "author": { | ||
"dependencies": { | ||
"check-types": "7.3.0", | ||
"moment": "2.21.0", | ||
"check-types": "8.0.2", | ||
"moment": "2.24.0", | ||
"numeral": "2.0.6" | ||
@@ -14,0 +14,0 @@ }, |
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
11403
4
335
+ Addedcheck-types@8.0.2(transitive)
+ Addedmoment@2.24.0(transitive)
- Removedcheck-types@7.3.0(transitive)
- Removedmoment@2.21.0(transitive)
Updatedcheck-types@8.0.2
Updatedmoment@2.24.0