UNPKG

1.19 kBJavaScriptView Raw
1/* eslint-disable consistent-return */
2/**
3 * Strip start of function
4 *
5 * @param {string} input
6 *
7 * @return {string}
8 */
9function stripFunctionStart(input) {
10 const BEGIN = 1;
11 const LBRACE = 2;
12 const EQ = 4;
13 const ARROW = 8;
14 const ARROW_LBRACE = 16;
15 const ARROW_PAREN = 32;
16 const DONE = 64;
17
18 const isWhitespace = ch => ch === ' ' || ch === '\t' || ch === '\n';
19
20 const nextState = (state, c) => {
21 switch (state) {
22 case BEGIN:
23 switch (c) {
24 case '{': return LBRACE;
25 case '=': return EQ;
26 default: return BEGIN;
27 }
28
29 case LBRACE:
30 return c === ' ' ? LBRACE : DONE;
31
32 case EQ:
33 return c === '>' ? ARROW : BEGIN;
34
35 case ARROW:
36 if (isWhitespace(c)) return ARROW;
37 switch (c) {
38 case '{': return ARROW_LBRACE;
39 case '(': return ARROW_PAREN;
40 default: return DONE;
41 }
42
43 case ARROW_LBRACE:
44 case ARROW_PAREN:
45 return DONE;
46 }
47 };
48
49 let state = BEGIN;
50 let pos = 0;
51 while (pos < input.length && state !== DONE) {
52 state = nextState(state, input.charAt(pos));
53 pos += 1;
54 }
55 return state === DONE ? input.slice(pos - 1) : input;
56}
57
58module.exports = stripFunctionStart;