0.99.0 • Published 3 years ago

bass-clarinet-typed v0.99.0

Weekly downloads
5
License
BSD-2-Clause
Repository
github
Last release
3 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

3 years ago

0.98.2

3 years ago

0.97.0

3 years ago

0.98.1

3 years ago

0.96.0

3 years ago

0.98.0

3 years ago

0.95.0

3 years ago

0.94.1

3 years ago

0.94.0

3 years ago

0.93.0

3 years ago

0.92.0

3 years ago

0.91.1

3 years ago

0.91.0

3 years ago

0.90.0

3 years ago

0.89.0

4 years ago

0.86.0

4 years ago

0.87.0

4 years ago

0.88.0

4 years ago

0.85.0

4 years ago

0.84.2

4 years ago

0.84.0

4 years ago

0.84.1

4 years ago

0.83.0

4 years ago

0.82.0

4 years ago

0.81.2

4 years ago

0.81.0

4 years ago

0.81.1

4 years ago

0.80.0

4 years ago

0.78.0

4 years ago

0.79.0

4 years ago

0.77.0

4 years ago

0.74.0

4 years ago

0.76.0

4 years ago

0.75.0

4 years ago

0.73.0

4 years ago

0.72.5

4 years ago

0.72.4

4 years ago

0.72.3

4 years ago

0.72.2

4 years ago

0.72.1

4 years ago

0.72.0

4 years ago

0.71.0

4 years ago

0.49.3

4 years ago

0.49.1

4 years ago

0.49.2

4 years ago

0.49.0

4 years ago

0.48.0

4 years ago

0.47.3

4 years ago

0.47.4

4 years ago

0.47.2

4 years ago

0.47.1

4 years ago

0.47.0

4 years ago