Skip to content

Commit 2569b6d

Browse files
author
竹程
committed
init
0 parents commit 2569b6d

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
jstype.js
2+
---
3+
类型检测库,弥补typeof的问题
4+
5+
### 如何使用
6+
7+
```javascript
8+
import * as jstype from 'jstype'
9+
10+
jstype.isNumber(1) // true
11+
jstype.isString(1) // false
12+
```

index.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict'
2+
3+
const types = {
4+
undefined: 'undefined',
5+
string: 'string',
6+
number: 'number',
7+
boolean: 'boolean',
8+
array: 'array',
9+
object: 'object',
10+
function: 'function'
11+
}
12+
13+
const validators = {
14+
[types.array]: (value) => {
15+
return typeof value === 'object' && Object.prototype.toString.call(value) === '[object Array]'
16+
},
17+
[types.object]: (value) => {
18+
return typeof value === 'object' && Object.prototype.toString.call(value) !== '[object Array]'
19+
}
20+
}
21+
22+
function isTypeOf (value, type) {
23+
let func = validators[type]
24+
if (func) {
25+
return func(value)
26+
} else {
27+
return typeof value === type
28+
}
29+
}
30+
31+
function isUndefined (value) {
32+
return isTypeOf(value, types.undefined)
33+
}
34+
35+
function isString (value) {
36+
return isTypeOf(value, types.string)
37+
}
38+
39+
function isNumber (value) {
40+
return isTypeOf(value, types.number)
41+
}
42+
43+
function isBool (value) {
44+
return isTypeOf(value, types.boolean)
45+
}
46+
47+
function isArray (value) {
48+
return isTypeOf(value, types.array)
49+
}
50+
51+
function isObject (value) {
52+
return isTypeOf(value, types.object)
53+
}
54+
55+
function isFunction (value) {
56+
return isTypeOf(value, types.function)
57+
}
58+
59+
function isOneOf (value, values) {
60+
for (let val of values) {
61+
if (val === value) {
62+
return true
63+
}
64+
}
65+
return false
66+
}
67+
68+
export {
69+
types,
70+
isTypeOf,
71+
isUndefined,
72+
isString,
73+
isNumber,
74+
isBool,
75+
isArray,
76+
isObject,
77+
isFunction,
78+
isOneOf
79+
}

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "jstype",
3+
"version": "1.0.0",
4+
"description": "javascript type",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+ssh://git@github.com/junhey/jstype.git"
12+
},
13+
"keywords": [
14+
"javascript",
15+
"type"
16+
],
17+
"author": "junhey",
18+
"license": "ISC",
19+
"bugs": {
20+
"url": "https://github.com/junhey/jstype/issues"
21+
},
22+
"homepage": "https://github.com/junhey/jstype#readme"
23+
}

0 commit comments

Comments
 (0)