Skip to content

Commit a952152

Browse files
committed
feat: add SkipDataAndType class
1 parent 4b765b8 commit a952152

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import FileService from './file'
2+
3+
export default class SkipDataAndType {
4+
private static moduleName = ''
5+
private static fileName = 'skip-data-and-type.json'
6+
7+
private open: boolean | undefined = undefined
8+
9+
private static instance: SkipDataAndType
10+
11+
static getInstance(): SkipDataAndType {
12+
if (!SkipDataAndType.instance) {
13+
SkipDataAndType.instance = new SkipDataAndType()
14+
}
15+
16+
return SkipDataAndType.instance
17+
}
18+
19+
// open means can use cells with data and type
20+
public update(open: boolean) {
21+
FileService.getInstance().writeFileSync(
22+
SkipDataAndType.moduleName,
23+
SkipDataAndType.fileName,
24+
JSON.stringify({
25+
open,
26+
})
27+
)
28+
// cache this variable
29+
this.open = open
30+
}
31+
32+
public get(): boolean {
33+
// if cached, don't to read file
34+
if (this.open !== undefined) {
35+
return this.open
36+
}
37+
const fileService = FileService.getInstance()
38+
const { moduleName, fileName } = SkipDataAndType
39+
40+
if (fileService.hasFile(moduleName, fileName)) {
41+
const info = FileService.getInstance().readFileSync(moduleName, fileName)
42+
const { open } = JSON.parse(info)
43+
if (open === false) {
44+
return false
45+
}
46+
}
47+
48+
// default is true
49+
return true
50+
}
51+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import SkipDataAndType from '../../src/services/skip-data-and-type'
2+
import FileService from '../../src/services/file'
3+
4+
describe(`SkipDataAndType`, () => {
5+
let skipDataAndType: SkipDataAndType | undefined
6+
7+
beforeEach(() => {
8+
const fileService = FileService.getInstance()
9+
// @ts-ignore: Private method
10+
const { moduleName, fileName } = SkipDataAndType
11+
if (fileService.hasFile(moduleName, fileName)) {
12+
fileService.deleteFileSync(moduleName, fileName)
13+
}
14+
15+
skipDataAndType = new SkipDataAndType()
16+
})
17+
18+
it('getInstance', () => {
19+
const skip = SkipDataAndType.getInstance()
20+
expect(skip).toBeInstanceOf(SkipDataAndType)
21+
})
22+
23+
it('update', () => {
24+
expect(() => {
25+
skipDataAndType!.update(true)
26+
}).not.toThrowError()
27+
})
28+
29+
describe('with instance cache', () => {
30+
it('get true', () => {
31+
skipDataAndType!.update(true)
32+
const open = skipDataAndType!.get()
33+
expect(open).toBe(true)
34+
})
35+
36+
it('get false', () => {
37+
skipDataAndType!.update(false)
38+
const open = skipDataAndType!.get()
39+
expect(open).toBe(false)
40+
})
41+
})
42+
43+
describe('without cache', () => {
44+
it('first time open', () => {
45+
const open = skipDataAndType!.get()
46+
expect(open).toBe(true)
47+
})
48+
49+
it('new instance', () => {
50+
skipDataAndType!.update(false)
51+
const newInstance = new SkipDataAndType()
52+
const open = newInstance.get()
53+
expect(open).toBe(false)
54+
})
55+
})
56+
})

0 commit comments

Comments
 (0)