Skip to content
本章目录
GitHub Workflow Status 
npm 
codecov 
NPM 
npm 
node-current 
GitHub Repo stars 
GitHub commit merge status 
GitHub language count 
GitHub top language 
GitHub commit activity 
GitHub last commit 
GitHub Release Date

快速上手

安装依赖

npm方式

sh
npm install tj-jstools
1

浏览器方式

js
<script src="https://cdn.jsdelivr.net/npm/tj-jstools@1.3.0/static/umd/index.js"></script>
<script>
  const {_tj} = window
  console.log(_tj);
</script>
1
2
3
4
5

引入后,查看全局变量中的window._tj对象,里面包含了所有工具函数。


判断数据类型

typescript
import { isInt, isFloat, isNumber} from 'tj-jstools'

const isNumRes = isNumber(12.9); // true
const isIntRes = isInt(12.9); // false
const isFloatRes = isFloat(12.9); // true

1
2
3
4
5
6

当你想确定某一个变量或者值,是否和你预想的一样是可以使用以上这些数据类型判断函数。

当你想获取某一个变量或者值具体的数据类型时,你可以使用以下函数:

typescript
import { getType, getArrayAllType, getObjectAllType} from 'tj-jstools'

const getTypeRes1 = getType(Array(1)) // array
const getTypeRes2 = getType({}) // object
const getTypeRes3 = getType() // undefined
const getTypeRes4 = getType(1/0) // infinite

// 判断数组里面的数据类型
 const arr = [true,null,undefined,1/0,5,5.01,{},[],()=>{},NaN,'']
 const arrRes = getArrayAllType(arr) 
  //['boolean', 'null', 'undefined', 'infinite', 'int', 'float','object',
  // 'array','function','NaN','string']
 
// 判断对象里面的数据类型
 const testObj = {
    a: true,
    b: null,
    c: undefined,
    d: 6,
    e: 6.01,
    f: 1/0,
    g: {},
    h: [],
    i: () => {}
  }
  const objRes = getObjectAllType(testObj)
  /*
[
  'boolean', 'null', 'undefined', 'int', 'float','infinite',
  'object',  'array', 'function'
]
  */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

浏览器缓存(cookie/localStorage/sessionStorage)

typescript
import { newStorage } from 'tj-jstools'
const Coptions = {
  prefix:'tj',
  linkSign: '@',
  suffix:'jstools',
  expireTime: 2,
  unitTime: 'd'
}
// 创建一个操作Cookie的实例
const CInstance = newStorage('cookie',Coptions)

//创建一个操作localStorage的实例
const LInstance = newStorage('local',Coptions)

// 创建一个操作sessionStorage的实例
const SInstance = newStorage('session',Coptions)

// 保存和获取cookie值
CInstance.setFun('test','testValue')
CInstance.getFun('test') // tj@test@jstools: testValue ; 过期时间:2天

// 保存和获取localStorage值
LInstance.setFun('test','testValue')
LInstance.getFun('test') // tj@test@jstools: testValue ; 过期时间:2天

// 保存和获取sessionStorage值
SInstance.setFun('test','testValue')
SInstance.getFun('test') // tj@test@jstools: testValue ; 过期时间:2天
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

注意:

  • 对sessionStorage设置过期时间,其实效果不大,会随着浏览器的关闭而消亡

  • 如果cookie不设置expires,cookie 会在对话结束时过期

其他详情查看函数具体文档

Released under the MIT License.