trim-everything trim所有数据类型

trim-everything

trim所有字段,因为底层使用的JSON.stringify作为遍历器,所以如果字段的值是function,那么会被trim掉。

项目地址:trim-everything

安装

1
2
3
npm i -S trim-everything

yarn add trim-everything

特点

  • trim undefined
  • trim null
  • trim number
  • trim string
  • trim object
  • trim array
  • 零依赖

开始

下面使用jest做的测试用例,可以从中看出trim的用法。

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* global test, expect */
const trim = require('trim-everything')

test('trim undefined', () => {
expect(trim()).toBeUndefined()
})

test('trim null', () => {
expect(trim(null)).toBeNull()
})

test('trim number', () => {
expect(trim(12.12)).toBe(12.12)
})

test('trim string', () => {
expect(trim(' 12abcd ')).toBe('12abcd')
})

test('trim object', () => {
expect(trim({
userName: ' wangdd ',
age: 12,
some: false,
address: ' shanghai'
}))
.toEqual({
userName: 'wangdd',
age: 12,
some: false,
address: 'shanghai'
})
})

test('trim array', () => {
expect(trim([
{
userName: ' wangdd ',
age: 12,
some: false,
address: ' shanghai'
},
' abcd ',
false,
12.12,
{
userName: ' wangdd ',
age: 12,
some: false,
address: ' shanghai',
child: {
userName: ' wangdd ',
age: 12,
some: false,
address: ' shanghai'
}
}
]))
.toEqual(
[
{
userName: 'wangdd',
age: 12,
some: false,
address: 'shanghai'
},
'abcd',
false,
12.12,
{
userName: 'wangdd',
age: 12,
some: false,
address: 'shanghai',
child: {
userName: 'wangdd',
age: 12,
some: false,
address: 'shanghai'
}
}
]
)
})