0.0.12 • Published 4 years ago

namebase-types v0.0.12

Weekly downloads
60
License
MIT
Repository
-
Last release
4 years ago

Namebase Types

This package lets you do things like this:

const {
  OR,
  OBJECT,
  STRING,
  INTEGER,
  BOOLEAN,
  ENUM,
  exceptions,
} = require('namebase-types');

const parser = OR(
  OBJECT({
    a: STRING,
    b: INTEGER,
  }),
  OBJECT({
    a: STRING,
    b: INTEGER,
    c: BOOLEAN,
    d: ENUM(10, 20),
  }),
)

// returns { a: "foo", b: 4 }
parser({ a: "foo", b: 4 })

// returns { a: "foo", b: 4, c: true, d: 20 }
parser({ a: "foo", b: 4, c: true, d: 20 })

// throws exceptions.InvalidType({ key: "a" })
parser({ a: false, b: 4, c: true, d: 20 })

// throws exceptions.MissingKey({ key: "b" })
parser({ a: "foo", c: true, d: 10 })

// throws exceptions.MissingKey({ key: "c" })
parser({ a: "foo", b: 4, d: 10 })

// throws exceptions.ExtraKey({ key: "e" })
parser({ a: "foo", b: 4, c: true, d: 10, e: "bar" })

Check the tests for more details. Note: instead of passing in the built-in primitives (STRING, INTEGER, BOOLEAN, ENUM, OBJECT), you can pass in any parsing function that returns its parsed input on success (and throws otherwise).

License

MIT License: https://igliu.mit-license.org

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago