Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3886921

Browse files
committed
fix stateToString function
1 parentc32d670 commit3886921

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

‎src/state/index.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,11 @@ import {interpret, Interpreter} from 'xstate'
22
import{TutorialModel}from'../services/tutorial'
33
import*asCRfrom'typings'
44
importcreateMachinefrom'./machine'
5+
importstateToStringfrom'./utils/stateToString'
56

67
// machine interpreter
78
// https://xstate.js.org/docs/guides/interpretation.html
89

9-
// convert state into a readable string
10-
conststateToString=(state:string|object,str:string=''):string=>{
11-
if(typeofstate==='object'){
12-
constkeys=Object.keys(state)
13-
if(keys&&keys.length){
14-
constkey=keys[0]
15-
returnstateToString(state[key],str.length ?`${str}.${key}` :key)
16-
}
17-
returnstr
18-
}elseif(typeofstate==='string'){
19-
returnstate
20-
}
21-
return''
22-
}
23-
24-
2510
classStateMachine{
2611
privatemachineOptions={
2712
devTools:true,
@@ -35,9 +20,10 @@ class StateMachine {
3520

3621
// format state as a string and send it to the client
3722
this.syncState=(state:any):void=>{
23+
console.log(state)
3824
conststateValue:CR.MessageState=stateToString(state.value)
3925
console.log(`STATE:${stateValue}`)
40-
editorDispatch('send_data',stateValue)
26+
editorDispatch('coderoad.send_state',stateValue)
4127
}
4228

4329
// callback on all state changes

‎src/state/utils/stateToString.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Note: This example test is leveraging the Mocha test framework.
3+
// Please refer to their documentation on https://mochajs.org/ for help.
4+
//
5+
6+
// The module 'assert' provides assertion methods from node
7+
importstateToStringfrom'./stateToString'
8+
9+
// You can import and use all API from the 'vscode' module
10+
// as well as import your extension to test it
11+
// import * as vscode from 'vscode'
12+
// import * as myExtension from '../extension'
13+
14+
// Defines a Mocha test suite to group tests of similar kind together
15+
describe('StateToString',()=>{
16+
17+
test('no levels deep',()=>{
18+
expect(stateToString('first')).toBe('first')
19+
})
20+
// Defines a Mocha unit test
21+
test('one level deep',()=>{
22+
expect(stateToString({first:'second'})).toBe('first.second')
23+
})
24+
25+
test('two levels deep',()=>{
26+
expect(stateToString({first:{second:'third'}})).toBe('first.second.third')
27+
})
28+
})

‎src/state/utils/stateToString.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
conststateToString=(state:string|object,str:string=''):string=>{
2+
if(typeofstate==='object'){
3+
constkeys=Object.keys(state)
4+
if(keys&&keys.length){
5+
constkey=keys[0]
6+
returnstateToString(state[key],str.length ?`${str}.${key}` :key)
7+
}
8+
returnstr
9+
}elseif(typeofstate==='string'){
10+
returnstr.length ?`${str}.${state}` :state
11+
}
12+
return''
13+
}
14+
15+
exportdefaultstateToString

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp