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:
1.2 KB
|
Rev | Line | |
---|
[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = extractValueFromTemplateLiteral;
|
---|
| 7 | function sortStarts(a, b) {
|
---|
| 8 | return (a.range ? a.range[0] : a.start) - (b.range ? b.range[0] : b.start);
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Returns the string value of a template literal object.
|
---|
| 13 | * Tries to build it as best as it can based on the passed
|
---|
| 14 | * prop. For instance `This is a ${prop}` will return 'This is a {prop}'.
|
---|
| 15 | *
|
---|
| 16 | * If the template literal builds to undefined (`${undefined}`), then
|
---|
| 17 | * this should return "undefined".
|
---|
| 18 | */
|
---|
| 19 | function extractValueFromTemplateLiteral(value) {
|
---|
| 20 | var quasis = value.quasis,
|
---|
| 21 | expressions = value.expressions;
|
---|
| 22 |
|
---|
| 23 | var partitions = quasis.concat(expressions);
|
---|
| 24 |
|
---|
| 25 | return partitions.sort(sortStarts).map(function (_ref) {
|
---|
| 26 | var type = _ref.type,
|
---|
| 27 | _ref$value = _ref.value;
|
---|
| 28 | _ref$value = _ref$value === undefined ? {} : _ref$value;
|
---|
| 29 | var raw = _ref$value.raw,
|
---|
| 30 | name = _ref.name;
|
---|
| 31 |
|
---|
| 32 | if (type === 'TemplateElement') {
|
---|
| 33 | return raw;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | if (type === 'Identifier') {
|
---|
| 37 | return name === 'undefined' ? name : '{' + name + '}';
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | if (type.indexOf('Expression') > -1) {
|
---|
| 41 | return '{' + type + '}';
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | return '';
|
---|
| 45 | }).join('');
|
---|
| 46 | } |
---|
Note:
See
TracBrowser
for help on using the repository browser.