📚 StringArrayManager
A robust class for managing arrays of strings with full support for validation, undo/redo, event handling, logging, and utility operations like search, pagination, and merging. It enforces uniqueness and optional case-insensitivity.
Import
1import { StringArrayManager } from "hbh-nodes";
Constructor
1new StringArrayManager(initialArray = [], options = {})
Parameters:
initialArray– Optional array of strings to initialize.options– Optional configuration object:
| Option | Type | Default | Description |
|---|---|---|---|
| maxHistory | number | 100 | Maximum undo history length |
| caseInsensitive | boolean | false | Normalize strings to lowercase for comparison |
| maxItems | number | Infinity | Maximum number of strings allowed |
| validate | function | null | Function (value) => boolean for custom validation |
| returnThis | boolean | false | If true, mutating methods return this instead of status objects |
| saveHistory | boolean | true | Enable/disable saving history for undo/redo |
Example:
1const manager = new StringArrayManager(["apple", "banana"], { caseInsensitive: true });
Events
Subscribe to events emitted during operations:
1manager.on("add", ({ value }) => console.log("Added:", value));2manager.on("remove", ({ value }) => console.log("Removed:", value));3manager.off("add", callback); // Remove listener
Available events include: add, remove, replace, delete, clear, reset, sort, undo, redo, merge, addBulk, removeBulk.
Adding & Removing Strings
add(value)
Adds a single string if valid, unique, and within limits.
1manager.add("orange");
addBulk(values)
Adds multiple strings with detailed results for added and skipped items.
1manager.addBulk(["kiwi", "pear", "apple"]);
remove(value) / removeBulk(values)
Remove single or multiple strings.
1manager.remove("banana");2manager.removeBulk(["kiwi", "apple"]);
replace(oldValue, newValue)
Replace a string with a new one (valid & unique).
1manager.replace("apple", "grape");
delete(index)
Remove by index.
1manager.delete(0);
Array Utilities
sortManual(compareFn)
Sort array manually, optionally using a comparator.
1manager.sortManual(); // Default lexicographical2manager.sortManual((a, b) => a.length - b.length);
search(pattern)
Search strings using a substring or regex.
1manager.search("app"); // substring search2manager.search(/^g/); // regex search
clear() / reset(initialArray)
Clear all strings or reset to a new array.
1manager.clear();2manager.reset(["pear", "melon"]);
getAll() / length() / stats()
Retrieve array, length, or statistics (longest, shortest, average length).
1manager.getAll();2manager.length();3manager.stats();
Undo / Redo
1manager.undo();2manager.redo();
Supports undoing any change with full history up to maxHistory.
Logging
1manager.enableLogging(true);2manager.getLog();
Tracks operations with timestamps and details.
Validation
Custom validation function:
1manager.setValidation(val => val.length > 3 && /^[a-z]+$/.test(val));
JSON Serialization
1const json = manager.toJSON();2manager.fromJSON(json);
Utility Methods
has(value)– Check if value exists.get(index)– Get value by index.indexOf(value)– Find index of value.clone()– Create a new instance with same array and options.diff(prevArray)– Compare current array with a previous array (added/removed).merge(values)– Merge multiple strings with duplicate/limit handling.getPage(pageNum, pageSize)– Paginate array.
Example Usage
1const manager = new StringArrayManager(["apple", "banana"], { caseInsensitive: true });3// Add strings4manager.add("Cherry");5manager.addBulk(["Date", "Fig"]);7// Remove strings8manager.remove("banana");10// Replace strings11manager.replace("apple", "Apricot");13// Undo / Redo14manager.undo();15manager.redo();17// Search18manager.search(/a/i);20// Pagination21manager.getPage(1, 2);23// Stats24manager.stats();26// Event listening27manager.on("add", ({ value }) => console.log("Added:", value));29// Logging30manager.enableLogging(true);31console.log(manager.getLog());
StringArrayManager is ideal for managing dynamic lists of strings with full control, safe operations, history tracking, and extensibility.