UNPKG

1.31 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 '{':
25 return LBRACE;
26 case '=':
27 return EQ;
28 default:
29 return BEGIN;
30 }
31
32 case LBRACE:
33 return c === ' ' ? LBRACE : DONE;
34
35 case EQ:
36 return c === '>' ? ARROW : BEGIN;
37
38 case ARROW:
39 if (isWhitespace(c)) return ARROW;
40 switch (c) {
41 case '{':
42 return ARROW_LBRACE;
43 case '(':
44 return ARROW_PAREN;
45 default:
46 return DONE;
47 }
48
49 case ARROW_LBRACE:
50 case ARROW_PAREN:
51 return DONE;
52 }
53 };
54
55 let state = BEGIN;
56 let pos = 0;
57 while (pos < input.length && state !== DONE) {
58 state = nextState(state, input.charAt(pos));
59 pos += 1;
60 }
61 return state === DONE ? input.slice(pos - 1) : input;
62}
63
64module.exports = stripFunctionStart;