npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

decodable-js

v2.0.3

Published

## Overview `decodable-js` is a JavaScript library inspired by Swift's approach to JSON decoding. It verifies the types within JSON data, and based on configuration, can either raise an error for type mismatches or ignore the misaligned fields. It also al

Readme

decodable-js

Overview

decodable-js is a JavaScript library inspired by Swift's approach to JSON decoding. It verifies the types within JSON data, and based on configuration, can either raise an error for type mismatches or ignore the misaligned fields. It also allows selective data extraction and offers the option to convert between strings and numbers and vice versa.

Installation

To integrate decodable-js into your project, run one of the following commands:

# If you use yarn:
yarn add decodable-js

# Or if you prefer npm:
npm install decodable-js

Usage

Here's how you can use decodable-js in your project:

import { decodable, T } from 'decodable-js';

// Define your JSON structure
const JsonStruct = {
    age: T.number,
    address: T.string,
    visible: T.boolean,
    numbers: [T.string],
    salary: T.number_$ // optional
    // Add more fields as required
}

// Your JSON data
const jsonData = {
    age: 12,
    address: '123 Cherry Lane',
    visible: true,
    numbers: ['one', 'two', 'three'],
    rest: ['Will bi skipped']
    // Additional data...
}

// Decode the data with type enforcement
const result = decodable({data: jsonData, struct: JsonStruct});

// Output the result
console.log(result);
// {
//    age: 12,
//    address: '123 Cherry Lane',
//    visible: true,
//    numbers: ['one', 'two', 'three'],
// }

const JsonStruct = {
    numbers: [T.string],
}


const jsonData = {
    numbers: [1, '2', '3'], //  1 will be converted to string
}

const result = decodable({
    data: jsonData, 
    struct: JsonStruct,
    enableConvert: true // enable convert
});

// Output the result
console.log(result);
// {
//    age: 12,
//    address: '123 Cherry Lane',
//    visible: true,
//    numbers: ['1', '2', '3'], // converted to string
// }

API Reference

Types

  • T.number
  • T.string
  • T.boolean
  • T.object
  • T.null = null
  • T.undefined

Optional types (type || undefined)

  • T.number_$
  • T.string_$
  • T.boolean_$
  • T.null_$
  • T.object_$

The main function decodable() is used to decode JSON data:

const result = decodable({
    data: { index:1 },
    struct: { 
        index:T.number,
        value: T.string_$ // optional
    },
    enableConvert: false,
    silentMode: false
});
  • data: {} | Array<any> - The JSON data to decode.
  • struct: {} | Array<any> - The structure that data should be decoded into.
  • enableConvert: boolean - If true, enables conversion between strings and numbers (defaults to false).
  • silentMode: boolean - If false, throws an error when data does not match the structure (defaults to false).

Author

  • Alex Shumihin - Initial work and maintenance.

For any feedback or issues, please open a GitHub issue or submit a pull request.