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

flatten-ts

v1.0.1

Published

![Alligator](./docs/alligator.png)

Readme

Alligator

Flatten-ts Documentation

Summary

Introduction

flatten-ts is a lightweight TypeScript library for flattening arrays of any depth into a single-level array.

Using the flatten method in general has several advantages, including: Usage:

1 - Reduced code complexity: flatten simplifies the process of manipulating arrays of arrays, reducing the need for writing complex code to perform this operation.

2 - Improved code readability: Using flatten makes the code more readable, as it is easier to understand what the code is doing when using a standard method instead of writing custom code.

3 - Performance improvements: flatten is a native method of JavaScript, which means it has been optimized for performance. This means that using flatten can improve the performance of the code compared to using custom methods for manipulating arrays of arrays.

4 - Cross-browser support: flatten is a native method of JavaScript, which means it is supported by all modern browsers. This means that it can be reliably used in all JavaScript projects, regardless of the browser used by users.

Overall, using flatten in JavaScript can simplify the manipulation of arrays of arrays, improve the performance of the code, and make the code more readable and maintainable.

Installation

You can install flatten-ts via npm:

npm install flatten-ts

Usage

To use Flatten-ts, import the library into your TypeScript code and call the flatten() function, passing in the array of arrays to be flattened.

import { flatten } from 'flatten-ts';

const obj = {
  name: 'John Doe',
  age: 30,
  email: '[email protected]',
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345',
  },
  phoneNumbers: [
    {
      type: 'home',
      number: '555-1234',
    },
    {
      type: 'work',
      number: '555-5678',
    },
  ],
};

const flatten = new Flatten();
flatten.populate(complexObject);

console.log(flatten.getCollection());
/*
{
  name: 'John Doe',
  age: 30,
  email: '[email protected]',
  'address.street': '123 Main St',
  'address.city': 'Anytown',
  'address.state': 'CA',
  'address.zip': '12345',
  'phoneNumbers.0.type': 'home',
  'phoneNumbers.0.number': '555-1234',
  'phoneNumbers.1.type': 'work',
  'phoneNumbers.1.number': '555-5678'
}

*/

Use external Json

You can import a JSON file into TypeScript using the import statement and specifying the file path of the JSON file. The imported JSON object can then be assigned to a variable or used directly in your code.

import { flatten } from 'flatten-ts';

// optional: you can import a json from external source
import extJson from '../output.json';

const flatObj = new Flatten();
flatObj.populate(extJson);

As you can see in the example above, when flatten encounters an array, it inserts the numeric index to identify the object within the array.

Update a property

Flatten is very convenient when you need to update a property. The library has a dedicated method for this purpose.

const obj = {
  name: 'John Doe',
  age: 30,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345',
  },
  phoneNumbers: [
    {
      type: 'home',
      number: '555-1234',
    },
    {
      type: 'work',
      number: '555-5678',
    },
  ],
};

const flatten = new Flatten();
flatten.populate(complexObject);

flatten.update('name', 'Mario Rossi');
flatten.update('address.city', 'Venice');
flatten.update('phoneNumbers.0.number', '123-456.789');

console.log(flatten.getCollection());
/*
{
  name: 'Mario Rossi',
  age: 30,
  'address.street': '123 Main St',
  'address.city': 'Venice',
  'address.state': 'CA',
  'address.zip': '12345',
  'phoneNumbers.0.type': 'home',
  'phoneNumbers.0.number': '123-456.789',
  'phoneNumbers.1.type': 'work',
  'phoneNumbers.1.number': '555-5678'
}
*/

Flatten allows you to update multiple properties with a single command, using the '*' character. Suppose you need to update the due date of an exam for an entire array.

const obj = {
  students: [
    {
      id: '001',
      name: 'John Doe',
      email: '[email protected]',
      age: 22,
      major: 'Computer Science',
      courses: [
        {
          code: 'CSC101',
          title: 'Introduction to Computer Science',
          credits: 3,
          grade: 'A',
          dueDate: '2023-05-30',
        },
        {
          code: 'CSC201',
          title: 'Data Structures and Algorithms',
          credits: 4,
          grade: 'B+',
          dueDate: '2023-06-15',
        },
        {
          code: 'MAT101',
          title: 'Calculus I',
          credits: 4,
          grade: 'A-',
          dueDate: '2023-05-10',
        },
      ],
    },
  ],
};

const flatten = new Flatten();
flatten.populate(obj);

flatten.update('students.*.dueDate', '2024-01-01');

You can use multiple '*' int the path.

Then, you can use the flatten function to flatten any array of any depth into a single-level array:

Contributing

To contribute to the Flatten-ts project, please read the CONTRIBUTING.md file and submit a pull request with your proposed changes.

License

Flatten-ts is released under the MIT license. Please read the LICENSE file for further information about the license.

I hope this structure is helpful as a starting point for the documentation of the Flatten-ts project. If you need any further assistance, please don't hesitate to ask.