1.0.3 • Published 5 months ago

@truto/replace-placeholders v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@truto/replace-placeholders

Replace placeholders in strings, arrays, and objects easily. This package internally utilizes the wild-wild-path library and supports all path formats provided by it.

Installation

npm install @truto/replace-placeholders

Usage

Basic

import replacePlaceholders from '@truto/replace-placeholders';

const result = replacePlaceholders('Foo: {{foo}}', { foo: 'bar' });
console.log(result);  // Outputs: 'Foo: bar'

Advanced Usage with Different Data Types

Strings

console.log(replacePlaceholders('{{foo}}', { foo: 'bar' }));  // Outputs: 'bar'
console.log(replacePlaceholders('{{foo-bar.something}}', { 'foo-bar': { something: true } }));  // Outputs: 'true'
console.log(replacePlaceholders('{{foo}} {{bar:bool}}', { foo: 'bar', bar: 'false' }));  // Outputs: 'bar false'

Arrays

console.log(replacePlaceholders(['Foo: {{foo}}'], { foo: 'bar' }));  // Outputs: ['Foo: bar']
console.log(replacePlaceholders(['{{foo}}', '{{bar:num}}'], { foo: 'bar', bar: '1.34' }));  // Outputs: ['bar', 1.34]

Objects

console.log(replacePlaceholders({ foo: '{{foo}}' }, { foo: 'bar' }));  // Outputs: { foo: 'bar' }
console.log(replacePlaceholders({ foo: '{{foo}}', bar: '{{bar:int}}' }, { foo: 'bar', bar: 1 }));  // Outputs: { foo: 'bar', bar: 1 }

Conversion Types

console.log(replacePlaceholders('{{foo:int}}', { foo: '1' }));  // Outputs: 1
console.log(replacePlaceholders('{{foo:num}}', { foo: '1.1' }));  // Outputs: 1.1
console.log(replacePlaceholders('{{foo:bool}}', { foo: 'true' }));  // Outputs: true
console.log(replacePlaceholders('{{foo:json}}', { foo: '{"foo":"bar"}' }));  // Outputs: { foo: 'bar' }
console.log(replacePlaceholders('{{foo:null}}', { foo: 'null' }));  // Outputs: null

Conditional Replacements (Fallback Values)

Using a | (pipe) character, you can provide fallback values right within the placeholder. The function will pick the first non-undefined value for the replacement.

console.log(replacePlaceholders('{{foo|bar}}', { foo: 'foo', bar: 'bar' }));  // Outputs: 'foo'
console.log(replacePlaceholders('{{foo:str|bar:int}}', { bar: 1 }));  // Outputs: 1
console.log(replacePlaceholders('{{foo.bar:str|bar:str}}', { foo: { bar: 'bar' } }));  // Outputs: 'bar'
console.log(replacePlaceholders('{{foo.bar|bar:str}}', { foo: { bar: true } }));  // Outputs: 'true'

License

This project is licensed under the MIT License.

MIT License

Copyright (c) 2023 Yin Yang Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Contributing

We welcome contributions from the community! If you wish to contribute, please follow these steps:

  1. Fork the repository: Click on the 'Fork' button at the top right of this page and clone your forked repository to your local machine.

  2. Create a new branch: Create a new branch named after the feature or fix you are working on. For example: feature/new-placeholder-syntax or fix/issue-123.

  3. Make your changes: Make the necessary modifications to the code. Ensure that you adhere to the existing coding standards and conventions.

  4. Commit your changes: Commit your changes with a clear and concise commit message that describes the changes you made.

  5. Push to your fork: Push your changes to your forked repository on GitHub.

  6. Submit a pull request: Create a new pull request from your forked repository to the main repository. Please ensure that your pull request describes the changes you made, references any related issues, and has been tested on the latest version of the package.

Please note: By contributing to this project, you agree to abide by the code of conduct and that your contributions will be licensed under the MIT license (as per the LICENSE section).

1.0.2

5 months ago

1.0.3

5 months ago

1.0.1

9 months ago

1.0.0

9 months ago