node-typesafe

Typesafe data structures for Javascript

Usage no npm install needed!

<script type="module">
  import nodeTypesafe from 'https://cdn.skypack.dev/node-typesafe';
</script>

README

node-typesafe

Typesafe data structures for Node.js

Example usage:

  • Define a typed structure:
var $ = require('node-typesafe');

var Location = $.Array($.Number);

var Product = $.Structure('Product', {
    name: $.String,
    added: $.Date,
    time: $.Time,
    removedAt: $.Datetime,
    price: $.Number,
    active: $.Boolean,
    location: Location
});

var getPrice = $.Function($.Number, [Product], function getPrice(product) {
    return product.price;
});

  • Create a typed object:
var product = new Product({
    added: '2009-08-10',
    time: '8:00:59',
    removedAt: '2013-12-22 01:44:22',
    price: 13.24,
    location: [1, 2, 56.2],
    active: true
});
  • Use typed object:
product.active = false;
console.log(product.price);
product.time = undefined;
console.log(product.json());

console.log(getPrice(product));