tag2content
Advanced tools
Comparing version 1.0.2 to 1.0.3
export interface IOptions { | ||
baseContent: string; | ||
delimiters?: IDelimiters; | ||
tags: ITags; | ||
text: string; | ||
} | ||
@@ -6,0 +6,0 @@ export interface IDelimiters { |
@@ -9,10 +9,10 @@ "use strict"; | ||
exports.default = (function (options) { | ||
var baseContent = options.baseContent, _a = options.delimiters, delimiters = _a === void 0 ? defaultDelimiters : _a, tags = options.tags; | ||
return Object.keys(tags).reduce(function (baseContent, tagKey) { | ||
var _a = options.delimiters, delimiters = _a === void 0 ? defaultDelimiters : _a, tags = options.tags, text = options.text; | ||
return Object.keys(tags).reduce(function (text, tagKey) { | ||
var tag = tags[tagKey]; | ||
var regExp = new RegExp("\\" + delimiters.start + tagKey + "(?:[^\\]]*)?\\" + delimiters.end, 'g'); | ||
return baseContent.replace(regExp, function (match) { | ||
return text.replace(regExp, function (match) { | ||
return tag(utils_1.getVariables(match)).toString(); | ||
}); | ||
}, baseContent); | ||
}, text); | ||
}); |
@@ -7,11 +7,11 @@ "use strict"; | ||
var options = { | ||
baseContent: 'My name is [name]', | ||
tags: { | ||
name: function () { return 'John'; }, | ||
}, | ||
text: 'My name is [name]', | ||
}; | ||
// When | ||
var updatedContent = index_1.default(options); | ||
var updatedText = index_1.default(options); | ||
// Then | ||
expect(updatedContent).toBe('My name is John'); | ||
expect(updatedText).toBe('My name is John'); | ||
}); | ||
@@ -21,11 +21,11 @@ test('It should replace all "country-name" tags by "Italy"', function () { | ||
var options = { | ||
baseContent: 'I love [country-name]. [country-name] is the best country in the world.', | ||
tags: { | ||
'country-name': function () { return 'Italy'; }, | ||
}, | ||
text: 'I love [country-name]. [country-name] is the best country in the world.', | ||
}; | ||
// When | ||
var updatedContent = index_1.default(options); | ||
var updatedText = index_1.default(options); | ||
// Then | ||
expect(updatedContent).toBe('I love Italy. Italy is the best country in the world.'); | ||
expect(updatedText).toBe('I love Italy. Italy is the best country in the world.'); | ||
}); | ||
@@ -35,3 +35,2 @@ test('It should replace all "city" and "year" tags', function () { | ||
var options = { | ||
baseContent: 'I arrived in [city] in [year]. I am so happy to live in [city]!', | ||
tags: { | ||
@@ -41,7 +40,8 @@ city: function () { return 'Paris'; }, | ||
}, | ||
text: 'I arrived in [city] in [year]. I am so happy to live in [city]!', | ||
}; | ||
// When | ||
var updatedContent = index_1.default(options); | ||
var updatedText = index_1.default(options); | ||
// Then | ||
expect(updatedContent).toBe('I arrived in Paris in 2003. I am so happy to live in Paris!'); | ||
expect(updatedText).toBe('I arrived in Paris in 2003. I am so happy to live in Paris!'); | ||
}); | ||
@@ -51,3 +51,2 @@ test('It should replace "link" tag with variables', function () { | ||
var options = { | ||
baseContent: 'You should visit my website. [link href="http://www.google.com"]', | ||
tags: { | ||
@@ -59,12 +58,12 @@ link: function (_a) { | ||
}, | ||
text: 'You should visit my website. [link href="http://www.google.com"]', | ||
}; | ||
// When | ||
var updatedContent = index_1.default(options); | ||
var updatedText = index_1.default(options); | ||
// Then | ||
expect(updatedContent).toBe('You should visit my website. <a href="http://www.google.com" title="Link to my website">My website</a>'); | ||
expect(updatedText).toBe('You should visit my website. <a href="http://www.google.com" title="Link to my website">My website</a>'); | ||
}); | ||
test('It should replace "link" tag with variables', function () { | ||
test('It should replace "user" tag with variables and return a login with admin symbols', function () { | ||
// Given | ||
var options = { | ||
baseContent: 'Your login is [user name="Joe" is-admin="true"]', | ||
tags: { | ||
@@ -76,7 +75,28 @@ user: function (_a) { | ||
}, | ||
text: 'Your login is [user name="Joe" is-admin="true"]', | ||
}; | ||
// When | ||
var updatedContent = index_1.default(options); | ||
var updatedText = index_1.default(options); | ||
// Then | ||
expect(updatedContent).toBe('Your login is *Joe*'); | ||
expect(updatedText).toBe('Your login is *Joe*'); | ||
}); | ||
test('It should replace "first-name" tag using custom delimiters', function () { | ||
// Given | ||
var options = { | ||
delimiters: { | ||
end: '}', | ||
start: '{', | ||
}, | ||
tags: { | ||
'first-name': function (_a) { | ||
var value = _a.value; | ||
return value; | ||
}, | ||
}, | ||
text: 'My first name is {first-name value="Robin"}.', | ||
}; | ||
// When | ||
var updatedText = index_1.default(options); | ||
// Then | ||
expect(updatedText).toBe('My first name is Robin.'); | ||
}); |
{ | ||
"name": "tag2content", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Replace a tag with content in a configurable way", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -54,3 +54,3 @@ # tag2content | ||
You can add some variables to your tags. | ||
You can add some variables to your tags: | ||
@@ -73,1 +73,24 @@ ```js | ||
``` | ||
## Custom delimiters | ||
You can use custom delimiters: | ||
```js | ||
const text = 'My first name is {first-name value="Robin"}.'; | ||
const tags = { | ||
'first-name': ({ value }) => value, | ||
}; | ||
const updatedText = tag2content({ | ||
delimiters: { | ||
end: '}', | ||
start: '{', | ||
}, | ||
tags, | ||
text, | ||
}); | ||
console.log(updatedText) //=> 'My first name is Robin.'; | ||
``` |
8917
181
95