-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
62 lines (55 loc) · 1.16 KB
/
user.js
File metadata and controls
62 lines (55 loc) · 1.16 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
// MIT License
// Copyright (c) 2026 Open2b
// See the LICENSE file for full text.
import { uuid } from './utils.js'
class User {
#storage
constructor(storage) {
this.#storage = storage
}
id(id) {
if (id === null) {
this.#storage.setUserId()
return null
}
if ((typeof id === 'string' && id !== '') || typeof id === 'number') {
id = String(id)
const previousId = this.#storage.userId()
if (id !== previousId) {
this.#storage.setUserId(id)
if (previousId != null) {
this.#storage.setTraits('user')
this.#storage.setAnonymousId(uuid())
}
}
return id
}
return this.#storage.userId()
}
anonymousId(id) {
if (id === undefined) {
id = this.#storage.anonymousId()
if (id === null) {
id = uuid()
}
} else if (typeof id === 'number') {
id = String(id)
}
if (typeof id === 'string' && id !== '') {
this.#storage.setAnonymousId(id)
} else {
this.#storage.setAnonymousId(uuid())
}
return id
}
traits(traits) {
if (traits !== undefined) {
if (traits === null) {
traits = {}
}
this.#storage.setTraits('user', traits)
}
return this.#storage.traits('user')
}
}
export default User