Socket
Socket
Sign inDemoInstall

intertype

Package Overview
Dependencies
Maintainers
1
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

intertype

A JavaScript typechecker


Version published
Weekly downloads
167
increased by943.75%
Maintainers
1
Weekly downloads
 
Created
Source

InterType

A JavaScript type checker with helpers to implement own types and do object shape validation.

Table of Contents generated with DocToc

InterType

Hedges

types.isa.integer                                           42
types.isa.even.integer                                      -42
types.isa.odd.integer                                       41
types.isa.negative1.integer                                 -42
types.isa.negative0.integer                                 0
types.isa.positive1.integer                                 42
types.isa.positive0.integer                                 0
types.isa.list_of.integer                                   [ 42, ]
types.isa.nonempty.list_of.negative1.integer                [ -42, ]
types.isa.nonempty.list_of.negative0.integer                [ 0, ]
types.isa.nonempty.list_of.positive1.integer                [ 42, ]
types.isa.nonempty.list_of.positive0.integer                [ 0, ]
types.isa.empty.list_of.integer                             []
types.isa.nonempty.list_of.integer                          [ 42, ]
types.isa.optional.integer                                  42
types.isa.optional.list_of.integer                          [ 42, ]
types.isa.optional.empty.list_of.integer                    []
types.isa.optional.nonempty.list_of.integer                 [ 42, ]
types.isa.optional.negative1.integer                        -42
types.isa.optional.negative0.integer                        0
types.isa.optional.positive1.integer                        42
types.isa.optional.positive0.integer                        0
types.isa.optional.nonempty.list_of.negative1.integer       [ -42, ]
types.isa.optional.nonempty.list_of.negative0.integer       [ 0, ]
types.isa.optional.nonempty.list_of.positive1.integer       [ 42, ]
types.isa.optional.nonempty.list_of.positive0.integer       [ 0, ]
types.isa.optional.empty.list_of.negative1.integer          -42
types.isa.optional.empty.list_of.negative0.integer          0
types.isa.optional.empty.list_of.positive1.integer          42
types.isa.optional.empty.list_of.positive0.integer          0

[all]     [all]     [isa_collection]  [isa_collection]  [isa_numeric]   [isa_numeric]   [mandatory]
————————————————————————————————————————————————————————————————————————————————————————————————————
isa       optional  empty             list_of           even            negative0       <type>
validate            nonempty                            odd             negative1
                                                                        positive0
                                                                        positive1

Type Declarations

'use strict'

types       = new ( require '../../../apps/intertype' ).Intertype()
{ isa
  declare } = types
log         = console.log

declare 'xy_quantity', test: [
  ( x ) -> @isa.object          x
  ( x ) -> @isa.float           x.value
  ( x ) -> @isa.nonempty.text   x.unit
  ]

# can use simplified form:

declare.xy_quantity,
  test: [
    ( x ) -> @isa.object          x
    ( x ) -> @isa.float           x.value
    ( x ) -> @isa.nonempty.text   x.unit
    ]


log '^1-1^', isa.xy_quantity null
log '^1-1^', isa.xy_quantity 42
log '^1-1^', isa.xy_quantity { value: 42, unit: 'm', }

To Do

  • [–] make hedgepaths configurable—hedges need an opt-in
    • using depth (length) of hedgepath; default depth is 0
    • using wildcard hedgepath pattern (provided by picomatchhttps://github.com/micromatch/picomatch)
    • both at instantiation time for all builtins and declaration time for the type being declared
  • [–] allow to filter out builtin types
  • [–] implement sum types (a.k.a. tagged union, variant, variant record, choice type, discriminated union, disjoint union, or coproduct :-O) as in isa.integer.or.optional.nonempty.text
  • [–] implement hedges odd, even; valid for floats but do imply those numbers will be integer
  • [–] implement diagnostic API, primarily to check for existence of hedged types; allow to distinguish between standard types and user-defined types
  • [–] "a group is a set of types. A group's groups property is itself, so group collection is groupmember of group collection, meaning there are tests for isa.collection, isa.empty.collection and so on."
  • [–] eliminate hedgepaths that end in a hedge instead of in a type (or group). So we don't allow to test for empty x, only for empty.collection x, empty.any x &c
  • [–] special types:
    • groups
    • hedges
    • existential / quantified:
      • anything: ( x ) -> true
      • something: ( x ) -> x?
      • nothing: ( x ) -> not x?
  • [–] allow to derive types, including derivation of defaults
  • [–] provide methods for the ubiquitous validate.$TYPE ( cfg = { defaults.$TYPE..., cfg..., } ) as cfg = types.get_defaults.$TYPE cfg
  • [–] add defaults parameter to declare
  • [–] make it so that type declarations can be queried / viewed / checked by user, especially defaults must be retrievable so they can be referenced from new type declarations
  • [–] rename group number to real? to avoid conflict with JS Number and to clarify that this does not cover imaginary, complex numbers. Observe we now have BigInts pre-generating literally hundreds of hedgpath chains
  • [–] fix naming of type test functions (always test, should be name of type)
  • [–] use 'auto-vivification' for hedgepaths as outlined in hengist/dev/intertype
    • [–] how to finalize hedges?
      • [–] demand to declare types with hedgepaths? types.declare.empty.list? types.declare 'empty', 'list'?
      • [–] possible to 'auto-vivify' hedgepaths?
      • [–] scrap hedgepaths, replace by isa.$TYPE x, cfg API? or isa.$TYPE P..., x where P may be any number of modifiers as in isa.list 'optional', 'empty', x
  • [–] salvage

Is Done

  • [+] use Reflect.has() to check for property availability in size_of() reject because Reflect.has() balks on primitive values like numbers, too, which further complicates the code instead of making it simpler. ⁂sigh⁂
  • [+] in keeping with APIs for isa[ typename ] (i.e. isa.integer &c) and validate[ typename ], implement the same for declare as in declare.my_type cfg

FAQs

Package last updated on 20 Jul 2022

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