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

structor.js

v0.0.4

Published

Generates super fast type constructors from a schema

Downloads

2

Readme

Structor.js

Generate super fast type constructors from a schema definition

Getting started

Install structor.js with npm:

$ npm install structor.js

Quick Example:

// Create a new structor namespace
var Structs = require("structor.js").extend();

//Define a property type
Structs.defineProperty("number", function(data) {
    var $number = data.$key;
    
    if(typeof $number !== "number") {
        $number = 1 * $number;
    }
    
    this.$key = $number;
});

//Define a struct type
var Thing = Structs.defineStruct("Thing", {
    id : { type : "number" }
});

// Create instances!
var things = [ new Thing({ id : "1" }), new Thing({ id : 1 }) ];

Meta-syntax

Structor.js recycles some rarely used (but still valid) javascript syntax. Meta-functions are functions that contain this syntax and are passed in to structor.compile(fn). There are three meta-syntax forms supported for substitutions, all of them require a preceeding $.

The simple form looks like this: $prop and gets replaced by the value of the same named property on the data object provided to the meta-function template.

Simple Replacements

var metaTpl = Structs.compile(function(){
    return this.$foo;
});

var metaFn = new Function(metaTpl({ foo : "bar" }));

console.log(metaFn.call({ bar : "baz" })); // logs baz

Beyond the simple case are meta-expressions which look like this: $(...) and will evaluate the expression within to produce the value to use as the replacement.

Expression Replacements

var metaTpl = Structs.compile(function(){
    return this.$(data.foo);
});

var metaFn = new Function(metaTpl({ foo : "bar" }));

console.log(metaFn.call({ bar : "baz" })); // logs baz

Helpers

You can also register normal function helpers to be used within meta-functions. The syntax is similar to meta-expressions only also included a helper label $helper(...).

Expression Helpers

Structs.registerHelper("required", function(partial, schema) {
    if("required" in schema && schema.required == true) {
        return "throw new Error('invalid data');";
    }
});

var metaTpl = Structs.compile(function(){
    if(!this.foo) {
        $required();
    }
    
    return this.$(data.foo);
});

var metaFn = new Function(metaTpl({ foo : { required: true } }));

console.log(metaFn.call({ bar : "baz" })); // error thrown

Block Helpers

While very useful for certain types of replacements, expressions can be limiting for others. The above will produce a condition that get's checked regardless of it being needed or not. The final meta-syntax form offers a solution for these cases through block helpers($helper: {}).

Structs.registerHelper("required", function(partial, schema) {
    return Object.keys(schema).map(function(key){
        return schema[key].required ? partial : "";
    }).join("");
});

var metaTpl = Structs.compile(function(){
    $required : {
        if(!this.foo) {
            throw new Error('invalid data');
        }
    }
    
    return this.$(data.foo);
});

var metaFn = new Function(metaTpl({ foo : { required: true } }));

console.log(metaFn.call({ bar : "baz" })); // error thrown