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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@js-bits/model

v0.1.1

Published

Multi-purpose data model

Downloads

8

Readme

[WORK IN PROGRESS] Multi-purpose data model

Inspired by Backbone.Model. Re-imagined and modernized.

Model is a static data structure, defined by certain rules using a data schema.

const Profile = new Model({
  firstName: String,
  lastName: String,
  yearBorn: Number,
  'verified?': Boolean, // optional property
});

const author = new Profile({
  firstName: 'Trygve',
  lastName: 'Reenskaug',
  yearBorn: 1930,
});

console.log(`${author}`);
// [object Model]

console.log(JSON.stringify(author, null, '  '));
// {
//   firstName: 'Trygve',
//   lastName: 'Reenskaug',
//   yearBorn: 1930,
//   verified: null
// }

Key features:

  • Strict data schema
  • Runtime data validation
  • Configurable data types
  • Optional deep objects immutability
  • Interactive data store
  • ...

[TBD]

Installation

Install with npm:

npm install @js-bits/model

Install with yarn:

yarn add @js-bits/model

Import where you need it:

import { Model, DataType } from '@js-bits/model';

or require for CommonJS:

const { Model, DataType } = require('@js-bits/model');

How to use

[TBD]

More examples can be found in examples folder.

How is this different from JSON Schema?

JSON Schema serves more as a data format description, while Model is much more functional and gives you more control over data.

[TBD]

Ok. But what about TypeScript?

Code written in TypeScript is only checked for errors before it is executed, during compile time. Moreover, not every project is built with TypeScript. And not every project needs TypeScript.

TypeScript is ‘not worth it’ for developing libraries, says Svelte author, as team switches to JavaScript and JSDoc

[TBD]

As for IDE's code-completion capabilities, you can achieve similar result with JSDoc annotations.

Hmm... But we already have Zod

Sure. Model does something similar to what Zod does, but while Zod is "a schema declaration and validation library", Model goes further and is aimed to serve more like a data store. Also, Model has more natural syntax.

[TBD]

GraphQL?

GraphQL queries tend to grow over time and you have to carry unnecessary data even though you don't use it. Model allows you to control what data exactly you need to operate.

[TBD]

How about immutability, then?

Well, immutability is not a dogma.

[TBD]

  • https://desalasworks.com/article/immutability-in-javascript-a-contrarian-view/
  • https://stackoverflow.com/questions/34385243/why-is-immutability-so-important-or-needed-in-javascript/43318963#43318963
  • https://medium.com/dailyjs/the-state-of-immutability-169d2cd11310
  • https://stackoverflow.com/questions/1863515/pros-cons-of-immutability-vs-mutability

But, anyway, you can make your data deeply immutable with this package and still use other benefits (like data validation) at the same time.

Object.freeze(object); // shallow freeze
// versus
new CustomModel(object); // deep freeze