Skip to content

martinerko/string-pattern-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

string-pattern-object

Do you like a new ES6 feature called Template literals?

const person = {
  'firstname': 'John',
  'lastname': 'Lennon'
};
console.log(`My name is ${person.firstname} ${person.lastname}!`);

which will print out something like:

  'My name is John Lennon!'

What if we could do it the other way and parse the name from a string and return the object?

const stringPattern2obj = require('.');
const person = stringPattern2obj('My name is John Lennon!', 'My name is ${firstname} ${lastname}!');
console.log(person);

which will print out something like:

{
  'firstname': 'John',
  'lastname': 'Lennon'
}

Simple and easy.

Please note, the name of your properties should contain only [a-zA-Z] characters.

About

🎼 simple lib that allows you to convert string into object based on provided string pattern

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published