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

files-to-tree

v2.0.0

Published

Convert file path objects to hierarchical tree structures

Readme

files-to-tree

npm version TypeScript

A lightweight TypeScript library that converts flat file path objects into hierarchical tree structures. Perfect for representing file systems, directory structures, or any hierarchical data in a clean, tree-like format.

Installation

npm install files-to-tree
yarn add files-to-tree
pnpm add files-to-tree

Quick Start

import { convertFilesToTreeItems } from 'files-to-tree';

const files = {
  'src/index.ts': 'export * from "./utils"',
  'src/utils/helpers.ts': 'export function help() {}',
  'src/components/Button.tsx': 'export const Button = () => {}',
  'package.json': '{"name": "my-app"}',
  'README.md': '# My App',
};

const tree = convertFilesToTreeItems(files);
console.log(tree);

// Output:
// [
//   'README.md',
//   'package.json',
//   [
//     'src',
//     ['components', 'Button.tsx'],
//     'index.ts',
//     ['utils', 'helpers.ts']
//   ]
// ]

API Reference

convertFilesToTreeItems(files: FilesObject): TreeItem[]

Converts a flat object of file paths to a hierarchical tree structure.

Parameters:

  • files (FilesObject): An object where keys are file paths and values are file contents (contents are used for sorting but not included in output)

Returns:

  • TreeItem[]: An array representing the tree structure

Types

// A tree item can be either a string (file/empty dir) or a tuple (directory with children)
type TreeItem = string | [string, ...TreeItem[]];

// Input object type
interface FilesObject {
  [path: string]: string;
}

Examples

Basic File Structure

import { convertFilesToTreeItems } from 'files-to-tree';

const files = {
  'index.html': '<html>...</html>',
  'styles.css': 'body { margin: 0; }',
  'script.js': 'console.log("Hello");',
};

const tree = convertFilesToTreeItems(files);
// Result: ['index.html', 'script.js', 'styles.css']

Nested Directories

const files = {
  'src/components/Header/index.ts': 'export',
  'src/components/Header/Header.tsx': 'component',
  'src/components/Footer/Footer.tsx': 'component',
  'src/utils/api.ts': 'api functions',
  'public/favicon.ico': 'icon',
  'package.json': 'manifest',
};

const tree = convertFilesToTreeItems(files);
// Result:
// [
//   'package.json',
//   ['public', 'favicon.ico'],
//   [
//     'src',
//     [
//       'components',
//       ['Footer', 'Footer.tsx'],
//       ['Header', 'Header.tsx', 'index.ts']
//     ],
//     ['utils', 'api.ts']
//   ]
// ]

Usage with Different Import Styles

// Named import
import { convertFilesToTreeItems } from 'files-to-tree';

// Default import
import convertFilesToTreeItems from 'files-to-tree';

// CommonJS
const { convertFilesToTreeItems } = require('files-to-tree');

Features

  • 🚀 Lightweight: Zero dependencies, minimal footprint
  • 📦 TypeScript First: Full TypeScript support with detailed type definitions
  • 🔧 Multiple Formats: Supports CommonJS, ESM, and TypeScript
  • 📊 Sorted Output: Files and directories are automatically sorted alphabetically
  • 🎯 Simple API: Single function with intuitive input/output
  • ✅ Well Tested: Comprehensive test coverage
  • 📱 Universal: Works in Node.js, browsers, and edge environments

Use Cases

  • File System Representation: Display directory structures in UI components
  • Documentation: Generate file tree views for project documentation
  • Code Analysis: Analyze project structure and dependencies
  • Build Tools: Process file hierarchies in build pipelines
  • API Responses: Structure file listings in REST APIs

Development

# Install dependencies
npm install

# Run tests
npm test

# Run tests with coverage
npm run test:coverage

# Build the package
npm run build

# Development mode (watch)
npm run dev

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

MIT © [KlyneChrysler]

Changelog

2.0.0

  • BREAKING: Fixed TypeScript compilation issues with strict mode
  • Improved error handling and type safety
  • Better handling of edge cases (empty paths, undefined values)
  • Enhanced sorting algorithm for consistent output
  • Full TypeScript declaration file generation

1.0.0

  • Initial release
  • Basic file-to-tree conversion functionality
  • TypeScript support
  • Comprehensive test coverage