Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typ3s

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typ3s

Allow define type on json

  • 0.1.10
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
55
increased by83.33%
Maintainers
1
Weekly downloads
 
Created
Source

typ3s

The objective of this library is to obtain the type of an object, array, function, etc.
Also be able to obtain information about the cardinality of the data.

Primitive

  • string
  • integer
  • decimal
  • number
  • boolean
  • date
  • dateTime
  • time
  • any
  • void

Array / Tuples

[]

Object

{}

Function

()

Modifiers

Modifierdescription
!Nullable
?Undefinable
~Async

Examples

typedescription
[string]array of string
[string, integer]tuple with string and integer properties
{name:string, nro:integer}object with string and integer properties
[{name:string, today:date}]array of object
(>any)function without params and return value
(list:[integer]>integer)function with array integer as params and return integer
(today:date,nro:?integer>date)function with undefinable integer param
{name:string,nro:?integer}object of properties string and undefinable integer
{name:string,nro:!integer}object of properties string and nullable integer
([integer]>~integer)function with return async of integer

Methods

methoddescription
typeget the type
stringifyconverts the type to a string
parseconverts the string to type
serializeserialize type
deserializeddeserialize type
cardinalitycomplete cardinality information in the type

Example

This example shows how you can obtain the type of an array of objects, convert it to a string, parse it, serialize it, and deserialize it.

import { Type } from 'typ3s'
const data = [
	{
		name: 'Spain',
		region: { name: 'Europe', code: 'EU', description: 'European Union' },
		languages: ['Spanish', 'Catalan', 'Galician', 'Basque'],
		phoneCode: 34
	},
	{
		name: 'France',
		region: { name: 'Europe', code: 'EU', description: 'European Union' },
		languages: ['French'],
		phoneCode: 33
	},
	{
		name: 'Canada',
		region: { name: 'North America', code: 'NA', description: null },
		languages: ['English', 'French']
	}
]
const type = Type.type(data)
const stringified = Type.stringify(type)
const type2 = Type.parse(stringified)
const serialized = Type.serialize(type2, 2)
const deserialized = Type.deserialize(serialized)
const serialize2 = Type.serialize(deserialized)
console.log(stringified)
console.log(serialized)
console.log(serialize2)

stringified:

[{name:string,region:{name:string,code:string,description:string},languages:[string],phoneCode:integer}]

serialized:

{
  "primitive": "list",
  "list": {
    "items": {
      "primitive": "obj",
      "obj": {
        "properties": [
          {
            "name": "name",
            "type": {
              "primitive": "string"
            }
          },
          {
            "name": "region",
            "type": {
              "primitive": "obj",
              "obj": {
                "properties": [
                  {
                    "name": "name",
                    "type": {
                      "primitive": "string"
                    }
                  },
                  {
                    "name": "code",
                    "type": {
                      "primitive": "string"
                    }
                  },
                  {
                    "name": "description",
                    "type": {
                      "primitive": "string"
                    }
                  }
                ]
              }
            }
          },
          {
            "name": "languages",
            "type": {
              "primitive": "list",
              "list": {
                "items": {
                  "primitive": "string"
                }
              }
            }
          },
          {
            "name": "phoneCode",
            "type": {
              "primitive": "integer"
            }
          }
        ]
      }
    }
  }
}

serialized2:

{"primitive":"list","list":{"items":{"primitive":"obj","obj":{"properties":[{"name":"name","type":{"primitive":"string"}},{"name":"region","type":{"primitive":"obj","obj":{"properties":[{"name":"name","type":{"primitive":"string"}},{"name":"code","type":{"primitive":"string"}},{"name":"description","type":{"primitive":"string"}}]}}},{"name":"languages","type":{"primitive":"list","list":{"items":{"primitive":"string"}}}},{"name":"phoneCode","type":{"primitive":"integer"}}]}}}}

Cardinality example

This example shows how you can obtain the cardinality of an array of objects. By using the cardinality method, you get information about the cardinality that is added to the type.

import { Type } from '../../lib'
const data = [
	{
		name: 'Spain',
		region: { name: 'Europe', code: 'EU', description: 'European Union' },
		languages: ['Spanish', 'Catalan', 'Galician', 'Basque'],
		phoneCode: 34
	},
	{
		name: 'France',
		region: { name: 'Europe', code: 'EU', description: 'European Union' },
		languages: ['French'],
		phoneCode: 33
	},
	{
		name: 'Canada',
		region: { name: 'North America', code: 'NA', description: null },
		languages: ['English', 'French']
	}
]
const type = Type.type(data)
Type.cardinality(data, type)
const stringified = Type.stringify(type)
const serialized = Type.serialize(type, 1)
console.log(stringified)
console.log(serialized)

stringified:

[{languages:[string],name:string,phoneCode:?integer,region:{code:string,description:!string,name:string}}]

serialized:

{
 "primitive": "list",
 "list": {
  "items": {
   "primitive": "obj",
   "obj": {
    "properties": [
     {
      "name": "languages",
      "type": {
       "primitive": "list",
       "list": {
        "items": {
         "primitive": "string",
         "indefinite": 0,
         "nullables": 0,
         "repeated": 1,
         "distinctCount": 6,
         "count": 7,
         "repeatRate": 0.047619047619047616,
         "nullable": false,
         "undefinable": false,
         "unique": false
        }
       },
       "indefinite": 0,
       "nullables": 0,
       "repeated": 1,
       "count": 7,
       "repeatRate": 0.047619047619047616,
       "nullable": false,
       "undefinable": false,
       "unique": false,
       "distinctCount": 6
      }
     },
     {
      "name": "name",
      "type": {
       "primitive": "string",
       "onParentDistinctRepeated": 0,
       "distinctCount": 3,
       "onParentDistinctRepeatedRate": 0,
       "onParentDistinctUnique": true,
       "indefinite": 0,
       "nullables": 0,
       "repeated": 0,
       "count": 3,
       "repeatRate": 0,
       "nullable": false,
       "undefinable": false,
       "unique": true
      }
     },
     {
      "name": "phoneCode",
      "type": {
       "primitive": "integer",
       "onParentDistinctRepeated": 0,
       "distinctCount": 3,
       "onParentDistinctRepeatedRate": 0,
       "onParentDistinctUnique": true,
       "indefinite": 1,
       "nullables": 0,
       "repeated": 0,
       "count": 3,
       "repeatRate": 0,
       "nullable": false,
       "undefinable": true,
       "unique": true
      }
     },
     {
      "name": "region",
      "type": {
       "primitive": "obj",
       "obj": {
        "properties": [
         {
          "name": "code",
          "type": {
           "primitive": "string",
           "onParentDistinctRepeated": 0,
           "distinctCount": 2,
           "onParentDistinctRepeatedRate": 0,
           "onParentDistinctUnique": true,
           "indefinite": 0,
           "nullables": 0,
           "repeated": 1,
           "count": 3,
           "repeatRate": 0.3333333333333333,
           "nullable": false,
           "undefinable": false,
           "unique": false
          }
         },
         {
          "name": "description",
          "type": {
           "primitive": "string",
           "onParentDistinctRepeated": 0,
           "distinctCount": 2,
           "onParentDistinctRepeatedRate": 0,
           "onParentDistinctUnique": true,
           "indefinite": 0,
           "nullables": 1,
           "repeated": 1,
           "count": 3,
           "repeatRate": 0.3333333333333333,
           "nullable": true,
           "undefinable": false,
           "unique": false
          }
         },
         {
          "name": "name",
          "type": {
           "primitive": "string",
           "onParentDistinctRepeated": 0,
           "distinctCount": 2,
           "onParentDistinctRepeatedRate": 0,
           "onParentDistinctUnique": true,
           "indefinite": 0,
           "nullables": 0,
           "repeated": 1,
           "count": 3,
           "repeatRate": 0.3333333333333333,
           "nullable": false,
           "undefinable": false,
           "unique": false
          }
         }
        ]
       },
       "indefinite": 0,
       "nullables": 0,
       "repeated": 1,
       "distinctCount": 2,
       "count": 3,
       "repeatRate": 0.3333333333333333,
       "nullable": false,
       "undefinable": false,
       "unique": false
      }
     }
    ]
   },
   "repeated": 0,
   "nullables": 0,
   "indefinite": 0,
   "distinctCount": 3,
   "count": 3,
   "repeatRate": 0,
   "nullable": false,
   "undefinable": false,
   "unique": true
  }
 }
}

Keywords

FAQs

Package last updated on 19 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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