Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle null as the first argument in identify #950

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nasty-tables-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Fixes calls to .identify() with null as id
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,33 @@ describe(resolveUserArguments, () => {
expect(traits).toEqual(userTraits)
expect(options).toEqual({})
})

it('should accept (undefined, traits)', () => {
user.reset()
const [id, traits, options] = resolver(undefined, userTraits)
expect(traits).toEqual(userTraits)
expect(options).toEqual({})
expect(id).toEqual(null)
})

it('should accept (null, traits) with unknown identity', () => {
user.reset()
const [id, traits, options] = resolver(null, userTraits)
console.log(id, traits, options)
oscb marked this conversation as resolved.
Show resolved Hide resolved
expect(traits).toEqual(userTraits)
expect(options).toEqual({})
expect(id).toEqual(null)
})

it('should accept (null, traits) when identity is set', () => {
user.reset()
user.identify('something')
const [id, traits, options] = resolver(null, userTraits)
console.log(id, traits, options)
oscb marked this conversation as resolved.
Show resolved Hide resolved
expect(traits).toEqual(userTraits)
expect(options).toEqual({})
expect(id).toEqual('something')
})
})

describe(resolveAliasArguments, () => {
Expand Down
58 changes: 46 additions & 12 deletions packages/browser/src/core/arguments-resolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,56 @@ export const resolveUserArguments = <T extends Traits, U extends User>(
user: U
): ResolveUser<T> => {
return (...args): ReturnType<ResolveUser<T>> => {
let id: string | ID | null = null
id = args.find(isString) ?? args.find(isNumber)?.toString() ?? user.id()

const objects = args.filter((obj) => {
if (id === null) {
return isPlainObject(obj)
const values: {
id?: ID
traits?: T | null
options?: Options
callback?: Callback
} = {}
// It's a stack so it's reversed so that we go through each of the expected arguments
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a leetcode problem 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It totally is

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I f'ing love this.

const orderStack: Array<keyof typeof values> = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the old TS array syntax???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason eslint didn't like the new syntax :(

'callback',
'options',
'traits',
'id',
]

// Read each argument and eval the possible values here
for (const arg of args) {
let current = orderStack.pop()
if (current === 'id') {
if (isString(arg) || isNumber(arg)) {
values.id = arg.toString()
continue
}
if (arg === null || arg === undefined) {
continue
}
// First argument should always be the id, if it is not a valid value we can skip it
current = orderStack.pop()
}
return isPlainObject(obj) || obj === null
}) as Array<Traits | null>

const traits = (objects[0] ?? {}) as T
const opts = (objects[1] ?? {}) as Options
// Traits and Options
if (
(current === 'traits' || current === 'options') &&
(arg === null || arg === undefined || isPlainObject(arg))
) {
values[current] = arg as T
}

const resolvedCallback = args.find(isFunction) as Callback | undefined
// Callback
if (isFunction(arg)) {
values.callback = arg as Callback
break // This is always the last argument
}
}

return [id, traits, opts, resolvedCallback]
return [
values.id ?? user.id(),
(values.traits ?? {}) as T,
values.options ?? {},
values.callback,
]
}
}

Expand Down