-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlodash.spec.ts
65 lines (57 loc) · 1.7 KB
/
lodash.spec.ts
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import _ from 'lodash'
// https://lodash.com/
describe('Lodash', () => {
test('_.cloneDeep: 深度拷贝', () => {
const src = {
name: 'Joel',
info: {
gender: 'M',
hobby: ['coding', 'reading']
}
}
const dest = _.cloneDeep(src)
expect(src).toEqual(dest)
})
test('_.isEqual: 深度比较', () => {
const src = {
name: 'Joel',
info: {
gender: 'M',
hobby: ['coding', 'reading']
}
}
const dest = _.cloneDeep(src)
const isSame = _.isEqual(src, dest)
expect(isSame).toEqual(true)
})
test('_.curry: 柯里化', () => {
const abc = function (a: any, b: any, c: any) {
return [a, b, c]
}
const curried = _.curry(abc)
expect(curried(1, 2, 3)).toEqual([1, 2, 3])
expect(curried(1, 2)(3)).toEqual([1, 2, 3])
expect(curried(1)(2)(3)).toEqual([1, 2, 3])
expect(curried(1)(_, 3)(2)).toEqual([1, 2, 3])
})
test('_.flow: 按顺序调用一系列函数', () => {
const sum = (a: number, b: number) => a + b
const showRes = (num: number) => `res is ${num}`
const sumAndShow = _.flow([sum, showRes])
expect(sumAndShow(1, 2)).toBe('res is 3')
})
describe('_.random: 生成随机数', () => {
test('随机整数', () => {
const randomInt = _.random(0, 5)
expect(Number.isInteger(randomInt)).toBe(true)
expect(randomInt >= 0 && randomInt <= 5).toBe(true)
// 默认值是 0,1
expect(_.random() >= 0 && _.random() <= 1).toBe(true)
})
test('随机小数', () => {
const randomFloat = _.random(0, 5, true)
expect(Number.isInteger(randomFloat)).toBe(false)
expect(randomFloat >= 0 && randomFloat <= 5).toBe(true)
})
})
})