# precedent

> Precedent Meta-Templating

Latest version **1.0.17** (published 2026-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install precedent
pnpm add precedent
yarn add precedent
bun add precedent
```

Provides the command `precedent`.

## Health

**Score 45/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.17 |
| Published | 2026-02-27 |
| First published | 2017-05-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 62.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Steven Velozo |
| Maintainers | stevenvelozo |
| Keywords | logging |

## Links

- npm: https://www.npmjs.com/package/precedent
- Repository: https://github.com/stevenvelozo/precedent
- Homepage: https://github.com/stevenvelozo/precedent#readme
- Issues: https://github.com/stevenvelozo/precedent/issues
- npm.io page: https://npm.io/package/precedent

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.17 (latest) — 2026-02-27
- 1.0.16 — 2026-02-18
- 1.0.15 — 2023-06-03
- 1.0.14 — 2023-05-24
- 1.0.13 — 2023-05-23
- 1.0.12 — 2023-05-23
- 1.0.11 — 2023-05-10
- 1.0.10 — 2023-05-01
- 1.0.9 — 2023-02-21
- 1.0.8 — 2023-02-21
- 1.0.7 — 2022-10-31
- 1.0.6 — 2017-06-09
- 1.0.5 — 2017-06-09
- 1.0.4 — 2017-05-14
- 1.0.3 — 2017-05-04
- … 2 more at https://npm.io/package/precedent/versions

## README

# Precedent

A meta-templating engine for processing text streams with pattern-based template expressions. Define start/end pattern markers with string or function parsers, and Precedent handles nested pattern resolution automatically.

[![Coverage Status](https://coveralls.io/repos/github/stevenvelozo/precedent/badge.svg?branch=master)](https://coveralls.io/github/stevenvelozo/precedent?branch=master)
[![Build Status](https://github.com/stevenvelozo/precedent/workflows/Precedent/badge.svg)](https://github.com/stevenvelozo/precedent/actions)
[![npm version](https://badge.fury.io/js/precedent.svg)](https://badge.fury.io/js/precedent)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

---

## Features

- **Pattern-Based Parsing** - Define start/end markers to identify template regions in text
- **String or Function Parsers** - Replace patterns with static strings or dynamic function output
- **Nested Pattern Support** - Overlapping pattern prefixes (e.g. `<%`, `<%=`, `<$$`) resolve correctly in a single pass
- **Word Tree Architecture** - Patterns are stored in a tree structure for efficient matching
- **Data Passing** - Pass a data object through to parser functions for context-aware rendering
- **Browser Compatible** - Works in both Node.js and browser environments
- **Zero Dependencies** - No external runtime dependencies

## Installation

```bash
npm install precedent
```

## Quick Start

```javascript
const Precedent = require('precedent');

const precedent = new Precedent();

// Add a simple string substitution pattern
precedent.addPattern('{Name', '}', 'David Bowie');

// Parse a string containing the pattern
precedent.parseString('Hello, {Name}!');
// => "Hello, David Bowie!"
```

## Usage

### String Substitution

Replace patterns with a fixed string value. Content between the start and end markers is ignored:

```javascript
const precedent = new Precedent();

precedent.addPattern('{Name', '}', 'David Bowie');

precedent.parseString('A message for {Name}.');
// => "A message for David Bowie."

// Content between markers is ignored for string substitutions
precedent.parseString('A message for {Name IGNORED TEXT}.');
// => "A message for David Bowie."
```

### Function-Based Parsing

Pass a function as the parser to dynamically process the content between markers:

```javascript
const precedent = new Precedent();

precedent.addPattern('{Length', '}', (pString) => { return pString.length; });

precedent.parseString('The length is {Length some text}.');
// => "The length is  some text."  (length of " some text")
```

### Passing Data to Parsers

A data object can be passed as the second argument to `parseString`, which is then available to parser functions:

```javascript
const precedent = new Precedent();

precedent.addPattern('<%=', '%>', (pContent, pData) =>
{
	return pData[pContent.trim()] || '';
});

precedent.parseString('Hello, <%= username %>!', { username: 'Steven' });
// => "Hello, Steven!"
```

## API

### `addPattern(patternStart, patternEnd, parser)`

Add a pattern to the parse tree.

| Parameter | Type | Description |
|-----------|------|-------------|
| `patternStart` | `String` | The opening marker for the pattern |
| `patternEnd` | `String` | The closing marker for the pattern |
| `parser` | `String` or `Function` | Replacement string, or function receiving `(content, data)` |

Returns `true` if the pattern was added successfully.

### `parseString(contentString, data)`

Parse a string against all registered patterns.

| Parameter | Type | Description |
|-----------|------|-------------|
| `contentString` | `String` | The text to parse |
| `data` | `Object` | Optional data object passed to parser functions |

Returns the parsed string.

## Part of the Retold Framework

Precedent is used throughout the Fable ecosystem for template processing:

- [fable](https://github.com/stevenvelozo/fable) - Application services framework
- [pict](https://github.com/stevenvelozo/pict) - UI framework
- [pict-template](https://github.com/stevenvelozo/pict-template) - Template engine built on Precedent

## Testing

Run the test suite:

```bash
npm test
```

Run with coverage:

```bash
npm run coverage
```

## Related Packages

- [fable](https://github.com/stevenvelozo/fable) - Application services framework
- [pict-template](https://github.com/stevenvelozo/pict-template) - Template engine

## License

MIT

## Contributing

Pull requests are welcome. For details on our code of conduct, contribution process, and testing requirements, see the [Retold Contributing Guide](https://github.com/stevenvelozo/retold/blob/main/docs/contributing.md).

---
_Source: https://npm.io/package/precedent · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
