Passed
Pull Request — master (#19)
by Muhammad Dyas
01:34
created

src/helpers/state.ts   A

Complexity

Total Complexity 41
Complexity/F 4.56

Size

Lines of Code 81
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 41
eloc 58
mnd 32
bc 32
fnc 9
dl 0
loc 81
rs 9.1199
bpm 3.5555
cpm 4.5555
noi 0
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A state.ts ➔ addOptionToState 0 16 2
A state.ts ➔ getChoicesFromInput 0 10 4
A state.ts ➔ getStateFromCard 0 8 5
C state.ts ➔ getStateFromCardWhenHasHeader 0 4 9
B state.ts ➔ getConfigFromInput 0 11 6
A state.ts ➔ getStateFromCardName 0 4 1
A state.ts ➔ getStateFromParameter 0 5 3
A state.ts ➔ getStringInputValue 0 6 2
C state.ts ➔ getStateFromCardWhenNoHeader 0 3 9

How to fix   Complexity   

Complexity

Complex classes like src/helpers/state.ts often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import {ClosableType, PollForm, PollFormInputs, PollState} from './interfaces';
2
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
3
import {MAX_NUM_OF_OPTIONS} from '../config/default';
4
5
/**
6
 * Add a new option to the state(like DB)
7
 *
8
 * @param {string} option - the new option name
9
 * @param {object} state - the current message state
10
 * @param {string} creator - who add the new option
11
 * @returns {void} card
12
 */
13
export function addOptionToState(option: string, state: PollState, creator = '') {
14
  const choiceLength = state.choices.length;
15
  state.choices.push(option);
16
  if (state.choiceCreator === undefined) {
17
    state.choiceCreator = {[choiceLength]: creator};
18
  } else {
19
    state.choiceCreator[choiceLength] = creator;
20
  }
21
}
22
23
export function getStateFromCard(event: chatV1.Schema$DeprecatedEvent) {
24
  const card = event.message?.cardsV2?.[0]?.card as chatV1.Schema$GoogleAppsCardV1Card;
25
  if (!card) {
26
    throw new ReferenceError('no valid card in the event');
27
  }
28
  return getStateFromCardName(card) || getStateFromParameter(event) || getStateFromCardWhenNoHeader(card) ||
29
    getStateFromCardWhenHasHeader(card);
30
}
31
32
function getChoicesFromInput(formValues: PollFormInputs) {
33
  const choices = [];
34
  for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
35
    const choice = formValues[`option${i}`]?.stringInputs!.value![0]!.trim();
36
    if (choice) {
37
      choices.push(choice);
38
    }
39
  }
40
  return choices;
41
}
42
43
function getStringInputValue(input: chatV1.Schema$Inputs) {
44
  if (!input) {
45
    return '';
46
  }
47
  return input.stringInputs!.value![0];
48
}
49
50
export function getConfigFromInput(formValues: PollFormInputs) {
51
  const state: PollForm = {topic: '', choices: []};
52
  state.topic = getStringInputValue(formValues.topic).trim() ?? '';
53
  state.anon = getStringInputValue(formValues.is_anonymous) === '1';
54
  state.optionable = getStringInputValue(formValues.allow_add_option) === '1';
55
  state.type = parseInt(getStringInputValue(formValues.type) || '1') as ClosableType;
56
  state.autoclose = getStringInputValue(formValues.is_autoclose) === '1';
57
  state.closedTime = parseInt(formValues.close_schedule_time?.dateTimeInput!.msSinceEpoch ?? '0');
58
  state.choices = getChoicesFromInput(formValues);
59
  return state;
60
}
61
62
function getStateFromCardWhenNoHeader(card: chatV1.Schema$GoogleAppsCardV1Card) {
63
  return card.sections?.[0].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value;
64
}
65
66
function getStateFromCardWhenHasHeader(card: chatV1.Schema$GoogleAppsCardV1Card) {
67
  // when has header the first section is header
68
  return card.sections?.[1].widgets?.[0].decoratedText?.button?.onClick?.action?.parameters?.[0]?.value;
69
}
70
71
export function getStateFromCardName(card: chatV1.Schema$GoogleAppsCardV1Card) {
72
  // when has header the first section is header
73
  return card.name;
74
}
75
76
function getStateFromParameter(event: chatV1.Schema$DeprecatedEvent) {
77
  const parameters = event.common?.parameters;
78
79
  return parameters?.['state'];
80
}
81