npm.io
2.0.0 • Published 4 years ago

discord-snowflake

Licence
MIT
Version
2.0.0
Deps
0
Size
8 kB
Vulns
0
Weekly
0

discord-snowflake

discord-snowflake is a library for parsing Discord Snowflakes.

Usage

To install the package, run the following in your terminal:

npm install discord-snowflake

Then in your code import the library:

import * as SnowflakeUtils from "discord-snowflake";

API

EPOCH

A constant representing the Discord epoch.

import { EPOCH } from "discord-snowflake";
Type: Snowflake

A type representing a Discord Snowflake.

import type { Snowflake } from "discord-snowflake";
Typeguard: isSnowflake(id: string): boolean

A typeguard function that returns whether the string is a valid Snowflake or not.

import { isSnowflake } from "discord-snowflake";

console.log(isSnowflake("90339695967350784"));
// => true
getTimestamp(snowflake: Snowflake): number
import { getTimestamp } from "discord-snowflake";

console.log(getTimestamp("90339695967350784"));
// => 1441609061949
getDate(snowflake: Snowflake): Date
import { getDate } from "discord-snowflake";

console.log(getDate("90339695967350784"));
// =>  2015-09-07T06:57:41.949Z (date object)
getWorkerId(snowflake: Snowflake): number
import { getWorkerId } from "discord-snowflake";

console.log(getWorkerId("90339695967350784"));
// => 0
getProcessId(snowflake: Snowflake): Number
import { getProcessId } from "discord-snowflake";

console.log(getProcessId("90339695967350784"));
// => 3
getIncrement(snowflake: Snowflake): number
import { getIncrement } from "discord-snowflake";

console.log(getIncrement("90339695967350784"));
// => 0
parse(snowflake: Snowflake)
import { parse } from "discord-snowflake";

console.log(parse("90339695967350784"));
// => { timestamp: 1441609061949, workerId: 0, processId: 3, increment: 0 }

Credits

Special thanks to theking465 for the original package implementation.