# castly

> unmarshalling and type-casting in javascript

Latest version **0.4.1** (published 2013-12-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install castly
pnpm add castly
yarn add castly
bun add castly
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.1 |
| Published | 2013-12-03 |
| First published | 2013-11-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | david tudury |
| Maintainers | dtudury |
| Keywords | typecasting, casting, marshal, marshalling, type, unmarshal, unmarshalling, cast, typecast |

## Links

- npm: https://www.npmjs.com/package/castly
- Repository: https://github.com/dtudury/castly
- Issues: https://github.com/dtudury/castly/issues
- npm.io page: https://npm.io/package/castly

## Dependencies (1)

- [is-a](https://npm.io/package/is-a.md) ~0.2.5

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.4.1 (latest) — 2013-12-03
- 0.4.0 — 2013-12-03
- 0.3.2 — 2013-12-03
- 0.3.1 — 2013-12-03
- 0.3.0 — 2013-12-03
- 0.2.0 — 2013-11-28
- 0.1.1 — 2013-11-26
- 0.1.0 — 2013-11-26
- 0.0.3 — 2013-11-25
- 0.0.2 — 2013-11-25
- 0.0.1 — 2013-11-25
- 0.0.0 — 2013-11-25

## README

#castly

Javascript unmarshalling.  Copies generic JSON attributes into "fresh" instances complete with methods and prototypes.
Easily describe what you expect to come back from the server and it will optionally log or throw when it doesn't match.

  [![Build Status](https://travis-ci.org/dtudury/castly.png)](https://travis-ci.org/dtudury/castly)

## Installation

    $ npm install is-a

## Usage

```js

var myJSON = JSON.stringify({
    tuple:[1, 2, "three"],
    flag: true,
    config1: {
        a:true,
        b:false
    },
    config2: {
        a:true,
        b:false,
        c:true
    }
});


function myConstructor() {
    this.describeType = function () {
        return {
            tuple: SpecialTuple,
            flag: typeIs.BOOLEAN,
            config1: Config,
            config2: Config
        };
    };
}

function Config() {
    this.describeType = function () {
        return {
            a: typeIs.BOOLEAN,
            b: typeIs.BOOLEAN,
            c: [typeIs.BOOLEAN, typeIs.UNDEFINED]
        };
    };
    this.do_stuff = function() {
        console.log(this.a && this.b || this.c)
    }
}

function SpecialTuple() {
    this.describeType = function () {
        this.length = 3;
        return function(obj, name, attr) {
            if(attr === '0') return castly.convert(obj, typeIs.NUMBER, name + "[0]");
            if(attr === '1') return castly.convert(obj, typeIs.NUMBER, name + "[1]");
            if(attr === '2') return castly.convert(obj, typeIs.STRING, name + "[2]");
            else return castly.convert(obj, typeIs.UNDEFINED, name + "[" + attr + "]");
        }
    };
    this.toString = function() {
        return "I'm a special tuple " + this.join("---");
    }
}
SpecialTuple.prototype = [];



var castly = require('../index');
var typeIs = castly.typeIs;
castly.strict = true;

var myObj = castly.unmarshal(myJSON, myConstructor);
console.log(myObj.tuple.toString());  //"I'm a special tuple 1---2---three"
myObj.config1.do_stuff(); //"undefined"
myObj.config2.do_stuff(); //"true"


```

---
_Source: https://npm.io/package/castly · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
