Register to get access to free programming courses with interactive exercises

Manipulating the virtual file system JS: Trees

The library we use to build trees is designed only for immutable file structures. I.e., once it's created, it cannot be modified. But it is possible to make a new structure based on the old one with some parts being changed.

The immutable structure is not some random choice. Such a structure is easier to debug and there are fewer chances to make mistakes. Also it allows you to fully dig into using higher-order functions.

Basic operations with nodes

The @hexlet/immutable-fs-trees package allows you to not only create files but also to extract data from previously created files and directories using basic operations. They allow you to hide the internal structure of the tree itself:

import {
  mkfile, mkdir, getChildren, getMeta, getName,
} from '@hexlet/immutable-fs-trees';

const tree = mkdir('/', [mkfile('hexlet.log')], { hidden: true });
getName(tree); // '/'
getMeta(tree).hidden; // true

const [file] = getChildren(tree);
getName(file); // 'hexlet.log'

// The file has no metadata
getMeta(file).unknown; // undefined

// A wrong usage
// Files have no children
getChildren(file);

Additionally, there are two functions in the package to check the type. They can be used to selectively work with files and directories:

import {
  mkfile, mkdir, isFile, isDirectory, getChildren,
} from '@hexlet/immutable-fs-trees';

const tree = mkdir('/', [mkfile('hexlet.log')], { hidden: true });
isDirectory(tree); // true
isFile(tree); // false

const [file] = getChildren(tree);
isFile(file); // true
isDirectory(file); // false

The above operations are enough to perform any operations on files and directories. Let's start with the simplest ones, which do not require recursive traversal.

Processing

Any immutable-style processing boils down to generating new data from old. Below, we'll implement some processing examples that reveal this idea.

Changing the file name

const file = mkfile('one', { size: 35 });

// When renaming, it's important that we keep the metadata
// _ – lodash
const newMeta = _.cloneDeep(getMeta(file));
const newFile = mkfile('new name', newMeta);

This actually creates a new file with the metadata of the old one. Before creating a new file, the metadata is cloned (using deep cloning). Why? Objects are referential data, so if you do not clone them, the new file metadata will contain the old metadata. As soon as we want to change something, we change the new one and thus break the old one:

const file = mkfile('one', { size: 35 });

// When renaming, it's important that we keep the metadata
const newMeta = getMeta(file);
// Boom! The file's metadata has also changed
newMeta.size = 15;
const newFile = mkfile('new name', newMeta);

console.log(getMeta(file)); // { size: 15 }

Sorting the contents of a directory

// Sorting in reverse order

const tree = mkdir('/', [
  mkfile('one'),
  mkfile('two'),
  mkdir('three'),
]);

const children = getChildren(tree);
const newMeta = _.cloneDeep(getMeta(tree));
// reverse changes the array, so we clone it
const newChildren = [...children].reverse();
const tree2 = mkdir(getName(tree), newChildren, newMeta);
console.log(tree2);
// => {
// =>   name: '/',
// =>   children: [
// =>     { name: 'three', children: [], meta: {}, type: 'directory' },
// =>     { name: 'two', meta: {}, type: 'file' },
// =>     { name: 'one', meta: {}, type: 'file' }
// =>   ],
// =>   meta: {},
// =>   type: 'directory'
// => }

Updating the contents of the directory

// Making the names of the directories and files lower case
// inside a specific directory

const tree = mkdir('/', [
  mkfile('oNe'),
  mkfile('Two'),
  mkdir('THREE'),
]);

const children = getChildren(tree);
const newChildren = children.map((child) => {
  const name = getName(child);
  const newMeta = _.cloneDeep(getMeta(child));
  if (isDirectory(child)) {
    return mkdir(name.toLowerCase(), getChildren(child), newMeta);
  }
  return mkfile(name.toLowerCase(), newMeta);
});
// Be sure to copy the metadata
const newMeta = _.cloneDeep(getMeta(tree));
const tree2 = mkdir(getName(tree), newChildren, newMeta);
console.log(tree2);
// => {
// =>   name: '/',
// =>   children: [
// =>     { name: 'one', meta: {}, type: 'file' },
// =>     { name: 'two', meta: {}, type: 'file' },
// =>     { name: 'three', children: [], meta: {}, type: 'directory' }
// =>   ],
// =>   meta: {},
// =>   type: 'directory'
// => }

Deleting files within a directory

const tree = mkdir('/', [
  mkfile('one'),
  mkfile('two'),
  mkdir('three'),
]);

const children = getChildren(tree);
const newChildren = children.filter(isDirectory);
const newMeta =  _.cloneDeep(getMeta(tree));
const tree2 = mkdir(getName(tree), newChildren, newMeta);
console.log(tree2);
// => {
// =>   name: '/',
// =>   children: [ { name: 'three', children: [], meta: {}, type: 'directory' } ],
// =>   meta: {},
// =>   type: 'directory'
// => }

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs
profession
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.