Comparing version 0.0.2 to 0.1.0
@@ -1,2 +0,1 @@ | ||
module.exports=require('./lib') | ||
module.exports = require('./lib'); |
436
lib/index.js
@@ -1,21 +0,27 @@ | ||
const $check = require("check-types"); | ||
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= { | ||
"string": { | ||
"string": { | ||
convert: function (val) {return val;}, | ||
validate: function (val) {return true;} | ||
const conversionMatrix = { | ||
string: { | ||
string: { | ||
convert: function (val) { | ||
return val; | ||
}, | ||
validate: function (val) { | ||
return true; | ||
}, | ||
}, | ||
"boolean": { | ||
boolean: { | ||
validate: function (val) { | ||
switch (val.toLowerCase().trim()) { | ||
case "true": | ||
case "yes": | ||
case "1": | ||
case 'true': | ||
case 'yes': | ||
case '1': | ||
return true; | ||
case "false": | ||
case "no": | ||
case "0": | ||
case 'false': | ||
case 'no': | ||
case '0': | ||
case null: | ||
@@ -29,31 +35,31 @@ return true; | ||
switch (val.toLowerCase().trim()) { | ||
case "true": | ||
case "yes": | ||
case "1": | ||
case 'true': | ||
case 'yes': | ||
case '1': | ||
return true; | ||
case "false": | ||
case "no": | ||
case "0": | ||
case 'false': | ||
case 'no': | ||
case '0': | ||
case null: | ||
return false; | ||
default: | ||
throw new Error("Cannot convert string to boolean"); | ||
throw new Error('Cannot convert string to boolean'); | ||
} | ||
} | ||
}, | ||
}, | ||
"date": { | ||
date: { | ||
validate: function (val, format) { | ||
let d = $moment(val, format); | ||
const d = $moment(val, format); | ||
return d.isValid(); | ||
}, | ||
convert: function (val, format) { | ||
let d = $moment.utc(val, format); | ||
const d = $moment.utc(val, format); | ||
if (!d.isValid()) { | ||
throw new Error("Cannot convert string to date"); | ||
throw new Error('Cannot convert string to date'); | ||
} else { | ||
return d.toDate(); | ||
} | ||
} | ||
}, | ||
}, | ||
"number": { | ||
number: { | ||
validate: function (val, format) { | ||
@@ -63,11 +69,11 @@ return !isNaN($numeral(val)); | ||
convert: function (val) { | ||
let v = $numeral(val); | ||
if (v.value()!==null) { | ||
const v = $numeral(val); | ||
if (v.value() !== null) { | ||
return v.value(); | ||
} else { | ||
throw new Error("Cannot convert string to number"); | ||
throw new Error('Cannot convert string to number'); | ||
} | ||
} | ||
}, | ||
}, | ||
"integer": { | ||
integer: { | ||
validate: function (val, format) { | ||
@@ -77,11 +83,11 @@ return !isNaN($numeral(val)); | ||
convert: function (val, format) { | ||
let v = $numeral(val); | ||
if (v.value()!==null) { | ||
const v = $numeral(val); | ||
if (v.value() !== null) { | ||
return Math.trunc(v.value()); | ||
} else { | ||
throw new Error("Cannot convert string to number"); | ||
throw new Error('Cannot convert string to number'); | ||
} | ||
} | ||
}, | ||
}, | ||
"array": { | ||
array: { | ||
validate: function (val) { | ||
@@ -91,6 +97,6 @@ return true; | ||
convert: function (val) { | ||
return val.split(","); | ||
} | ||
return val.split(','); | ||
}, | ||
}, | ||
"object": { | ||
object: { | ||
validate: function (val) { | ||
@@ -100,4 +106,3 @@ try { | ||
return true; | ||
} | ||
catch (exp) { | ||
} catch (exp) { | ||
return false; | ||
@@ -109,9 +114,8 @@ } | ||
return JSON.parse(val); | ||
} catch (exp) { | ||
throw new Error('Cannot convert string to object'); | ||
} | ||
catch (exp) { | ||
throw new Error("Cannot convert string to object"); | ||
} | ||
} | ||
}, | ||
}, | ||
"base64string": { | ||
base64string: { | ||
convert: function (val) { | ||
@@ -122,51 +126,91 @@ return Buffer.from(val).toString('base64'); | ||
return true; | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
"boolean": { | ||
"boolean": { | ||
convert: function (val) {return val;}, | ||
validate: function (val) {return true;} | ||
boolean: { | ||
boolean: { | ||
convert: function (val) { | ||
return val; | ||
}, | ||
validate: function (val) { | ||
return true; | ||
}, | ||
}, | ||
"string": { | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return forceToString(val, format);} | ||
string: { | ||
validate: function (val) { | ||
return true; | ||
}, | ||
convert: function (val, format) { | ||
return forceToString(val, format); | ||
}, | ||
}, | ||
"integer": { | ||
convert: function (val) {return val === true ? 1 : 0;}, | ||
validate: function (val) {return true;} | ||
integer: { | ||
convert: function (val) { | ||
return val === true ? 1 : 0; | ||
}, | ||
validate: function (val) { | ||
return true; | ||
}, | ||
}, | ||
"number": { | ||
convert: function (val) {return val === true ? 1.0 : 0.0;}, | ||
validate: function (val) {return true;} | ||
} | ||
number: { | ||
convert: function (val) { | ||
return val === true ? 1.0 : 0.0; | ||
}, | ||
validate: function (val) { | ||
return true; | ||
}, | ||
}, | ||
}, | ||
"date": { | ||
"date": { | ||
convert: function (val) {return val;}, | ||
validate: function (val) {return true;} | ||
date: { | ||
date: { | ||
convert: function (val) { | ||
return val; | ||
}, | ||
validate: function (val) { | ||
return true; | ||
}, | ||
}, | ||
"string": { | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return forceToString(val, format);} | ||
string: { | ||
validate: function (val) { | ||
return true; | ||
}, | ||
convert: function (val, format) { | ||
return forceToString(val, format); | ||
}, | ||
}, | ||
"integer": { | ||
validate: function (val) {return true;}, | ||
convert: function (val) {return val.getTime();} | ||
integer: { | ||
validate: function (val) { | ||
return true; | ||
}, | ||
convert: function (val) { | ||
return val.getTime(); | ||
}, | ||
}, | ||
"number": { | ||
validate: function (val) {return true;}, | ||
convert: function (val) {return val.getTime();} | ||
} | ||
number: { | ||
validate: function (val) { | ||
return true; | ||
}, | ||
convert: function (val) { | ||
return val.getTime(); | ||
}, | ||
}, | ||
}, | ||
"integer": { | ||
"integer": { | ||
convert: function (val) {return val;}, | ||
validate: function (val) {return true;} | ||
integer: { | ||
integer: { | ||
convert: function (val) { | ||
return val; | ||
}, | ||
validate: function (val) { | ||
return true; | ||
}, | ||
}, | ||
"string": { | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return forceToString(val, format);} | ||
string: { | ||
validate: function (val) { | ||
return true; | ||
}, | ||
convert: function (val, format) { | ||
return forceToString(val, format); | ||
}, | ||
}, | ||
"boolean": { | ||
boolean: { | ||
validate: function (val) { | ||
@@ -185,8 +229,7 @@ if (val === 0 || val === 1) { | ||
} else { | ||
throw new Error("Cannot convert integer to boolean"); | ||
throw new Error('Cannot convert integer to boolean'); | ||
} | ||
} | ||
}, | ||
}, | ||
"date": { | ||
date: { | ||
validate: function (val) { | ||
@@ -197,5 +240,5 @@ return true; | ||
return new Date(val); | ||
} | ||
}, | ||
}, | ||
"number": { | ||
number: { | ||
validate: function (val) { | ||
@@ -206,17 +249,23 @@ return true; | ||
return Number(val); | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
"number": { | ||
"integer": { | ||
convert: function (val) {return Math.trunc(val);}, | ||
number: { | ||
integer: { | ||
convert: function (val) { | ||
return Math.trunc(val); | ||
}, | ||
validate: function (val) { | ||
return true; | ||
} | ||
}, | ||
}, | ||
"string": { | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return TypeMagic.forceToString(val, format);} | ||
string: { | ||
validate: function (val) { | ||
return true; | ||
}, | ||
convert: function (val, format) { | ||
return forceToString(val, format); | ||
}, | ||
}, | ||
"number": { | ||
number: { | ||
validate: function (val) { | ||
@@ -227,99 +276,81 @@ return true; | ||
return val; | ||
} | ||
} | ||
}, | ||
}, | ||
}, | ||
"object": { | ||
"object": { | ||
convert: function (val) {return val;}, | ||
validate: function (val) {return true;} | ||
object: { | ||
object: { | ||
convert: function (val) { | ||
return val; | ||
}, | ||
validate: function (val) { | ||
return true; | ||
}, | ||
}, | ||
"string": { | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return TypeMagic.forceToString(val, format);} | ||
} | ||
string: { | ||
validate: function (val) { | ||
return true; | ||
}, | ||
convert: function (val, format) { | ||
return forceToString(val, format); | ||
}, | ||
}, | ||
}, | ||
"array": { | ||
"array": { | ||
convert: function (val) {return val;}, | ||
validate: function (val) {return true;} | ||
array: { | ||
array: { | ||
convert: function (val) { | ||
return val; | ||
}, | ||
validate: function (val) { | ||
return true; | ||
}, | ||
}, | ||
"string": { | ||
validate: function (val) {return true;}, | ||
convert: function (val, format) {return TypeMagic.forceToString(val, format);} | ||
} | ||
} | ||
string: { | ||
validate: function (val) { | ||
return true; | ||
}, | ||
convert: function (val, format) { | ||
return forceToString(val, format); | ||
}, | ||
}, | ||
}, | ||
}; | ||
module.exports = { | ||
convert: convert, | ||
validate: validate, | ||
getTypeName: getTypeName, | ||
forceToString: forceToString, | ||
check: $check, | ||
}; | ||
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); | ||
function convert(val, toType, format) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
} | ||
static validate(val, toType, format) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
} | ||
let valType = getTypeName(val); | ||
if (!isValidCast(valType, toType)) { | ||
return false; | ||
} | ||
return _conversionMatrix[valType][toType].validate(val, format); | ||
const valType = getTypeName(val); | ||
if (!isValidCast(valType, toType)) { | ||
throw new Error('Cannot convert ' + valType + ' to ' + toType); | ||
} | ||
return conversionMatrix[valType][toType].convert(val, format); | ||
} | ||
static forceToString(val, format){ | ||
return forceToString(val,format); | ||
function validate(val, toType, format) { | ||
if (!$check.assigned(val)) { | ||
return null; | ||
} | ||
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); | ||
const valType = getTypeName(val); | ||
if (!isValidCast(valType, toType)) { | ||
return false; | ||
} | ||
static get check(){ | ||
return $check; | ||
} | ||
return conversionMatrix[valType][toType].validate(val, format); | ||
} | ||
// ********************************************************************************************************************** | ||
// Private Functions | ||
// ********************************************************************************************************************** | ||
//********************************************************************************************************************** | ||
//Private Functions | ||
//********************************************************************************************************************** | ||
function isValidCast(fromType, toType) { | ||
return (_conversionMatrix[fromType] && _conversionMatrix[fromType][toType]); | ||
return conversionMatrix[fromType] && conversionMatrix[fromType][toType]; | ||
} | ||
@@ -338,10 +369,8 @@ | ||
} | ||
} else if ($check.date(val)) { | ||
if (format){ | ||
if (format) { | ||
return $moment(val).format(format); | ||
}else{ | ||
} else { | ||
return val.toISOString(); | ||
} | ||
} else if ($check.integer(val)) { | ||
@@ -356,13 +385,40 @@ if (format) { | ||
} else if ($check.array(val)) { | ||
return val.map( (doc) =>{ | ||
return TypeMagic.forceToString(doc,format); | ||
}).join(","); | ||
return val | ||
.map(function (doc) { | ||
return forceToString(doc, format); | ||
}) | ||
.join(','); | ||
} else if ($check.object(val)) { | ||
return JSON.stringify(val); | ||
} else { | ||
throw new Error("Unable to force type:" + (typeof val) + " to string"); | ||
throw new Error('Unable to force type:' + typeof val + ' to string'); | ||
} | ||
} | ||
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.2", | ||
"description": "Utilities for checking and converting types", | ||
"version": "0.1.0", | ||
"description": "Utilities for checking and converting javascript types", | ||
"main": "index.js", | ||
"files": [ | ||
"lib/", | ||
"index.js" | ||
], | ||
"scripts": { | ||
"lint": "eslint .", | ||
"prettier": "prettier . --write", | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
"test-ci": "nyc --reporter text-summary mocha -- --reporter spec --check-leaks test/", | ||
"test-cov": "nyc --reporter lcov --reporter text mocha -- --reporter dot --check-leaks test/" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/synatic/type-magic.git" | ||
}, | ||
"keywords": [], | ||
"author": { | ||
"name": "Martin Naude", | ||
"email": "martin@filepounder.com" | ||
"name": "FilePounder Inc", | ||
"url": "https://synatic.com" | ||
}, | ||
"dependencies": { | ||
"check-types": "8.0.2", | ||
"moment": "2.24.0", | ||
"numeral": "2.0.6" | ||
"contributors": [ | ||
{ | ||
"name": "Martin Naude", | ||
"email": "martin@synatic.com" | ||
}, | ||
{ | ||
"name": "Thiren Bunsee", | ||
"email": "thiren@synatic.com" | ||
} | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/synatic/type-magic/issues" | ||
}, | ||
"homepage": "https://github.com/synatic/type-magic#readme", | ||
"engines": { | ||
"node": ">=6.0.0", | ||
"npm": ">=3.0.0" | ||
"node": ">=12.0.0", | ||
"npm": ">=6.0.0" | ||
}, | ||
"homepage": "https://github.com/filepounder/type-magic", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/filepounder/type-magic.git" | ||
"dependencies": { | ||
"check-types": "11.1.2", | ||
"moment": "2.29.1", | ||
"numeral": "2.0.6" | ||
}, | ||
"devDependencies": { | ||
"mocha": "5.0.1" | ||
"eslint": "^7.15.0", | ||
"eslint-config-google": "^0.14.0", | ||
"eslint-config-prettier": "^7.0.0", | ||
"mocha": "^8.2.1", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.2.1" | ||
} | ||
} |
# Type Magic | ||
##Utilities to check and convert types |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
13827
5
402
0
4
6
1
1
+ Addedcheck-types@11.1.2(transitive)
+ Addedmoment@2.29.1(transitive)
- Removedcheck-types@8.0.2(transitive)
- Removedmoment@2.24.0(transitive)
Updatedcheck-types@11.1.2
Updatedmoment@2.29.1