Skip to content

Commit 689a7bb

Browse files
committed
change: remove lodash from utils.js + split speechSynthesis
1 parent 9f3db03 commit 689a7bb

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

lib/ChatBot.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {
1717
} from './components';
1818
import Recognition from './recognition';
1919
import { ChatIcon, CloseIcon, SubmitIcon, MicIcon } from './icons';
20-
import { isMobile, speakFn } from './utils';
20+
import { isMobile } from './utils';
21+
import { speakFn } from './speechSynthesis';
2122

2223
class ChatBot extends Component {
2324
/* istanbul ignore next */

lib/speechSynthesis.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { isString } from './utils';
2+
3+
const getSpeakText = (step) => {
4+
const { message, metadata = {} } = step;
5+
if (isString(metadata.speak)) {
6+
return metadata.speak;
7+
}
8+
if (isString(message)) {
9+
return message;
10+
}
11+
return '';
12+
};
13+
14+
export const speakFn = speechSynthesisOptions => (step, previousValue) => {
15+
const { lang, voice, enable } = speechSynthesisOptions;
16+
const { user } = step;
17+
18+
if (!window.SpeechSynthesisUtterance || !window.speechSynthesis) {
19+
return;
20+
}
21+
if (user) {
22+
return;
23+
}
24+
if (!enable) {
25+
return;
26+
}
27+
const text = getSpeakText(step);
28+
const msg = new window.SpeechSynthesisUtterance();
29+
msg.text = text.replace(/{previousValue}/g, previousValue);
30+
msg.lang = lang;
31+
msg.voice = voice;
32+
window.speechSynthesis.speak(msg);
33+
};

lib/utils.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
import _ from 'lodash';
2-
31
export const isMobile = () => /iphone|ipod|android|ie|blackberry|fennec/i.test(navigator.userAgent);
42

5-
export const speakFn = speechSynthesisOptions => (step, previousValue) => {
6-
const { lang, voice, enable } = speechSynthesisOptions;
7-
const { user, message, metadata = {} } = step;
8-
9-
if (user) {
10-
return;
11-
}
12-
if (!enable) {
13-
return;
14-
}
15-
16-
const text = _.cond([
17-
[() => _.isString(metadata.speak), _.constant(metadata.speak)],
18-
[() => _.isString(message), _.constant(message)],
19-
[_.stubTrue, _.constant('')],
20-
])(step);
21-
22-
const msg = new SpeechSynthesisUtterance();
23-
msg.text = text.replace(/{previousValue}/g, previousValue);
24-
msg.lang = lang;
25-
msg.voice = voice;
26-
window.speechSynthesis.speak(msg);
27-
};
3+
export const isString = value => typeof value === 'string';

0 commit comments

Comments
 (0)