mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
772 B
26 lines
772 B
8 years ago
|
const start = /\n(\t+)/;
|
||
|
|
||
|
export default function deindent ( strings, ...values ) {
|
||
|
const indentation = start.exec( strings[0] )[1];
|
||
|
const pattern = new RegExp( `^${indentation}`, 'gm' );
|
||
|
|
||
|
let result = strings[0].replace( start, '' ).replace( pattern, '' );
|
||
|
|
||
|
let trailingIndentation = getTrailingIndentation( result );
|
||
|
|
||
|
for ( let i = 1; i < strings.length; i += 1 ) {
|
||
|
const value = String( values[ i - 1 ] ).replace( /\n/g, `\n${trailingIndentation}` );
|
||
|
result += value + strings[i].replace( pattern, '' );
|
||
|
|
||
|
trailingIndentation = getTrailingIndentation( result );
|
||
|
}
|
||
|
|
||
|
return result.trim();
|
||
|
}
|
||
|
|
||
|
function getTrailingIndentation ( str ) {
|
||
|
let i = str.length;
|
||
|
while ( str[ i - 1 ] === ' ' || str[ i - 1 ] === '\t' ) i -= 1;
|
||
|
return str.slice( i, str.length );
|
||
|
}
|