Parser generator for JavaScript that produces fast parsers with excellent error reporting
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Internal utility modules providing helper functions for array operations, object manipulation, and class inheritance used throughout PEG.js.
Collection of array manipulation functions for common operations like searching, mapping, and iteration.
/**
* Array utility functions
*/
const arrays = {
/**
* Generate an array of integers in a range
* @param start - Starting number (inclusive)
* @param stop - Ending number (exclusive)
* @returns Array of integers from start to stop-1
*/
range: function(start, stop),
/**
* Find first element matching value or predicate
* @param array - Array to search
* @param valueOrPredicate - Value to match or predicate function
* @returns First matching element or undefined
*/
find: function(array, valueOrPredicate),
/**
* Find index of first element matching value or predicate
* @param array - Array to search
* @param valueOrPredicate - Value to match or predicate function
* @returns Index of first match or -1 if not found
*/
indexOf: function(array, valueOrPredicate),
/**
* Check if array contains value or element matching predicate
* @param array - Array to search
* @param valueOrPredicate - Value to match or predicate function
* @returns True if array contains matching element
*/
contains: function(array, valueOrPredicate),
/**
* Iterate over array elements
* @param array - Array to iterate
* @param iterator - Function called for each element (element, index)
*/
each: function(array, iterator),
/**
* Transform array elements using iterator function
* @param array - Array to transform
* @param iterator - Transform function (element, index) => newElement
* @returns New array with transformed elements
*/
map: function(array, iterator),
/**
* Extract property values from array of objects
* @param array - Array of objects
* @param key - Property key to extract
* @returns Array of property values
*/
pluck: function(array, key),
/**
* Test if all elements pass predicate test
* @param array - Array to test
* @param predicate - Test function (element) => boolean
* @returns True if all elements pass test
*/
every: function(array, predicate),
/**
* Test if any element passes predicate test
* @param array - Array to test
* @param predicate - Test function (element) => boolean
* @returns True if any element passes test
*/
some: function(array, predicate)
};Usage Examples:
const peg = require("pegjs");
// Note: Utility modules are internal and not directly exported
// These examples show conceptual usage
// Generate range of numbers
const numbers = arrays.range(1, 6); // [1, 2, 3, 4, 5]
// Find elements
const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 }
];
const alice = arrays.find(users, user => user.name === "Alice");
const ageIndex = arrays.indexOf(users, user => user.age === 30); // 1
// Transform arrays
const names = arrays.pluck(users, "name"); // ["Alice", "Bob"]
const adults = arrays.every(users, user => user.age >= 18); // true
// Iterate
arrays.each(users, (user, index) => {
console.log(`User ${index}: ${user.name}`);
});Functions for object manipulation including key/value extraction, cloning, and setting defaults.
/**
* Object utility functions
*/
const objects = {
/**
* Get array of object's own property keys
* @param object - Object to get keys from
* @returns Array of property keys
*/
keys: function(object),
/**
* Get array of object's own property values
* @param object - Object to get values from
* @returns Array of property values
*/
values: function(object),
/**
* Create shallow clone of object
* @param object - Object to clone
* @returns New object with same properties
*/
clone: function(object),
/**
* Set default values on object (modifies original object)
* @param object - Object to set defaults on
* @param defaults - Object with default values
*/
defaults: function(object, defaults)
};Usage Examples:
// Note: Utility modules are internal and not directly exported
// These examples show conceptual usage
const config = {
format: "commonjs",
optimize: "speed"
};
// Get keys and values
const keys = objects.keys(config); // ["format", "optimize"]
const values = objects.values(config); // ["commonjs", "speed"]
// Clone object
const configCopy = objects.clone(config);
// Set defaults
const options = { format: "amd" };
objects.defaults(options, {
format: "bare", // Won't override existing value
optimize: "speed", // Will be added
cache: false // Will be added
});
// options is now: { format: "amd", optimize: "speed", cache: false }Utility for implementing classical inheritance patterns in JavaScript.
/**
* Class inheritance utilities
*/
const classes = {
/**
* Set up prototype-based inheritance between classes
* @param child - Constructor function for child class
* @param parent - Constructor function for parent class
*/
subclass: function(child, parent)
};Usage Examples:
// Note: Utility modules are internal and not directly exported
// This example shows how it's used within PEG.js
// Create parent class
function BaseError(message) {
this.message = message;
this.name = "BaseError";
}
// Create child class
function GrammarError(message, location) {
BaseError.call(this, message);
this.name = "GrammarError";
this.location = location;
}
// Set up inheritance
classes.subclass(GrammarError, BaseError);
// Now GrammarError inherits from BaseError
const error = new GrammarError("Invalid rule", { line: 1, column: 5 });
console.log(error instanceof BaseError); // true
console.log(error instanceof GrammarError); // trueThese utility modules are used internally throughout PEG.js:
While these modules are not part of the public API, understanding their functionality helps when:
These utilities are designed to work in older JavaScript environments and provide cross-browser compatibility:
Array.prototype.indexOfInstall with Tessl CLI
npx tessl i tessl/npm-pegjs