-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (66 loc) · 2.52 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function inputAdjustDefault( str ){
return str.trim();
}
function outputAdjustDefault( str ){
return str;
}
const same = x => x;
function splitMe({ content = '', openTag, closeTag, makeErr, handler_outside = same, handler_inside = same } = {} ){
let result = [];
( content + '' ).split( openTag ).forEach( function( one, index ){
if( !index ){
result.push( handler_outside( one, index, one ) );
return; // ignore first one
}
let rightList = one.split( closeTag );
if( 2 != rightList.length ) throw makeErr( one, index );
result.push( handler_inside( rightList.shift(), index, one ) );
result.push( handler_outside( rightList.shift(), index, one ) );
} );
return result.filter( x => x );
}
function handleTargetRange( content, { openTag = '{%', closeTag = '%}', inputAdjust = inputAdjustDefault, outputAdjust = outputAdjustDefault } = {}, range_index = 0 ){
let map = [],
makeErr = ( one, index ) => {
index = index - 1;
let err = new Error( `closeTag did not match openTag at index ${ range_index } - ${ index } : ${ one }` );
err.range_index = range_index;
err.index = index;
err.content = one;
throw err;
},
handler_inside = ( content, index, one ) => {
index = index - 1;
let from = inputAdjust( content ),
to = outputAdjust( from );
map.push( { from, to } );
return to;
};
let result = splitMe({ content, openTag, closeTag, makeErr, handler_inside });
return { result, map };
}
export default function( content, options = {} ){
let map = [],
{ ignoreStart : openTag, ignoreEnd : closeTag } = options,
makeErr = ( one, index ) => {
let err = new Error( `closeTag did not match openTag at index ${ index } : ${ one }` );
err.index = index;
err.content = one;
throw err;
},
handler_outside = ( content, index, one ) => {
let { result : _result, map : _map } = handleTargetRange( content, options, index );
map.push( _map );
return _result.join( '' );
}
let result;
if( openTag && closeTag ){
result = splitMe({ content, openTag, closeTag, makeErr, handler_outside, })
} else {
result = [ handler_outside( content, 0, content ) ];
}
return {
result,
map : 1 == map.length ? map[0] : map,
};
}