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

object-creator

v1.1.1

Published

A utility to create nested objects based on string paths with TypeScript support.

Downloads

7

Readme

Object Creator

Object Creator is a TypeScript-compatible utility that allows you to create nested JavaScript objects based on string paths. It supports dynamic object structures, including arrays and various data types, making it a versatile tool for managing complex data structures.

Table of Contents

Installation

You can install object-creator via npm:

npm install object-creator

Or using Yarn:

yarn add object-creator

Usage

Import the DynamicObject class into your project and use it to build nested objects based on string paths.

Basic Example

Creating a simple nested object without arrays:

import { DynamicObject } from 'object-creator';

const dynObj = new DynamicObject();
dynObj.set('user:object.name:string', 'Alice');
console.log(dynObj.getObject());
// Output: { user: { name: 'Alice' } }

Handling Arrays

Creating nested structures that include arrays:

import { DynamicObject } from 'object-creator';

const dynObj = new DynamicObject();
dynObj.set('user:object.emails:array', '[email protected]');
console.log(dynObj.getObject());
// Output: { user: { emails: ['[email protected]'] } }

Multiple Entries

Adding multiple entries to an array:

import { DynamicObject } from 'object-creator';

const dynObj = new DynamicObject();

// Adding first email
dynObj.set('user:object.emails:array', '[email protected]');

// Adding second email
dynObj.set('user:object.emails:array', '[email protected]');

console.log(dynObj.getObject());
// Output:
// {
//   user: {
//     emails: ['[email protected]', '[email protected]']
//   }
// }

API Reference

DynamicObject

Encapsulates the object being manipulated and provides methods to interact with it.

Constructor

constructor(initialObject?: Record<string, any>);
  • Parameters:
    • initialObject (optional): The initial object to update or create. Defaults to an empty object {} if not provided.

Methods

set(path: string, value?: ValueType): Record<string, any>

Creates or updates a value at the specified path.

  • Parameters:

    • path (string): The string path indicating where to set the value (e.g., "user:object.emails:array").
    • value (ValueType): The value to set at the specified path.
  • Returns:

    • Record<string, any>: The updated object.
  • Example:

    dynObj.set('user:object.age:number', 30);
    console.log(dynObj.getObject());
    // Output: { user: { age: 30 } }
getObject(): Record<string, any>

Retrieves the internal object.

  • Returns:

    • Record<string, any>: The current object.
  • Example:

    const obj = dynObj.getObject();
    console.log(obj);

Type Definitions

type ValueType = string | number | boolean | Record<string, any> | any[];

Path Syntax

  • Structure:

    • Use : to denote the type of the current segment.
    • Specify the data type after the colon (e.g., string, number, boolean, object, array).
    • Separate nested keys with a dot ..
  • Examples:

    • 'user:object.name:string' creates { user: { name: 'value' } }.
    • 'users:object.list:array.username:string' creates { users: { list: [ { username: 'value' } ] } }.
  • Handling Arrays:

    • When a segment is specified as an array, subsequent segments within that array item can be defined.
    • Each call to set with the same array path will append a new entry to the array.

Examples

// Creating a simple nested object
dynObj.set('user:object.profile.name:string', 'Alice');
console.log(dynObj.getObject());
// Output: { user: { profile: { name: 'Alice' } } }

// Creating a nested object with an array
dynObj.set('users:object.list:array.username:string', 'Bob');
console.log(dynObj.getObject());
// Output: { user: { profile: { name: 'Alice' } }, users: { list: [ { username: 'Bob' } ] } }

// Adding another user to the array
dynObj.set('users:object.list:array.username:string', 'Charlie');
console.log(dynObj.getObject());
// Output:
// {
//   user: { profile: { name: 'Alice' } },
//   users: {
//     list: [
//       { username: 'Bob' },
//       { username: 'Charlie' }
//     ]
//   }
// }

Contributing

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the Repository

    Click the "Fork" button at the top right of the repository page to create a copy of the repository under your GitHub account.

  2. Clone the Forked Repository

    git clone https://github.com/yourusername/object-creator.git
  3. Navigate to the Project Directory

    cd object-creator
  4. Create a New Branch

    git checkout -b feature/your-feature-name
  5. Make Your Changes

    Implement your feature or bug fix.

  6. Commit Your Changes

    git commit -m "Add feature: your feature description"
  7. Push to the Branch

    git push origin feature/your-feature-name
  8. Open a Pull Request

    Go to the original repository and click on "Compare & pull request" to submit your changes for review.

License

This project is licensed under the MIT License.