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

morphia

v1.0.4

Published

A powerful library designed for transforming complex JSON structures into compact, index-based arrays and back. Ideal for efficient data serialization and deserialization, Morphia simplifies the handling of nested and dynamic data by flattening JSON objec

Downloads

15

Readme

Morphia

npm version License: MIT

Morphia is a lightweight TypeScript library for converting complex objects to and from array representations. It provides an efficient way to serialize and deserialize nested data structures, making it useful for data storage, transmission, and manipulation.

Installation

You can install Morphia using npm:

npm install morphia

Usage

Basic Example

import { Morphia } from "morphia";

const morphia = new Morphia();

// Original object
const originalObject = {
  name: "John Doe",
  age: 30,
  hobbies: ["reading", "cycling"],
  address: {
    street: "123 Main St",
    city: "Anytown",
  },
};

// Convert to array
const arrayRepresentation = morphia.toArray(originalObject);
console.log(arrayRepresentation);
// Output: [6, '&2', 'John Doe', 30, '@2', 'reading', 'cycling', '&2', '123 Main St', 'Anytown']

// Convert back to object
const reconstructedObject = morphia.fromArray(arrayRepresentation);
console.log(reconstructedObject);
// Output: { name: 'John Doe', age: 30, hobbies: [ 'reading', 'cycling' ], address: { street: '123 Main St', city: 'Anytown' } }

Advanced Usage

Morphia can handle complex nested structures and preserves the structure of your data:

import { isEqual } from "lodash"; // or use another deep equality function

const complexObject = {
  users: [
    { id: 1, name: "Alice", roles: ["admin", "user"] },
    { id: 2, name: "Bob", roles: ["user"] },
  ],
  settings: {
    theme: "dark",
    notifications: {
      email: true,
      push: false,
    },
  },
};

const arrayForm = morphia.toArray(complexObject);
const reconstructed = morphia.fromArray(arrayForm);

// Note: Using JSON.stringify for comparison may give false negatives due to object key ordering
// Instead, use a deep equality function for accurate comparison
console.log(isEqual(complexObject, reconstructed)); // true

Important Note: When converting objects to arrays and back, the order of properties in the resulting object may differ from the original. This is because JavaScript objects do not guarantee property order. For example:

const obj1 = { a: 1, b: 2 };
const array = morphia.toArray(obj1);
const obj2 = morphia.fromArray(array);

console.log(obj2); // Might output: { b: 2, a: 1 }

While the content is the same, the order of properties may change. Always use a deep equality function (like lodash.isEqual) for comparing objects, rather than comparing stringified versions.

API Reference

Morphia

The main class for converting objects to and from array representations.

Methods

  • toArray<Input>(input: Input): Flatten<Input>[] Converts a complex object to an array representation.

  • fromArray(input: Primitive[]): any Converts an array representation back to its original complex object form.

  • get keys(): Record<string, number> Gets the current mapping of keys to their indices.

  • set keys(newKeys: Record<string, number>) Sets a new mapping of keys to their indices.

Why Morphia?

  • Efficient Serialization: Morphia provides a compact array representation of complex objects, which can be useful for storage or transmission.
  • Type Safety: Written in TypeScript, Morphia provides type safety and autocompletion in supported IDEs.
  • Preservation of Structure: Morphia accurately preserves the structure of your data, including nested objects and arrays.
  • Customizable: The key-to-index mapping can be customized and reused for consistent serialization across multiple objects.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any issues or have questions, please file an issue on the GitHub repository.