main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
934 bytes
|
Rev | Line | |
---|
[d565449] | 1 | function sortStarts(a, b) {
|
---|
| 2 | return (a.range ? a.range[0] : a.start) - (b.range ? b.range[0] : b.start);
|
---|
| 3 | }
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * Returns the string value of a template literal object.
|
---|
| 7 | * Tries to build it as best as it can based on the passed
|
---|
| 8 | * prop. For instance `This is a ${prop}` will return 'This is a {prop}'.
|
---|
| 9 | *
|
---|
| 10 | * If the template literal builds to undefined (`${undefined}`), then
|
---|
| 11 | * this should return "undefined".
|
---|
| 12 | */
|
---|
| 13 | export default function extractValueFromTemplateLiteral(value) {
|
---|
| 14 | const {
|
---|
| 15 | quasis,
|
---|
| 16 | expressions,
|
---|
| 17 | } = value;
|
---|
| 18 | const partitions = quasis.concat(expressions);
|
---|
| 19 |
|
---|
| 20 | return partitions.sort(sortStarts).map(({ type, value: { raw } = {}, name }) => {
|
---|
| 21 | if (type === 'TemplateElement') {
|
---|
| 22 | return raw;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | if (type === 'Identifier') {
|
---|
| 26 | return name === 'undefined' ? name : `{${name}}`;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | if (type.indexOf('Expression') > -1) {
|
---|
| 30 | return `{${type}}`;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | return '';
|
---|
| 34 | }).join('');
|
---|
| 35 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.