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

entityforge

v0.1.4-beta

Published

Entities >> Objects

Downloads

4

Readme

EntityForge

An integer isn't a data type, it's a compiler hint.

Models aren't just bundles of generic primitives.

Define a model to match reality:

import {EF} from "entityforge";

let UserForge = EF.obj({
    uuid: EF.string().minLength(20).maxLength(20).ascii(),
    email: EF.string().minLength(5).maxLength(255),
    memberSince: EF.int(2016).min(2000).max(2200)
})

Now create an instance of your model:

let NullableUserModel = UserForge.asNewable()
let example = new NullableUserModel()
console.log("UUID: ", example.uuid) // null
console.log("Email: ", example.email) // null
console.log("MemberSince: ", example.memberSince) // 2016
example.uuid = "-JhLeOlGIEjaIOFHR0xd"
example.email = "[email protected]"

try {
    example.uuid = example.uuid.substring(0, 10)
} catch (e) {
    console.log("I'm sorry dave....") // Nope, not allowed.
    console.log("Validation errors provide a cause", e.cause); 
    console.log("To be clear, the messaging system needs some work. The cause message is: ", e.cause.minLength.message) // @restriction.minLength
}
console.log("UUID wasn't modified by the attempt to set it to illegal value:", example.uuid) // "-JhLeOlGIEjaIOFHR0xd" --- value not modified if invalid.

Take care to recognize that there is a difference between setting minLength to zero and not allowing null. A null string is still valid even if minLength is set to zero.

Generating data

One advantage of specifying our data type in detail is that we can use that specification to do cool things. Like generate semi-random instances of our models:

let randomUser = UserForge.gen()
console.log(randomUser) // This instance will be as valid (or invalid) as your Forge definition constraints allow.

There is still work to be done on the data generation side. Of the missing functionality, the most important is the handling of cases that are hard to code for explicitly, such as string matching on a regex.

Built In Forges

The initial batch of forges cover the basic primitive types, and object wrappers:

let BiggerUserForge = EF.obj({
    uuid: EF.string().minLength(20).maxLength(20).ascii(),
    email: EF.string().minLength(5).maxLength(255),
    memberSince: EF.int(2016).min(2000).max(2200),
    karmaScore: EF.number().min(0).max(1).initTo(0.5),
    groups: EF.enumeration().values(["admin", "guest", "subscriber", "paid-member"]),
    contact: EF.obj({
        surname: EF.string().minLength(1).maxLength(255).ascii(),
        forename: EF.string().minLength(1).maxLength(255).ascii(),
        addressLine1: EF.string().minLength(1).maxLength(255).ascii(),
        addressLine2: EF.string().minLength(1).maxLength(255).ascii(),
        postcode: EF.string().minLength(3).maxLength(25).ascii(),
    })

})
let biggerUser = BiggerUserForge.gen()
console.log("A randomly generated 'BiggerUser:", biggerUser)

Contributing

Getting started

npm install 
npm run test