PHPackages                             mooeypoo/funcystr - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mooeypoo/funcystr

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mooeypoo/funcystr
=================

A lightweight PHP library for processing strings with embedded functions.

v2.0.1(6mo ago)140[2 issues](https://github.com/mooeypoo/funcystr/issues)MITJavaScriptPHP &gt;=8.0CI passing

Since Nov 4Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/mooeypoo/funcystr)[ Packagist](https://packagist.org/packages/mooeypoo/funcystr)[ RSS](/packages/mooeypoo-funcystr/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (2)Used By (0)

FuncyStr Library
================

[](#funcystr-library)

[![Node.js CI](https://github.com/mooeypoo/FuncyStr/actions/workflows/test.yaml/badge.svg)](https://github.com/mooeypoo/FuncyStr/actions/workflows/test.yaml/badge.svg) [![License: MIT](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](https://opensource.org/licenses/MIT)

[!["Buy Me A Coffee"](https://camo.githubusercontent.com/9f44ce2dc3b3eecdd02598900866ffc518801df1932849703dae1e5ce5031070/68747470733a2f2f7777772e6275796d6561636f666665652e636f6d2f6173736574732f696d672f637573746f6d5f696d616765732f6f72616e67655f696d672e706e67)](https://buymeacoffee.com/mooeypoo)

FuncyStr is a lightweight JavaScript library designed to process strings with embedded functions. It allows dynamic string resolution based on provided parameters, supporting nested and multiple function calls within a single string.

Features
--------

[](#features)

- **Dynamic String Resolution**: Replace placeholders in strings with dynamic values based on provided parameters.
- **Custom Functions**: Define your own functions to handle specific placeholders.
- **Nested Functions**: Supports resolving functions within other functions.
- **Graceful Handling**: Leaves unresolved placeholders intact if no matching function is defined.

Monorepo layout
---------------

[](#monorepo-layout)

This repository contains both the JavaScript and PHP implementations of funcystr:

- JavaScript (npm)

    - Source: `src/`
    - Tests: `test/`
    - Build output: `dist/`
    - Published as the `funcystr` package on npm from the repository root.
- PHP (Composer / Packagist)

    - Source: `php/src/`
    - Tests: `php/tests/`
    - Configuration: `composer.json` at the repository root
    - Published as the `mooeypoo/funcystr` package on Packagist from this same repository.

Javascript Installation (npm)
-----------------------------

[](#javascript-installation-npm)

Install FuncyStr via npm:

```
npm install funcystr --save-dev
```

### Usage

[](#usage)

#### Basic Example

[](#basic-example)

```
import FuncyStr from 'funcystr';

const fstr = await new FuncyStr({
    PRONOUN: (params, he, she, they) => params.pronoun === 'he' ? he : params.pronoun === 'she' ? she : they,
    PLURAL: (params, one, plural) => (params.plural ? plural : one),
});

const result = await fstr.process("{{He is|She is|They are}} very welcome to join us.", { pronoun: 'he' });
console.log(result); // Output: "He is very welcome to join us."
```

#### Nested Functions

[](#nested-functions)

```
const input = "{{PLURAL|This is|These are}} lovely {{PRONOUN|{{PLURAL|man|men}}|{{PLURAL|woman|women}}|{{PLURAL|person|people}}}}.";

const result = await fstr.process(input, { pronoun: 'they', plural: true });
console.log(result); // Output: "These are lovely people."
```

#### Handling Missing Functions

[](#handling-missing-functions)

```
const result = await fstr.process("This is a {{UNKNOWN|arg1|arg2}}.", {});
console.log(result); // Output: "This is a {{UNKNOWN|arg1|arg2}}."
```

### API

[](#api)

#### `new FuncyStr(functions)`

[](#new-funcystrfunctions)

Creates a new FuncyStr instance.

- `functions`: An object where keys are function names and values are the corresponding resolver functions.

#### `process(input, params)`

[](#processinput-params)

Processes a string and resolves all placeholders.

- `input`: The string containing placeholders to resolve.
- `params`: An object containing parameters used by the resolver functions.

### Testing

[](#testing)

Run the test suite to ensure everything is working as expected:

```
npm run test
```

PHP usage (Composer / Packagist)
--------------------------------

[](#php-usage-composer--packagist)

Install from Packagist:

```
composer require mooeypoo/funcystr
```

Basic usage:

```
