sindresorhus / pupa

Simple micro templating
MIT License
362 stars 22 forks source link

The model does not match property keys with the dynamic name. #15

Closed brunos3d closed 4 years ago

brunos3d commented 4 years ago

Hi. 😊 I'm using pupa in a discord bot in a multi-language system. 🤖

The files containing the translations are made up of objects with properties whose keys contain names with special characters such as spaces and -.

However, the formatter does not find these properties. I added a simple code below simulating the environment. 😅

var pupa = require("pupa")

const args = {
   "commonName": {
      test: 1
   },
   "dynamic-key-name": {
      test: 2
   },
   "dynamic key name": {
      test: 2
   }
}

console.log(pupa("testing {commonName.test}", args))         //=> "testing 1"
console.log(pupa("testing {dynamic-key-name.test}", args))   //=> "testing {dynamic-key-name.test}"
console.log(pupa("testing {dynamic key name.test}", args))   //=> "testing {dynamic key name.test}"
mukaschultze commented 4 years ago

Maybe the package should interpret keys enclosed by quotes as literal strings? For example pupa("testing {'dynamic key name'.test}", args) should work, as strings are also valid JS keys.

brunos3d commented 4 years ago

I also thought that this could work. But it did not work.

brunos3d commented 4 years ago

These tests were done using RunKit

image

brunos3d commented 4 years ago

I believe that another good solution would be to implement the object-path package to solve this problem.

These tests were done using RunKit

image