Skip to content
/ mapTypes Public

NodeJS Module for iterating over an Object to replace key(String) with key(ObjectType)

License

Notifications You must be signed in to change notification settings

Vonor/mapTypes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

mapTypes

When converting yaml to JavaScript objects you can't specify object types, as they turn into Strings. For example, the following yaml code turns into this JavaScript object.

type: String
{
    'type': 'String'
}

But we would expect something like this:

{
    'type': String
}

This module maps the following object definitions into the corresponding objects.

{
    "string": String,
    "object": Object,
    "number": Number,
    "array": Array,
    "boolean": Boolean,
    "bool": Boolean
}

Usage

npm i vonor/mapTypes
const mapTypes = require('mapTypes');

const schemaData = {
    'someDeclaration': {
        'type': 'Number'
    }
}
console.log(typeof schemaData.someDeclaration.type) // => String
let schema = mapTypes(schemaData, 'type')
console.log(typeof schemaData.someDeclaration.type) // => Number

Additionally it is possible to turns a String into a Regex

const mapTypes = require('mapTypes');

const schemaData = {
    'someDeclaration': {
        'match': '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' // Match IP Address
    }
}
console.log(typeof schemaData.someDeclaration.match) // => String
let schema = mapTypes(schema, 'match', {regex: true})
console.log(typeof schemaData.someDeclaration.match) // => RegExp

About

NodeJS Module for iterating over an Object to replace key(String) with key(ObjectType)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published