0.99.0 • Published 4 years ago

bass-clarinet-typed v0.99.0

Weekly downloads
5
License
BSD-2-Clause
Repository
github
Last release
4 years ago

bass-clarinet-typed

NPM Downloads NPM Version

bass-clarinet-typed is an extension to bass-clarinet that adds helper functions for typed files

if the document needs to conform to an expected structure (or schema)

import * as bc from "bass-clarinet-typed"
import * as fs from "fs"

const [, , path] = process.argv

if (path === undefined) {
    console.error("missing path")
    process.exit(1)
}

const data = fs.readFileSync(path, { encoding: "utf-8" })

const parser = new bc.Parser(
    err => { console.error("FOUND PARSER ERROR", err) },
)
const ec = new bc.ExpectContext(
    (_message, _range) => {
        throw new Error("encounterd error")
    },
    (_message, _range) => {
        throw new Error("encounterd warning")
    },
    () => bc.createDummyArrayHandler(),
    () => bc.createDummyObjectHandler(),
    () => bc.createDummyValueHandler(),
    () => bc.createDummyValueHandler(),
)

/**
 * expect an object/type with 2 properties, 'prop a' and 'prop b', both numbers
 */
parser.ondata.subscribe(bc.createStackedDataSubscriber(
    ec.expectType(
        (_range, _comments) => {
            //prepare code here
        },
        {
            "prop a": {
                onExists: _propertyMetaData => ec.expectNumber((_value, _metaData) => {
                    //handle 'prop a'
                }),
                onNotExists: null,
            },
            "prop b": {
                onExists: () => ec.expectNumber(_value => {
                    //handle 'prop b'
                }),
                onNotExists: null,
            },
        },
        (_hasErrors, _range, _comments) => {
            //wrap up the object
        }
    ),
    error => {
        if (error.context[0] === "range") {
            throw new bc.RangeError(error.message, error.context[1])
        } else {
            throw new bc.LocationError(error.message, error.context[1])
        }
    },
    _comments => {
        //wrap up the document
    }
))

bc.tokenizeString(
    parser,
    err => { console.error("FOUND TOKENIZER ERROR", err) },
    data
)
0.99.0

4 years ago

0.98.2

4 years ago

0.97.0

4 years ago

0.98.1

4 years ago

0.96.0

4 years ago

0.98.0

4 years ago

0.95.0

4 years ago

0.94.1

4 years ago

0.94.0

4 years ago

0.93.0

4 years ago

0.92.0

4 years ago

0.91.1

5 years ago

0.91.0

5 years ago

0.90.0

5 years ago

0.89.0

5 years ago

0.86.0

5 years ago

0.87.0

5 years ago

0.88.0

5 years ago

0.85.0

5 years ago

0.84.2

5 years ago

0.84.0

5 years ago

0.84.1

5 years ago

0.83.0

5 years ago

0.82.0

5 years ago

0.81.2

5 years ago

0.81.0

5 years ago

0.81.1

5 years ago

0.80.0

5 years ago

0.78.0

5 years ago

0.79.0

5 years ago

0.77.0

5 years ago

0.74.0

5 years ago

0.76.0

5 years ago

0.75.0

5 years ago

0.73.0

5 years ago

0.72.5

5 years ago

0.72.4

5 years ago

0.72.3

5 years ago

0.72.2

5 years ago

0.72.1

5 years ago

0.72.0

5 years ago

0.71.0

5 years ago

0.49.3

5 years ago

0.49.1

5 years ago

0.49.2

5 years ago

0.49.0

5 years ago

0.48.0

5 years ago

0.47.3

5 years ago

0.47.4

5 years ago

0.47.2

5 years ago

0.47.1

5 years ago

0.47.0

5 years ago