Giter Site home page Giter Site logo

object-replace-mustache's Introduction

object-replace-mustache

npm GitHub Workflow Status Code Climate maintainability Code Climate coverage libraries.io npm GitHub license

This project uses the concepts of mustache for objects. Properties with {{ stringsWithCurlyBrackets }} placeholders get replaced by their corresponding view object properties. It's pretty useful for storing 'dynamic' objects in databases.

Installation

npm i object-replace-mustache

Usage

import { replace } from 'object-replace-mustache'

const original = { isTest: '{{ nested.test }}' };

const view = { nested: { test: true } }

const transformed = replace(original, view);
console.log(transformed)
// { isTest: true }

render

There is also a render function that is like mustache.js or handlebars

import { render } from 'object-replace-mustache'

const rendered = render('hello { test }!', { test: 'world' })
console.log(rendered);
// hello world!

Testing

Simply run npm test and all your tests in the test/ directory will be run. It has full support for Visual Studio Code. You can use the debugger to set breakpoints.

License

Licensed under the MIT license.

object-replace-mustache's People

Contributors

fratzinger avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

miguelrk

object-replace-mustache's Issues

Support for multiple (inline) properties

Hey @fratzinger! Thanks for this very useful library.

Issue: I came across the need of using multiple (inline) properties e.g. { url: "https://example.com/{{version}}/{{resource}}" }, which I believe is a relatively common use case. However, this package currently only supports a single property (starting/ending with {{ and }} respectively e.g. "{{prop1}}".

My current workaround is simply wrapping the entire property in curly braces e.g. { url: "{{url}}" }.

I was wondering if this is a feature that would be viable for this package, since it might complicate the parsing logic (e.g. for array/object values). However, this can be handled simply by stringifying the value (despite it being array/object) and inserting it in the string. Some examples:

const original = { url: "https://example.com/v{{versionNumber}}/{{resource}}" }

// 1) primitive types (string, number, boolean, undefined, ...)
const viewPrimitives = { versionNumber: 1, resource: "users" }
replace(original, viewPrimitives) // returns { url: "https://example.com/v1/users" }

// 2) array/object types
const viewNonPrimitives = { versionNumber: { foo: 'bar' }, resource: ['users'] }
replace(original, viewNonPrimitives) // returns { url: "https://example.com/v{ foo: 'bar' }/['users']" }

NOTE: use case (2), handling array/object types in this way (inline) is certainly uncommon, however stringifying will ensure the rendering works also for these cases. This might be desired.

Is something like this worth adding to the library? If so, I think I could try to have a PR.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.