Specialized formats for organizing, processing, storing, and retrieving data within a computer's memory. Data structures define the relationships between data elements and the operations that can be performed on them, with type-safe nested object manipulation, path-based data access with dot notation, built-in parsers for query strings, form data, and arguments, and chainable API for fluent data operations.
The choice of data structure significantly impacts the efficiency and performance of algorithms and overall program execution. Stackpress provides enhanced data structures that extend native JavaScript collections with additional functionality for common development patterns.
Hierarchical data management utilities for nested objects and arrays. The Nest class provides type-safe access to deeply nested data structures with convenient methods for manipulation, iteration, and serialization.
import { nest, Nest, ReadonlyNest } from '@stackpress/lib';
type NestMap = {
database: Record<string, { host: string, port: number}>
};
const callable = nest<NestMap>({
database: {
postgres: {
host: 'localhost',
port: 5432
}
}
});
const config = new Nest<NestMap>({
database: {
postgres: {
host: 'localhost',
port: 5432
}
}
});
const readonly = new ReadonlyNest<NestMap>({
database: {
postgres: {
host: 'localhost',
port: 5432
}
}
});The following properties are available when instantiating a Nest.
| Property | Type | Description |
|---|---|---|
data |
M |
Raw nested data structure |
size |
number |
Total number of top-level keys |
withArgs |
ArgString |
Parser for terminal args |
withFormData |
FormData |
Parser for multipart/form-data |
withPath |
PathString |
Parser for path notations |
withQuery |
QueryString |
Parser for query string |
The following example shows how to retrieve data from a nest using type-safe path navigation.
config.get('database', 'postgres', 'port'); //--> 5432
config.get<number>('database', 'postgres', 'port'); //--> 5432Parameters
| Parameter | Type | Description |
|---|---|---|
...path |
Key[] |
A path of object keys leading to the value |
Returns
The value given the object key path. If you don't provide a generic, will follow the type map provided.
The following example shows how to add or update data in a nest with chainable operations.
config.set('database', 'postgres', 'port', 5432); //--> Nest
config.set({ foo: 'bar', baz: 'qux' }); //--> NestParameters
| Parameter | Type | Description |
|---|---|---|
...path |
any[] |
A path of object keys leading to the value to be set, with the last argument being the value |
Returns
The nest object to allow chainability.
The following example shows how to check if an object key path has been set.
config.has('database', 'postgres', 'port'); //--> trueParameters
| Parameter | Type | Description |
|---|---|---|
...path |
Key[] |
A path of object keys leading to the value to check |
Returns
true if the object key path is set, false otherwise.
The following example shows how to delete a value from the nested structure.
config.delete('database', 'postgres', 'port'); //--> NestParameters
| Parameter | Type | Description |
|---|---|---|
...path |
Key[] |
A path of object keys leading to the value to be deleted |
Returns
The nest object to allow chainability.
The following example shows how to purge all data from the nest.
config.clear(); //--> NestReturns
The nest object to allow chainability.
The following example shows how to get the top-level keys in a nest.
config.keys(); //--> [ 'database' ]Returns
An array of object key strings.
The following example shows how to get the top-level values in a nest.
config.values(); //--> [ { postgres: { host: 'localhost', port: 5432 } } ]Returns
An array of arbitrary values.
The following example shows how to get the top-level entries in a nest as key-value pairs.
config.entries(); //--> [ ['database', { postgres: { host: 'localhost', port: 5432 } }] ]Returns
An array of key-value pairs.
The following example shows how to retrieve data from a nest using a dot-separated key path.
config.path('database.postgres.port'); //--> 5432
config.path<number>('database.postgres.port'); //--> 5432
config.path('database.mysql.port', 3306); //--> 3306Parameters
| Parameter | Type | Description |
|---|---|---|
path |
string |
An object key path separated by dots leading to the value |
defaults |
TypeOf<T> |
Default value to return if path doesn't exist |
Returns
The value given the object key path. If you don't provide a generic, will follow the type map provided.
The following example shows how to iterate over data at a specific path with async callback support.
await config.forEach('database', (value, key) => {
console.log(key, value);
return true; // continue iteration
});Parameters
| Parameter | Type | Description |
|---|---|---|
...path |
any[] |
A path of object keys leading to the data to iterate, with the last argument being the callback function |
Returns
A promise that resolves to true if all iterations completed, false if stopped early.
The following example shows how to generate a JSON string from a nest with formatting options.
config.toString();
//--> { "database": { "postgres": { "host": "localhost", "port": 5432 } } }
config.toString(false); //--> {"database":{"postgres":{"host":"localhost","port":5432}}}Parameters
| Parameter | Type | Description |
|---|---|---|
expand |
boolean |
Whether to format the JSON with indentation (default: true) |
...path |
Key[] |
Optional path to convert only a subset of the data |
Returns
A JSON string derived from the nest.
Creates a callable Nest instance that can be invoked as a function to get nested values with functional programming patterns.
import { nest } from '@stackpress/lib';
const config = nest<NestMap>({
database: {
postgres: {
host: 'localhost',
port: 5432
}
}
});
config('database', 'postgres', 'host'); // 'localhost'
config<string>('database', 'postgres', 'host'); // 'localhost'Creates a callable Map instance that can be invoked as a function to get values. The enhanced Map provides additional functionality while maintaining compatibility with the native Map interface.
import { map } from '@stackpress/lib';
const userMap = map<string, User>([
['john', { name: 'John', age: 30 }],
['jane', { name: 'Jane', age: 25 }]
]);
// Use as function to get values
const john = userMap('john'); // { name: 'John', age: 30 }
// Use as Map
userMap.set('bob', { name: 'Bob', age: 35 });
console.log(userMap.size); // 3The following properties are available on a callable map.
| Property | Type | Description |
|---|---|---|
size |
number |
Number of key-value pairs in the map |
The following example shows how to use the map as a function for convenient value access.
const value = myMap('key'); // Equivalent to myMap.get('key')Parameters
| Parameter | Type | Description |
|---|---|---|
key |
K |
The key to retrieve the value for |
Returns
The value associated with the key, or undefined if not found.
The following example shows how to set key-value pairs with method chaining support.
myMap.set('newKey', 'newValue');Parameters
| Parameter | Type | Description |
|---|---|---|
key |
K |
The key to set |
value |
V |
The value to associate with the key |
Returns
The Map instance to allow method chaining.
The following example shows how to get values by key using the standard Map interface.
const value = myMap.get('key');Parameters
| Parameter | Type | Description |
|---|---|---|
key |
K |
The key to retrieve the value for |
Returns
The value associated with the key, or undefined if not found.
The following example shows how to check if a key exists in the map.
const exists = myMap.has('key'); // true or falseParameters
| Parameter | Type | Description |
|---|---|---|
key |
K |
The key to check for existence |
Returns
true if the key exists, false otherwise.
The following example shows how to delete a key-value pair from the map.
const deleted = myMap.delete('key'); // true if deleted, false if not foundParameters
| Parameter | Type | Description |
|---|---|---|
key |
K |
The key to delete |
Returns
true if the key was deleted, false if the key didn't exist.
The following example shows how to remove all key-value pairs from the map.
myMap.clear();
console.log(myMap.size); // 0Returns
undefined
The following example shows how to iterate over all key-value pairs in the map.
for (const [key, value] of myMap.entries()) {
console.log(key, value);
}Returns
An iterator of [key, value] pairs.
The following example shows how to iterate over all keys in the map.
for (const key of myMap.keys()) {
console.log(key);
}Returns
An iterator of keys.
The following example shows how to iterate over all values in the map.
for (const value of myMap.values()) {
console.log(value);
}Returns
An iterator of values.
The following example shows how to execute a function for each key-value pair in the map.
myMap.forEach((value, key, map) => {
console.log(`${key}: ${value}`);
});Parameters
| Parameter | Type | Description |
|---|---|---|
callback |
(value: V, key: K, map: Map<K, V>) => void |
Function to execute for each element |
Returns
undefined
Creates a callable Set instance that can be invoked as a function to get values by index. The enhanced Set provides indexed access while maintaining compatibility with the native Set interface.
import { set } from '@stackpress/lib';
const tags = set(['javascript', 'typescript', 'node.js']);
// Use as function to get by index
const firstTag = tags(0); // 'javascript'
const secondTag = tags(1); // 'typescript'
// Use as Set
tags.add('react');
console.log(tags.size); // 4The following properties are available on a callable set.
| Property | Type | Description |
|---|---|---|
size |
number |
Number of values in the set |
The following example shows how to use the set as a function to get values by index position.
const value = mySet(0); // Get first item
const value2 = mySet(2); // Get third itemParameters
| Parameter | Type | Description |
|---|---|---|
index |
number |
The index of the value to retrieve |
Returns
The value at the specified index, or undefined if index is out of bounds.
The following example shows how to get values by index using the explicit index method.
const value = mySet.index(0); // Same as mySet(0)Parameters
| Parameter | Type | Description |
|---|---|---|
index |
number |
The index of the value to retrieve |
Returns
The value at the specified index, or undefined if index is out of bounds.
The following example shows how to add values to the set with method chaining support.
mySet.add('newValue');Parameters
| Parameter | Type | Description |
|---|---|---|
value |
V |
The value to add to the set |
Returns
The Set instance to allow method chaining.
The following example shows how to check if a value exists in the set.
const exists = mySet.has('value'); // true or falseParameters
| Parameter | Type | Description |
|---|---|---|
value |
V |
The value to check for existence |
Returns
true if the value exists, false otherwise.
The following example shows how to delete a value from the set.
const deleted = mySet.delete('value'); // true if deleted, false if not foundParameters
| Parameter | Type | Description |
|---|---|---|
value |
V |
The value to delete |
Returns
true if the value was deleted, false if the value didn't exist.
The following example shows how to remove all values from the set.
mySet.clear();
console.log(mySet.size); // 0Returns
undefined
The following example shows how to iterate over all value pairs in the set.
for (const [value1, value2] of mySet.entries()) {
console.log(value1, value2); // Both values are the same in a Set
}Returns
An iterator of [value, value] pairs.
The following example shows how to iterate over all values in the set.
for (const value of mySet.values()) {
console.log(value);
}Returns
An iterator of values.
The following example shows how to execute a function for each value in the set.
mySet.forEach((value1, value2, set) => {
console.log(value1); // value1 and value2 are the same
});Parameters
| Parameter | Type | Description |
|---|---|---|
callback |
(value: V, value2: V, set: Set<V>) => void |
Function to execute for each element |
Returns
undefined