-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathindex.spec.ts
More file actions
180 lines (147 loc) · 5.49 KB
/
index.spec.ts
File metadata and controls
180 lines (147 loc) · 5.49 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { describe, it, expect } from 'vitest';
import { groupBy, objectEntries, objectValues, find, sprintf, keyBy, assignBy } from '.'
describe('utils', () => {
describe('groupBy', () => {
it('should group values by some key function', () => {
const input = [
{ firstName: 'jordan', lastName: 'foo' },
{ firstName: 'jordan', lastName: 'bar' },
{ firstName: 'james', lastName: 'foxy' },
]
const result = groupBy(input, item => item.firstName)
expect(result).toEqual([
[
{ firstName: 'jordan', lastName: 'foo' },
{ firstName: 'jordan', lastName: 'bar' },
],
[{ firstName: 'james', lastName: 'foxy' }],
])
})
})
describe('objectEntries', () => {
it('should return object entries', () => {
expect(objectEntries({ foo: 'bar', bar: 123 })).toEqual([['foo', 'bar'], ['bar', 123]])
})
})
describe('objectValues', () => {
it('should return object values', () => {
expect(objectValues({ foo: 'bar', bar: 123 })).toEqual(['bar', 123])
})
// TODO test for enumerable properties only
})
describe('find', () => {
it('should return the value if found in an array', () => {
const input = [
{ firstName: 'jordan', lastName: 'foo' },
{ firstName: 'jordan', lastName: 'bar' },
{ firstName: 'james', lastName: 'foxy' },
]
expect(find(input, item => item.firstName === 'jordan')).toEqual({
firstName: 'jordan',
lastName: 'foo',
})
})
it('should return undefined if NOT found in an array', () => {
const input = [
{ firstName: 'jordan', lastName: 'foo' },
{ firstName: 'jordan', lastName: 'bar' },
{ firstName: 'james', lastName: 'foxy' },
]
expect(find(input, item => item.firstName === 'joe')).toBeUndefined()
})
})
describe('keyBy', () => {
it('return an object with keys generated from the key function', () => {
const input = [
{ key: 'foo', firstName: 'jordan', lastName: 'foo' },
{ key: 'bar', firstName: 'jordan', lastName: 'bar' },
{ key: 'baz', firstName: 'james', lastName: 'foxy' },
]
expect(keyBy(input, 'key')).toEqual({
foo: { key: 'foo', firstName: 'jordan', lastName: 'foo' },
bar: { key: 'bar', firstName: 'jordan', lastName: 'bar' },
baz: { key: 'baz', firstName: 'james', lastName: 'foxy' },
})
})
})
describe('assignBy', () => {
it('should assign array elements to an object using the specified key', () => {
const input = [
{ key: 'foo', firstName: 'jordan', lastName: 'foo' },
{ key: 'bar', firstName: 'jordan', lastName: 'bar' },
{ key: 'baz', firstName: 'james', lastName: 'foxy' },
]
const base = {}
assignBy(input, 'key', base)
expect(base).toEqual({
foo: { key: 'foo', firstName: 'jordan', lastName: 'foo' },
bar: { key: 'bar', firstName: 'jordan', lastName: 'bar' },
baz: { key: 'baz', firstName: 'james', lastName: 'foxy' },
})
})
it('should append to an existing object', () => {
const input = [
{ key: 'foo', firstName: 'jordan', lastName: 'foo' },
{ key: 'bar', firstName: 'jordan', lastName: 'bar' },
]
const base: any = { existing: 'value' }
assignBy(input, 'key', base)
expect(base).toEqual({
existing: 'value',
foo: { key: 'foo', firstName: 'jordan', lastName: 'foo' },
bar: { key: 'bar', firstName: 'jordan', lastName: 'bar' },
})
})
it('should handle empty array', () => {
const base: any = { existing: 'value' }
assignBy([], 'key', base)
expect(base).toEqual({ existing: 'value' })
})
it('should handle null/undefined array', () => {
const base: any = { existing: 'value' }
assignBy(null as any, 'key', base)
expect(base).toEqual({ existing: 'value' })
assignBy(undefined as any, 'key', base)
expect(base).toEqual({ existing: 'value' })
})
it('should override existing values with the same key', () => {
const input = [
{ key: 'foo', firstName: 'jordan', lastName: 'updated' },
{ key: 'bar', firstName: 'james', lastName: 'new' },
]
const base: any = {
foo: { key: 'foo', firstName: 'john', lastName: 'original' },
existing: 'value'
}
assignBy(input, 'key', base)
expect(base).toEqual({
existing: 'value',
foo: { key: 'foo', firstName: 'jordan', lastName: 'updated' },
bar: { key: 'bar', firstName: 'james', lastName: 'new' },
})
})
})
describe('sprintf', () => {
it('sprintf(msg)', () => {
expect(sprintf('this is my message')).toBe('this is my message')
})
it('sprintf(msg, arg1)', () => {
expect(sprintf('hi %s', 'jordan')).toBe('hi jordan')
})
it('sprintf(msg, arg1, arg2)', () => {
expect(sprintf('hi %s its %s', 'jordan', 'jon')).toBe('hi jordan its jon')
})
it('should print undefined if an argument is missing', () => {
expect(sprintf('hi %s its %s', 'jordan')).toBe('hi jordan its undefined')
})
it('should evaluate a function', () => {
expect(sprintf('hi %s its %s', 'jordan', () => 'a function')).toBe('hi jordan its a function')
})
it('should work with numbers', () => {
expect(sprintf('hi %s', 123)).toBe('hi 123')
})
it('should not error when passed an object', () => {
expect(sprintf('hi %s', { foo: 'bar' })).toBe('hi [object Object]')
})
})
})