🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

string-object-formatter

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-object-formatter

Python inspired named template formatter for Javascript strings

4.0.1
latest
Source
npm
Version published
Weekly downloads
386
-11.87%
Maintainers
1
Weekly downloads
 
Created
Source

string-object-formatter

Inspired by python named formatter function, replace text inside a string based on object properties names and values.

Example usage

Import it in your code

In a CommonJS environment

const Formatter = require("string-object-formatter");

Using import

import Formatter from "string-object-formatter";

Default delimiters

const formatter = new Formatter();
const toFormat = "My name is {firstName} {lastName}";
const formatted = formatter.format(toFormat, {
  firstName: "John",
  lastName: "Doe",
});

// formatted is 'My name is John Doe'

Custom delimiters

const formatter = new Formatter("{{", "}}");
const toFormat = "My name is {{firstName}} {{lastName}}";
const formatted = formatter.format(toFormat, {
  firstName: "John",
  lastName: "Doe",
});

// formatted is 'My name is John Doe'

Table of contents

Constructors

  • constructor

Properties

Methods

Constructors

constructor

+ new default(startDelimiter?: string, endDelimiter?: string, silent: boolean): default

Creates an instance of Formatter.

memberof Formatter

Parameters:

NameTypeDefault value
startDelimiterstring'{'
endDelimiterstring'}'

Returns: default

Properties

endDelimiter

endDelimiter: string

startDelimiter

startDelimiter: string

Methods

format

format(stringToFormat: string, formatItems: Record<string, string | number>): string

Formats string according to object

memberof Formatter

Parameters:

| Name | Type | Description | | :--------------- | :---------------------- | :------------------- | --------------------------------------------------------------------------------- | | stringToFormat | string | The string to format | | formatItems | _Record<string, string | number>_ | Ex.: {'toReplace': 'replaced'} turns 'example_{toReplace}' to 'example_replaced' |

Returns: string

The replaced string

Keywords

strings

FAQs

Package last updated on 25 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts