1.0.5 • Published 5 years ago

type-spaces v1.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Small package for Node.js.

NPM

Table Of Contents

Installation

npm i type-spaces

Usage

This is an experimental library for custom types in Node.js.

const { generateNewTypeSpace } = require('type-spaces')

const TS = generateNewTypeSpace()
/**
 * You can attach typespace to Node.js `global`: `global.TS = TS;`
 * and use it anywhere in your app.
 */
with (TS) {
  int ['count'] = 33
  string ['borough'] = 'Manhattan'
  bool ['configChecked'] = true
  symbol ['varWithUniqueVal'] = Symbol('foo')

  int ['nullInt'] = null
  string ['nullString'] = null
  bool ['nullBool'] = null
  symbol ['nullSymbol'] = null

  int ['undefinedInt'] = undefined
  string ['undefinedString'] = undefined
  bool ['undefinedBool'] = undefined
  symbol ['undefinedSymbol'] = undefined
}

Examples

Variable example 1:

const { generateNewTypeSpace } = require('type-spaces')

with (generateNewTypeSpace()) {
  int ['count'] = 33
  count = 'Manhattan' // TypeError: invalid conversion from 'int' to 'string'
}

Variable example 2:

const { generateNewTypeSpace } = require('type-spaces')

with (generateNewTypeSpace()) {
  int ['count'] = 33
  int ['count'] = 34 // SyntaxError: Identifier 'count' has already been declared
}

Function example 1:

const { Func, INT, STRING, BOOL } = require('type-spaces')

const myTypedFunction = Func (INT ('foo'), STRING ('bar'), BOOL ('baz')) (INT) (() => {
  console.log(foo, bar, baz) // 133 'text' false
  return 0
})
const result = myTypedFunction(133, 'text', false)
console.log(result) // 0

Function example 2:

const { Func, INT, STRING, BOOL } = require('./lib/type-spaces')

const myTypedFunction = Func (INT ('foo'), STRING ('bar'), BOOL ('baz')) (INT) (() => {
  console.log(foo, bar, baz)
  return 0
})
const result = myTypedFunction(133, 'text', 'test') // TypeError: invalid conversion from 'STRING' to 'BOOL'

Function example 3:

const { Func, INT, STRING, BOOL } = require('./lib/type-spaces')

const myTypedFunction = Func (INT ('foo'), STRING ('bar'), BOOL ('baz')) (BOOL) (() => {
  if (foo === 132)
    return true
  else if (bar === 'test')
    return 1 // TypeError: invalid conversion from 'INT' to 'BOOL'
  else if (baz)
    return false
  return true
})
const result = myTypedFunction(133, 'text', false)

Function example 4:

const { Func, INT, STRING, BOOL } = require('./lib/type-spaces')

const myTypedFunction = Func (INT ('foo'), STRING ('bar'), BOOL ('baz')) (void 0) (() => {
  return 1 // TypeError: return-statement with a value, in function returning 'void'
})
const result = myTypedFunction(133, 'text', false)

Notes

  • Supported types for variables: int, string, bool, symbol
  • Supported types for functions: INT, STRING, BOOL
  • It doesn't enforce you to use itself in the hole application, you can include each type space in whichever part you want
  • It does type checking at runtime
1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago