Skip to content

Commit 2b35a07

Browse files
committed
fix argument resolver
1 parent d6c504c commit 2b35a07

File tree

2 files changed

+43
-44
lines changed

2 files changed

+43
-44
lines changed

packages/browser/src/browser/__tests__/page-enrichment.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('Page Enrichment', () => {
165165
})
166166

167167
it('in page events, event.name overrides event.properties.name', async () => {
168-
const ctx = await ajs.page('My Event', undefined, undefined, {
168+
const ctx = await ajs.page('My Event', undefined, {
169169
name: 'some propery name',
170170
})
171171
expect(ctx.event.properties!.name).toBe('My Event')

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

Lines changed: 42 additions & 43 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
*/
@@ -70,12 +67,34 @@ export function resolvePageArguments(
7067
Options,
7168
Callback | undefined
7269
] {
73-
let resolvedCategory: string | undefined | null = null
74-
let resolvedName: string | undefined | null = null
7570
let resolvedProperties: EventProperties
7671
let resolvedOptions: Options
72+
let resolvedCategory: string | undefined | null = null
73+
let resolvedName: string | undefined | null = null
7774
const args = [category, name, properties, options, callback]
7875

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
79+
const strings = args.filter(isString)
80+
if (strings.length === 1) {
81+
if (isString(args[1])) {
82+
resolvedName = args[1]
83+
resolvedCategory = null
84+
} else {
85+
resolvedName = strings[0]
86+
resolvedCategory = null
87+
}
88+
} else if (strings.length === 2) {
89+
if (typeof args[0] === 'string') {
90+
resolvedCategory = args[0]
91+
}
92+
if (typeof args[1] === 'string') {
93+
resolvedName = args[1]
94+
}
95+
}
96+
97+
// handle: analytics.page('category', 'name', properties, options, callback)
7998
const resolvedCallback = args.find(isFunction) as Callback | undefined
8099

81100
// handle:
@@ -89,46 +108,26 @@ export function resolvePageArguments(
89108
// - analytics.page('category', 'name', properties, options, callback)
90109
// - analytics.page('category', 'name', callback)
91110
// - analytics.page(callback), etc
92-
args.forEach((arg, argIdx) => {
93-
if (isString(arg)) {
94-
if (argIdx === 0) {
95-
if (isString(arg)) {
96-
resolvedName = arg
97-
}
98-
}
99-
if (argIdx === 1) {
100-
resolvedCategory = args[0] as string | undefined | null
101-
resolvedName = arg
102-
}
103-
}
104-
105-
if (isPlainObject(arg)) {
106-
if (argIdx === 0) {
107-
resolvedProperties = arg
108-
}
109-
110-
if (argIdx === 1 || argIdx == 2) {
111-
if (isNil(resolvedProperties)) {
112-
resolvedProperties = arg
113-
} else {
114-
resolvedOptions = arg
115-
}
116-
}
117111

118-
if (argIdx === 3) {
119-
resolvedOptions = arg
120-
}
121-
}
122-
123-
if (isNil(arg)) {
124-
if (argIdx === 1) {
125-
if (isString(args[0])) {
126-
resolvedName = arg
127-
resolvedCategory = args[0]
128-
}
129-
}
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 = {}
130126
}
131-
})
127+
} else if (objects.length === 2) {
128+
resolvedProperties = objects[0]
129+
resolvedOptions = objects[1]
130+
}
132131

133132
return [
134133
resolvedCategory,

0 commit comments

Comments
 (0)