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

infrastruct

v0.0.2

Published

Make JavaScript objects with stricter, struct-like data access.

Downloads

6

Readme

Infrastruct

Make JavaScript objects with stricter, struct-like data access.

npm version Build Status

Install

Install the dependency

# using npm
npm i --save infrastruct

# using yarn
yarn add infrastruct

Import into module

// if using ES6
import defineStruct from 'infrastruct';

// if not using ES6
const defineStruct = require('infrastruct');

What does this do and why?

The freedom to call any key on JavaScript objects can lead to lot of bugs. For example, you might have a user object that has the following structure:

const exampleUser = {
  username: 'opensourcer324',
  email: '[email protected]',
  zodiac: 'libra',
};

exampleUser.phoneNumber
//=> undefined

If you tried to access value with a key that is not username, email, or zodiac, such as phoneNumber JavaScript will simply return undefined. This is okay and reasonable in some cases, but if this user-object structure should be consistent for all users, it'd make sense to not allow for any data access other than for username, email, and zodiac, and to actually throw a runtime error if anyone tries to access another value. This can be accomplished with a struct. For example:

import defineStruct from 'infrastructure';

const createUserStruct = defineStruct(['username', 'email', 'zodiac']);

const exampleUser = createUserStruct({
  username: 'opensourcer324',
  email: '[email protected]',
  zodiac: 'libra',
});

exampleUser.phoneNumber
//=> Error: attempted to access non-existent property `phoneNumber` on struct


exampleUser.phoneNumber = '845-382-8492';
//=> Error: attempted to set non-existent property `phoneNumber` on struct

Struct creating functions defined with defineStruct will also throw a runtime error if you attempt to create a struct with params that contains any keys that weren't originally passed to defineStruct.

import defineStruct from 'infrastructure';

const createUserStruct = defineStruct(['username', 'email', 'zodiac']);

const exampleUser = createUserStruct({
  username: 'opensourcer324',
  email: '[email protected]',
  zodiac: 'libra',
  phoneNumber: '845-382-8492',
});
//=> Error: attempted to create struct with non-existent property `phoneNumber`

Are structs still objects though?

Yes! Under the hood, structs created with any defined-struct-functions are still plain JavaScript objects, and can do all the normal JavaScript object things like destructuring, using the spread syntax, getting passed to functions like Object.keys, etc.

However, anything that tries to call internal JavaScript object values on the object will not work. For example, if you tried to check the constructor of the exampleUser struct by calling exampleUser.constructor, an error would be thrown saying:

Error: attempted to access non-existent property `phoneNumber` on struct

How does it work?

When you call define struct, what you are receiving back is a function that will take whatever object you give it and wrap it in a Proxy. The Proxy basically intecepts any call made on the object that deals with property access, checks to see if the property that is trying to be accessed is valid property previousily defined when defineStruct was called, and if it is, it simply returns the value, otherwise it will throw an error.