This class wraps the Node.js fs file system API so that you can work with a file system folder as a Map.
JavaScript usage #
Pass the FileMap constructor a location that’s either a file: URL or a file system path. A relative path will be resolved with respect to the current working directory.
To get a folder relative to the JavaScript module itself, construct a URL relative to import.meta.url.
import { FileMap } from "@weborigami/async-tree";
// Get the tree of files in the "assets" folder next to this module
const files = new FileMap(new URL("assets", import.meta.url));
// Get a stylesheet from the assets folder
const styles = await files.get("styles.css");
API #
A file system folder as a Map.
File values are returned as Uint8Array instances. The underlying Node fs API returns file contents as instances of the Node-specific Buffer class, but that class has some incompatible method implementations; see https://nodejs.org/api/buffer.html#buffers-and-typedarrays. For greater compatibility, files are returned as standard Uint8Array instances instead.
new FileMap(location)
- location: any
delete(key)
- key: any
Returns: boolean
Removes the entry for the given key, return true if an entry was removed and false if there was no entry for the key.
If the readOnly property is true, calling this method throws a
TypeError.
get(key)
- key: any
Returns: any
Returns the value associated with the key, or undefined if there is none.
keys()
Returns: any
Returns a new Iterator object that contains the keys for each element in
the map in insertion order.
set(key, value)
- key: any
- value: any
Returns: FileMap
Adds a new entry with a specified key and value to this Map, or updates an existing entry if the key already exists.
If the readOnly property is true, calling this method throws a TypeError.
clear()
Returns: void
Removes all key/value entries from the map.
Unlike the standard Map.prototype.clear(), this method invokes an
overridden keys() and delete() to ensure proper behavior in subclasses.
If the readOnly property is true, calling this method throws a
TypeError.
entries()
Returns: MapIterator<[any, any]>
Returns a new Iterator object that contains a two-member array of [key,
value] for each element in the map in insertion order.
Unlike the standard Map.prototype.clear(), this method invokes an
overridden keys() and get() to ensure proper behavior in subclasses.
forEach(callback, [thisArg])
- callback: (value: any, key: any, thisArg: any) => void
- thisArg: any
Returns: void
Calls callback once for each key/value pair in the map, in insertion order.
Unlike the standard Map.prototype.forEach(), this method invokes an
overridden entries() to ensure proper behavior in subclasses.
has(key)
- key: any
Returns: any
Returns true if the given key appears in the set returned by keys().
It doesn’t matter whether the value returned by get() is defined or not.
If the requested key has a trailing slash but has no associated value, but the alternate form with a slash does appear, this returns true.
parent
Type: SyncMap
The parent of this node in a tree.
readOnly
Type: boolean
True if the object is read-only. This will be true if the get() method has
been overridden but set() and delete() have not.
size
Type: any
Returns the number of keys in the map.
The size property invokes an overridden keys() to ensure proper
behavior in subclasses. Because a subclass may not enforce a direct
correspondence between keys() and get(), the size may not reflect the
number of values that can be retrieved.
values()
Returns: MapIterator<[any]>
Returns a new Iterator object that contains the values for each element
in the map in insertion order.