Skip to content

Commit

Permalink
feat(FileData): add isFileData
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 1, 2018
1 parent 6fe5a19 commit 95b9da3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/FileData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import isObject from './isObject'

const flag = 0x666

export default class FileData<T = any> {
private readonly $$instanceOf = flag

private content: T

/**
Expand All @@ -18,4 +24,14 @@ export default class FileData<T = any> {
public get(): T {
return this.content
}

/**
* 检查值是否是 FileData。
*
* @param value 要检查的值
* @returns 是(true)或否(false)
*/
public static isFileData(value: any): boolean {
return isObject(value) && (value as any).$$instanceOf === flag
}
}
5 changes: 4 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,10 @@ describe('isChineseName', () => {
describe('FileData', () => {
test('ok', () => {
[1, true, /x/, {}].forEach(item => {
expect(new vtils.FileData(item).get()).toBe(item)
expect(vtils.FileData.isFileData(item)).toBeFalsy()
const fileData = new vtils.FileData(item)
expect(fileData.get()).toBe(item)
expect(vtils.FileData.isFileData(fileData)).toBeTruthy()
})
})
})

0 comments on commit 95b9da3

Please sign in to comment.