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 🙏

© 2026 – Pkg Stats / Ryan Hefner

makestruct

v1.2.9

Published

Dynamically Generate Object Structures

Downloads

73

Readme

makeStruct

MakeStruct is a lightweight fast dynamic constructor generator for JavaScript.

The aim of makeStruct is to easily generate objects with dynamic properties on the fly.

concept

A struct is used for building data structures. These structures or records are used to group related data together.

installation

To install with npm:

npm i makestruct

Reference

makeStruct( 'keys' ) : Constructor

let myModel = 'example,foo, baz, the lazy fox jumps, over the, rainbow';
const MyDynamicClass = new makeStruct(myModel);
const myDynamicObject = new MyDynamicClass();

Object.keys(myDynamicObject); // ["example","foo","baz","theLazyFoxJumps","overThe","rainbow", ... methods]

Constructor definition:

/**
 * @constructor Generates a constructor for a given data structure
 * @param {string} keys comma separated.
 * @returns {constructor} Constructor for the new struct
 */

methods

propagateArray( Array ) : void

Populates a key-value array into the Object instance properties.

const myData = [
  ['id', 1],
  ['name', 'John'],
  ['email', '[email protected]'],
];
const MyDynamicClass = new makeStruct('id, name, email');
const myDynamicObject = new MyDynamicClass();

myDynamicObject.propagateArray(myData);
// myDynamicObject { id: 1, name: 'John', email: '[email protected]' }

propagateObject( Object ) : void

Populates an Object into the Object instance properties.

const myData = {
  id: 1,
  name: 'John',
  email: '[email protected]',
};
const MyDynamicClass = new makeStruct('id, name, email');
const myDynamicObject = new MyDynamicClass();

myDynamicObject.propagateObject(myData);
// myDynamicObject { id: 1, name: 'John', email: '[email protected]' }

hasKey( 'key' ) : boolean

myDynamicObject.hasKey('name'); // true
myDynamicObject.hasKey('nome'); // false

hasValue( 'value' ) : boolean

myDynamicObject.hasValue('John'); // true
myDynamicObject.hasValue('Johnny'); // false

toArray() : Array

myDynamicObject.toArray(); // [['id', 1], ['name', 'John'], ['email', '[email protected]']]

Overview:

const Dog = new makeStruct('id, name, breed');
// Dog -> Function, constructor()
const myDog = new Dog(1, 'baxter', 'New Scotland Retriever');
myDog; // { id: 1, name: 'baxter', breed: 'New Scotland Retriever' }

myDog.id; // returns 1
myDog.name; // returns 'baxter'
myDog.breed; // returns 'New Scotland Retriever'

Type information about the example above

typeof myDog.id; // 'number'
typeof myDog.breed; // 'string'
typeof myDog; // 'object'
myDog instanceof Dog; // true
Dog.prototype.isPrototypeOf(myDog); // true

importing

// in a module (like in a React component)
import makeStruct from 'makestruct';
// outside module (like in Node JS)
const makestruct = require('makestruct');

Define some structure:

const User = new makeStruct('id, name, country');

Instantiate your new structure

const foo = new User(1, 'John', 'UK');

Access the struct properties

foo.id; // 1
foo.name; // 'john'
foo.country; // 'UK'

Struct inside a struct

// Define a structure
const User = new makeStruct('id, name, country');
// Define another structure
const UserDetails = new makeStruct('phone, age, hairColor');
// Instantiate the inner struct first
const johnInfo = new UserDetails('555-777-888', 31, 'blonde');
// instantiate the parent struct passing the child struct as param
const john = new User(1, 'John', 'US', johnInfo);

// Accessing parent struct properties
john.id; // 1
john.name; // John
john.country; // 'US'

// Accessing child struct properties
john.info.phone; // '555-777-888'
john.info.age; // 31
john.info.hairColor; // 'blonde'

You can add structs into structs into other structs... just like a matroska.

The latest information I've relative to the maximum amount of keys in a JavaScript Object is around 8.3 million in V8 so that would be the limit approximately.

TypeDefs

You can define your data structures in JavaScript with TS pragma and JSDoc like that:

// @ts-check

/**
 * @typedef UserInfo
 * @property {string} phone
 * @property {number} age
 * @property {string} hairColor
 */

/** @type {ObjectConstructor|any} */
const UserInfo = new makeStruct('phone, age, hairColor');

/** @type {UserInfo} */
const extraInfo = new UserInfo('555-777-888', 31, 'blonde');

TypeScript example:

type Dog = {
  id: number;
  name: string;
  breed: string;
};

const LeDog = new makeStruct('id, name, breed');
const myDog: Dog = new LeDog(1, 'baxter', 'Retriever');

myDog.id.includes(); // Property 'includes' does not exist on type 'number'.ts(2339)

Dynamically creating structures from a third party Object

const myAPIResponse = {
  id: 1,
  name: 'John',
  userInfo: {
    city: 'NY',
    phone: '555-666-777',
  },
  pets: ['Baxter', 'Flurfils'],
};

const MyDynamicClass = new makeStruct(Object.keys(myAPIResponse).toString());
const myDynamicObject = new MyDynamicClass();

myDynamicObject.propagate(myAPIResponse);

myDynamicObject.name; // 'John'
myDynamicObject.pets; // ['Baxter', 'Flurfils']
myDynamicObject.userInfo; // { city: 'NY', phone: '555-666-777'}

Changelog

  • Added support for dynamically create structures from a third party Object

Project Status

  • The project is provided as is without warranties of any kind.
  • I may add d.ts in future updates but ts-pragma along with JSDoc should suffice to export types for the constructor that returns a constructor. You'll need to define your own types for the objects you create with makeStruct either way.
  • If you face any problem feel free to open a new issue and I'll try to look at it asap.
  • Same applies for any pull request. The project is open source and It's maintained through collaborators or by my own.