Skip to content

Commit 58a100f

Browse files
committed
add formatInstanceId and formatLocation
1 parent d5e54e7 commit 58a100f

File tree

9 files changed

+237
-10
lines changed

9 files changed

+237
-10
lines changed

Readme.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,23 @@ pnpm add vrchat-location-parser
1717
## Usage
1818

1919
```ts
20-
import { parseInstance, parseLocation } from 'vrchat-location-parser'
20+
import {
21+
parseInstance,
22+
parseLocation,
23+
formatLocation,
24+
} from 'vrchat-location-parser'
2125

2226
parseLocation('wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd:12345~region(eu)')
2327

2428
parseInstance('wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd', '12345~region(eu)')
2529

30+
formatLocation({
31+
worldId: 'wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd',
32+
name: '12345',
33+
type: 'public',
34+
})
2635
```
2736

2837
## Contributing
29-
Pull requests are welcome! If you have any ideas, feel free to open an issue.
3038

39+
Pull requests are welcome! If you have any ideas, feel free to open an issue.

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"@types/node": "^22.13.1",
1515
"nodemon": "^3.1.9",
16+
"ts-node": "^10.9.2",
1617
"typescript": "^5.7.3"
1718
}
1819
}

example/pnpm-lock.yaml

Lines changed: 129 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/src/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { parseInstance, parseLocation } from 'vrchat-location-parser'
1+
import {
2+
parseInstance,
3+
parseLocation,
4+
formatInstanceId,
5+
formatLocation,
6+
} from 'vrchat-location-parser'
27

38
console.log(
49
parseLocation(
@@ -22,9 +27,19 @@ console.log(
2227
parseLocation('wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd:69420~region(us)'),
2328
)
2429

30+
const instance = parseInstance(
31+
'wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd',
32+
'69420~region(us)',
33+
)
34+
35+
console.log(instance)
36+
console.log(formatInstanceId(instance!))
37+
console.log(formatLocation(instance!))
38+
2539
console.log(
26-
parseInstance(
27-
'wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd',
28-
'69420~region(us)',
29-
),
40+
formatLocation({
41+
worldId: 'wrld_4432ea9b-729c-46e3-8eaf-846aa0a37fdd',
42+
name: '12345',
43+
type: 'public',
44+
}),
3045
)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vrchat-location-parser",
3-
"version": "0.1.3",
3+
"version": "0.2.0",
44
"description": "Parser for VRchat Locations",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/formatInstanceId.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { Instance } from './interfaces/Instance'
2+
3+
/**
4+
* Format the instance from the instance object.
5+
* @param instance instance object
6+
* @returns object with worldId and instanceId
7+
*/
8+
export const formatInstanceId = (instance: Instance) => {
9+
let instanceId = instance.name
10+
11+
// hidden(usr_00000000-0000-0000-0000-000000000000)
12+
if (instance.type == 'friends+') {
13+
instanceId += `~hidden(${instance.userId})`
14+
}
15+
// friends(usr_00000000-0000-0000-0000-000000000000)
16+
else if (instance.type == 'friends') {
17+
instanceId += `~friends(${instance.userId})`
18+
}
19+
// private(usr_00000000-0000-0000-0000-000000000000)
20+
else if (instance.type == 'invite+') {
21+
instanceId += `~private(${instance.userId})~canRequestInvite`
22+
}
23+
24+
// private(usr_00000000-0000-0000-0000-000000000000)~canRequestInvite
25+
else if (instance.type == 'invite') {
26+
instanceId += `~private(${instance.userId})`
27+
}
28+
// group(grp_00000000-0000-0000-0000-000000000000)
29+
else if (
30+
instance.type == 'group' ||
31+
instance.type == 'groupPublic' ||
32+
instance.type == 'group+'
33+
) {
34+
instanceId += `~group(${instance.groupId})`
35+
36+
if (instance.type == 'groupPublic') {
37+
instanceId += '~groupAccessType(public)'
38+
} else if (instance.type == 'group+') {
39+
instanceId += '~groupAccessType(plus)'
40+
} else {
41+
instanceId += '~groupAccessType(members)'
42+
}
43+
44+
if (instance.require18yo) {
45+
instanceId += '~ageGate'
46+
}
47+
}
48+
49+
if (instance.region) {
50+
instanceId += `~region(${instance.region})`
51+
}
52+
53+
if (instance.nonce) {
54+
instanceId += `~nonce(${instance.nonce})`
55+
}
56+
57+
return instanceId
58+
}

src/formatLocation.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { formatInstanceId } from './formatInstanceId'
2+
import type { Instance } from './interfaces/Instance'
3+
4+
/**
5+
* Format the location string from the instance object.
6+
* @param instance instance object
7+
* @returns location string
8+
*/
9+
export const formatLocation = (instance: Instance) => {
10+
return `${instance.worldId}:${formatInstanceId(instance)}`
11+
}

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export { parseInstance } from './parseInstance'
22
export { parseLocation } from './parseLocation'
3-
export { Instance } from './interfaces/Instance'
3+
4+
export { formatInstanceId } from './formatInstanceId'
5+
export { formatLocation } from './formatLocation'
6+
7+
export type { Instance } from './interfaces/Instance'

src/parseInstance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Instance } from './interfaces/Instance'
1+
import type { Instance } from './interfaces/Instance'
22

33
/**
44
* Parses the instance

0 commit comments

Comments
 (0)