object-to-text
TypeScript icon, indicating that this package has built-in type declarations

1.8.1 • Public • Published

object-to-text

Recursive nested text replacer that converts any data objects into strings using templates.

Examples

very simple example

const { toText } = requrie('object-to-text')

let data = { someProperty: "World" }
let text = toText(data, "Hello {someProperty}")
// text === "Hello World"

simple example

const { toText } = requrie('object-to-text')

let data = { someProperty: "World", cities: ["London", "New York", "Paris", "Moscow", "Tokyo"] }
let text = toText(data, "Hello {someProperty}!\nHere's a list of some must-see cities: {cities:\n - {$}}")

/* text === "Hello World!
Here's a list of some must-see citites:
 - London
 - New York
 - Paris
 - Moscow
 - Tokyo"
*/

a bit better variant of that example:

const { toText } = requrie('object-to-text')

let data = {

    someProperty: "World",
    cities: [
        {name: "London",    code: "LND" },
        {name: "New York",  code: "NYC" },
        {name: "Paris",     code: "PRS" },
        {name: "Moscow",    code: "MSC" },
        {name: "Tokyo",     code: "TKY" }
    ]
}

let text = toText(data, "Hello {someProperty}!\nHere's a list of some must-see cities: {cities:\n - {name}}")

/* text === "Hello World!
Here's a list of some must-see citites:
 - London
 - New York
 - Paris
 - Moscow
 - Tokyo"
*/

Package Sidebar

Install

npm i object-to-text

Weekly Downloads

2

Version

1.8.1

License

MIT

Unpacked Size

34.4 kB

Total Files

32

Last publish

Collaborators

  • domsky2