Skip to content

Commit 21775be

Browse files
committed
Move template literal logic into its extractor.
No need to separate logic here. It makes the code less readable.
1 parent 6d74a25 commit 21775be

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

src/util/buildTemplateLiteral.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
1-
import buildTemplateLiteral from '../../buildTemplateLiteral';
1+
/**
2+
* Returns the string value of a template literal object.
3+
* Tries to build it as best as it can based on the passed
4+
* prop. For instance `This is a ${prop}` will return 'This is a {prop}'.
5+
*
6+
* If the template literal builds to undefined (`${undefined}`), then
7+
* this should return "".
8+
*/
9+
const extract = value => {
10+
const {
11+
quasis,
12+
expressions
13+
} = value;
14+
const partitions = quasis.concat(expressions);
215

3-
const extract = value => buildTemplateLiteral(value);
16+
return partitions.sort((a, b) => a.start - b.start).reduce((raw, part) => {
17+
const {
18+
type
19+
} = part;
20+
if (type === 'TemplateElement') {
21+
return raw + part.value.raw;
22+
} else if (type === 'Identifier') {
23+
return part.name === 'undefined' ? raw : `${raw}{${part.name}}`;
24+
}
25+
26+
return raw;
27+
}, '');
28+
};
429

530
export default extract;

0 commit comments

Comments
 (0)