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-morpher

v1.0.1

Published

Transform and tailor object structures with ease using dynamic compute functions and value aliases

Downloads

18

Readme

object-morpher

object-morpher is an advanced transformation utility designed to work with single-level object structures, such as command-line arguments. It serves as an extension and rewrite of the previous library, alias-mapper, with enhanced capabilities to handle complex data transformations while maintaining a focus on single-level initial objects.

Features

  1. Extended Functionality: Building upon the foundation of my previous library (alias-mapper), object-morpher introduces additional features like dynamic compute functions and value mappings, offering more power and flexibility in data shaping.

  2. Single-Level Object Focus: Despite its advanced features, object-morpher remains targeted at processing single-level initial objects, ensuring simplicity and ease of use where complex nested structures are not required.

  3. Value Parsing: It is recommended to use a parsing library such as zod for handling value parsing and type safety, which complements object-morpher's transformation capabilities.

  4. TypeScript Support: Fully typed to enable IntelliSense in your favorite IDE.

By leveraging object-morpher, developers can easily manipulate data structures to fit their application's needs, streamlining the process of data handling and transformation.

It's primarily used with Bun, so use:

bun add github:ristosha/object-morpher

or just npm:

npm install object-morpher

Usage

First, import the library:

import { transformObject } from 'object-morpher';

Define your schema:

const schema = {
  age: {
    _compute: (val: number) => val + 1,
    _aliases: ['profileAge'],
    _values: {
      "60": "old",
      "30": "middle",
      "12": "young"
    }
  },
  // ... other schema definitions
};

Transform your object:

const user = {
  profileAge: "young",
  // ... other user properties
};

const transformedUser = transformObject(user, schema);

Some examples from tests:

const obj = { name: 'John', age: 30 }

const schema: Schema<typeof obj> = {
    name: { _compute: (val: string) => val.toUpperCase() },
    age: { _compute: (val: number) => val + 1 }
}

// transformObject(obj, schema)

const expected = { name: 'JOHN', age: 31 }
const obj = { fullName: 'Alice Wonderland' }

const schema: Schema = {
    __composite: {
      fullName: {
        _compute: (val: string) => {
          const [firstName, lastName] = val.split(' ')
          return { firstName, lastName }
        }
      }
    }
}

// transformObject(obj, schema)

const expected = { firstName: 'Alice', lastName: 'Wonderland' }

⚠️ Caution

For frequent use cases, it is highly recommended to pre-generate the alias map using generateAliasMap(schema). This practice improves performance by avoiding the overhead of recalculating the map for each transformation, especially when dealing with large schemas or processing numerous objects.

API Reference

transformObject(obj, schema, aliasMap?)

Transforms an object based on the provided schema and optional alias map.

  • obj: The object to transform.
  • schema: The schema definition for the transformation.
  • aliasMap: Optional map of aliases to property paths.

Schema Definition

A schema is defined as an object where each key corresponds to a property in the source object. Each key's value is an object that can have the following properties:

  • _compute: A function that takes the property's value and returns a new value.
  • _values: An object mapping possible values to their aliases. (don't work with _compute)
  • _aliases: An array of strings representing aliases for the property.

License

object-morpher is MIT licensed.