Skip to content

Commit 96ebb03

Browse files
committed
fix / refactor
1 parent cc14e52 commit 96ebb03

File tree

3 files changed

+146
-44
lines changed

3 files changed

+146
-44
lines changed

.changeset/fast-lions-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Add support for more argument resolver edge cases

packages/browser/src/core/arguments-resolver/__tests__/index.test.ts

Lines changed: 113 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
resolvePageArguments,
44
resolveUserArguments,
55
resolveAliasArguments,
6-
} from '../'
6+
} from '../index'
77
import { Callback } from '../../events'
88
import { User } from '../../user'
99

@@ -195,7 +195,7 @@ describe(resolveArguments, () => {
195195
})
196196

197197
describe(resolvePageArguments, () => {
198-
test('empty strings ("", "", null, { integrations })', () => {
198+
test('empty strings ("", "", {}, { integrations })', () => {
199199
const [category, name, properties, options] = resolvePageArguments(
200200
'',
201201
'',
@@ -211,14 +211,44 @@ describe(resolvePageArguments, () => {
211211

212212
expect(category).toEqual('')
213213
expect(name).toEqual('')
214-
expect(properties).toEqual({})
215214
expect(options).toEqual({
216215
integrations: {
217216
Amplitude: {
218217
sessionId: '123',
219218
},
220219
},
221220
})
221+
expect(properties).toEqual({})
222+
})
223+
224+
it('should accept (category, name)', () => {
225+
const [category, name, properties, options, cb] = resolvePageArguments(
226+
'category',
227+
'name'
228+
)
229+
230+
expect(name).toEqual('name')
231+
expect(category).toEqual('category')
232+
expect(properties).toEqual({})
233+
expect(options).toEqual({})
234+
expect(cb).toEqual(undefined)
235+
})
236+
237+
it('should accept (category, name, properties, options, cb)', () => {
238+
const fn = jest.fn()
239+
const [category, name, properties, options, cb] = resolvePageArguments(
240+
'foo',
241+
'name',
242+
bananaPhone,
243+
baseOptions,
244+
fn
245+
)
246+
247+
expect(category).toEqual('foo')
248+
expect(name).toEqual('name')
249+
expect(properties).toEqual(bananaPhone)
250+
expect(options).toEqual(baseOptions)
251+
expect(cb).toEqual(fn)
222252
})
223253

224254
test('should accept (category, name, properties, callback)', () => {
@@ -254,52 +284,48 @@ describe(resolvePageArguments, () => {
254284
expect(options).toEqual({})
255285
})
256286

257-
it('should accept (name, properties, options, callback)', () => {
258-
const fn = jest.fn()
287+
it('should accept (name, properties)', () => {
259288
const [category, name, properties, options, cb] = resolvePageArguments(
260289
'name',
261-
bananaPhone,
262-
baseOptions,
263-
fn
290+
bananaPhone
264291
)
265292

266-
expect(category).toEqual(null)
267293
expect(name).toEqual('name')
294+
expect(category).toEqual(null)
268295
expect(properties).toEqual(bananaPhone)
269-
expect(options).toEqual(baseOptions)
270-
expect(cb).toEqual(fn)
296+
expect(options).toEqual({})
297+
expect(cb).toEqual(undefined)
271298
})
272299

273-
it('should accept (name, properties, callback)', () => {
300+
it('should accept (name, properties, options, callback)', () => {
274301
const fn = jest.fn()
275302
const [category, name, properties, options, cb] = resolvePageArguments(
276303
'name',
277304
bananaPhone,
305+
baseOptions,
278306
fn
279307
)
280308

281309
expect(category).toEqual(null)
282310
expect(name).toEqual('name')
283311
expect(properties).toEqual(bananaPhone)
312+
expect(options).toEqual(baseOptions)
284313
expect(cb).toEqual(fn)
285-
expect(options).toEqual({})
286314
})
287315

288-
it('should accept (category, name, properties, options, cb)', () => {
316+
it('should accept (name, properties, callback)', () => {
289317
const fn = jest.fn()
290318
const [category, name, properties, options, cb] = resolvePageArguments(
291-
'foo',
292319
'name',
293320
bananaPhone,
294-
baseOptions,
295321
fn
296322
)
297323

298-
expect(category).toEqual('foo')
324+
expect(category).toEqual(null)
299325
expect(name).toEqual('name')
300326
expect(properties).toEqual(bananaPhone)
301-
expect(options).toEqual(baseOptions)
302327
expect(cb).toEqual(fn)
328+
expect(options).toEqual({})
303329
})
304330

305331
it('should accept (name, callback)', () => {
@@ -348,7 +374,7 @@ describe(resolvePageArguments, () => {
348374
expect(category).toEqual(null)
349375
})
350376

351-
test('should accept (category = null, name, properties, options, callback)', () => {
377+
test('should accept (null, name, properties, options, callback)', () => {
352378
const fn = jest.fn()
353379
const [category, name, properties, options, cb] = resolvePageArguments(
354380
null,
@@ -365,6 +391,64 @@ describe(resolvePageArguments, () => {
365391
expect(cb).toEqual(fn)
366392
})
367393

394+
test('should accept (name, null, properties, options, callback)', () => {
395+
const fn = jest.fn()
396+
const [category, name, properties, options, cb] = resolvePageArguments(
397+
'name',
398+
null,
399+
bananaPhone,
400+
baseOptions,
401+
fn
402+
)
403+
404+
expect(category).toEqual(null)
405+
expect(name).toEqual('name')
406+
expect(properties).toEqual(bananaPhone)
407+
expect(options).toEqual(baseOptions)
408+
expect(cb).toEqual(fn)
409+
})
410+
411+
test('should accept (name, null, properties)', () => {
412+
const [category, name, properties, options] = resolvePageArguments(
413+
'name',
414+
null,
415+
bananaPhone
416+
)
417+
418+
expect(name).toEqual('name')
419+
expect(category).toEqual(null)
420+
expect(properties).toEqual(bananaPhone)
421+
expect(options).toEqual({})
422+
})
423+
424+
test('should accept (name, null, null, options)', () => {
425+
const [category, name, properties, options] = resolvePageArguments(
426+
'name',
427+
null,
428+
null,
429+
baseOptions
430+
)
431+
432+
expect(name).toEqual('name')
433+
expect(category).toEqual(null)
434+
expect(properties).toEqual({})
435+
expect(options).toEqual(baseOptions)
436+
})
437+
438+
test('should accept (null, null, properties)', () => {
439+
const [category, name, properties, options, cb] = resolvePageArguments(
440+
null,
441+
null,
442+
bananaPhone
443+
)
444+
445+
expect(category).toEqual(null)
446+
expect(name).toEqual(null)
447+
expect(properties).toEqual(bananaPhone)
448+
expect(options).toEqual({})
449+
expect(cb).toEqual(undefined)
450+
})
451+
368452
test('should accept (null, null, properties, options, callback)', () => {
369453
const fn = jest.fn()
370454
const [category, name, properties, options, cb] = resolvePageArguments(
@@ -425,6 +509,16 @@ describe(resolvePageArguments, () => {
425509
expect(options).toEqual({})
426510
expect(cb).toEqual(fn)
427511
})
512+
513+
test('should accept (null, null, null, null, callback)', () => {
514+
const fn = jest.fn()
515+
const [category, name, properties, options, cb] = resolvePageArguments(fn)
516+
expect(category).toEqual(null)
517+
expect(name).toEqual(null)
518+
expect(properties).toEqual({})
519+
expect(options).toEqual({})
520+
expect(cb).toEqual(fn)
521+
})
428522
})
429523

430524
describe(resolveUserArguments, () => {

packages/browser/src/core/arguments-resolver/index.ts

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ export function resolveArguments(
5151
return [name, data, opts, cb]
5252
}
5353

54-
const isNil = (val: any): val is null | undefined =>
55-
val === null || val === undefined
56-
5754
/**
5855
* Helper for page, screen methods
5956
*/
@@ -76,13 +73,18 @@ export function resolvePageArguments(
7673
let resolvedName: string | undefined | null = null
7774
const args = [category, name, properties, options, callback]
7875

79-
// handle:
80-
// - analytics.page('name')
81-
// - analytics.page('category', 'name')
76+
// The legacy logic is basically:
77+
// - If there is a string, it's the name
78+
// - If there are two strings, it's category and name
8279
const strings = args.filter(isString)
8380
if (strings.length === 1) {
84-
resolvedCategory = null
85-
resolvedName = strings[0]
81+
if (isString(args[1])) {
82+
resolvedName = args[1]
83+
resolvedCategory = null
84+
} else {
85+
resolvedName = strings[0]
86+
resolvedCategory = null
87+
}
8688
} else if (strings.length === 2) {
8789
if (typeof args[0] === 'string') {
8890
resolvedCategory = args[0]
@@ -106,25 +108,26 @@ export function resolvePageArguments(
106108
// - analytics.page('category', 'name', properties, options, callback)
107109
// - analytics.page('category', 'name', callback)
108110
// - analytics.page(callback), etc
109-
args.forEach((obj, argIdx) => {
110-
if (isPlainObject(obj)) {
111-
if (argIdx === 0) {
112-
resolvedProperties = obj
113-
}
114-
115-
if (argIdx === 1 || argIdx == 2) {
116-
if (isNil(resolvedProperties)) {
117-
resolvedProperties = obj
118-
} else {
119-
resolvedOptions = obj
120-
}
121-
}
122111

123-
if (argIdx === 3) {
124-
resolvedOptions = obj
125-
}
112+
// The legacy logic is basically:
113+
// - If there is a plain object, it's the properties
114+
// - If there are two plain objects, it's properties and options
115+
const objects = args.filter(isPlainObject)
116+
if (objects.length === 1) {
117+
if (isPlainObject(args[2])) {
118+
resolvedOptions = {}
119+
resolvedProperties = args[2]
120+
} else if (isPlainObject(args[3])) {
121+
resolvedProperties = {}
122+
resolvedOptions = args[3]
123+
} else {
124+
resolvedProperties = objects[0]
125+
resolvedOptions = {}
126126
}
127-
})
127+
} else if (objects.length === 2) {
128+
resolvedProperties = objects[0]
129+
resolvedOptions = objects[1]
130+
}
128131

129132
return [
130133
resolvedCategory,

0 commit comments

Comments
 (0)