Skip to content

Commit d1eb93c

Browse files
committed
wip
1 parent cc14e52 commit d1eb93c

File tree

3 files changed

+145
-42
lines changed

3 files changed

+145
-42
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: 112 additions & 17 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

@@ -219,6 +219,37 @@ describe(resolvePageArguments, () => {
219219
},
220220
},
221221
})
222+
expect(properties).toEqual({})
223+
})
224+
225+
it('should accept (category, name)', () => {
226+
const [category, name, properties, options, cb] = resolvePageArguments(
227+
'category',
228+
'name'
229+
)
230+
231+
expect(name).toEqual('name')
232+
expect(category).toEqual('category')
233+
expect(properties).toEqual({})
234+
expect(options).toEqual({})
235+
expect(cb).toEqual(undefined)
236+
})
237+
238+
it('should accept (category, name, properties, options, cb)', () => {
239+
const fn = jest.fn()
240+
const [category, name, properties, options, cb] = resolvePageArguments(
241+
'foo',
242+
'name',
243+
bananaPhone,
244+
baseOptions,
245+
fn
246+
)
247+
248+
expect(category).toEqual('foo')
249+
expect(name).toEqual('name')
250+
expect(properties).toEqual(bananaPhone)
251+
expect(options).toEqual(baseOptions)
252+
expect(cb).toEqual(fn)
222253
})
223254

224255
test('should accept (category, name, properties, callback)', () => {
@@ -254,52 +285,48 @@ describe(resolvePageArguments, () => {
254285
expect(options).toEqual({})
255286
})
256287

257-
it('should accept (name, properties, options, callback)', () => {
258-
const fn = jest.fn()
288+
it('should accept (name, properties)', () => {
259289
const [category, name, properties, options, cb] = resolvePageArguments(
260290
'name',
261-
bananaPhone,
262-
baseOptions,
263-
fn
291+
bananaPhone
264292
)
265293

266-
expect(category).toEqual(null)
267294
expect(name).toEqual('name')
295+
expect(category).toEqual(null)
268296
expect(properties).toEqual(bananaPhone)
269-
expect(options).toEqual(baseOptions)
270-
expect(cb).toEqual(fn)
297+
expect(options).toEqual({})
298+
expect(cb).toEqual(undefined)
271299
})
272300

273-
it('should accept (name, properties, callback)', () => {
301+
it('should accept (name, properties, options, callback)', () => {
274302
const fn = jest.fn()
275303
const [category, name, properties, options, cb] = resolvePageArguments(
276304
'name',
277305
bananaPhone,
306+
baseOptions,
278307
fn
279308
)
280309

281310
expect(category).toEqual(null)
282311
expect(name).toEqual('name')
283312
expect(properties).toEqual(bananaPhone)
313+
expect(options).toEqual(baseOptions)
284314
expect(cb).toEqual(fn)
285-
expect(options).toEqual({})
286315
})
287316

288-
it('should accept (category, name, properties, options, cb)', () => {
317+
it('should accept (name, properties, callback)', () => {
289318
const fn = jest.fn()
290319
const [category, name, properties, options, cb] = resolvePageArguments(
291-
'foo',
292320
'name',
293321
bananaPhone,
294-
baseOptions,
295322
fn
296323
)
297324

298-
expect(category).toEqual('foo')
325+
expect(category).toEqual(null)
299326
expect(name).toEqual('name')
300327
expect(properties).toEqual(bananaPhone)
301-
expect(options).toEqual(baseOptions)
302328
expect(cb).toEqual(fn)
329+
expect(options).toEqual({})
303330
})
304331

305332
it('should accept (name, callback)', () => {
@@ -348,7 +375,7 @@ describe(resolvePageArguments, () => {
348375
expect(category).toEqual(null)
349376
})
350377

351-
test('should accept (category = null, name, properties, options, callback)', () => {
378+
test('should accept (null, name, properties, options, callback)', () => {
352379
const fn = jest.fn()
353380
const [category, name, properties, options, cb] = resolvePageArguments(
354381
null,
@@ -365,6 +392,64 @@ describe(resolvePageArguments, () => {
365392
expect(cb).toEqual(fn)
366393
})
367394

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

430525
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)