@fluxerjs/util 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,5 +1,9 @@
1
1
  /**
2
2
  * Data structure that allows efficient storage of multiple flags (bits) in a single number.
3
+ *
4
+ * Note: JavaScript bitwise operations (|, &, ^, <<, >>) operate on 32-bit signed integers.
5
+ * Flags with values exceeding 2^30 may exhibit unexpected behavior. For permission-style
6
+ * bitfields with more than 30 bits, consider using BigInt-based implementations.
3
7
  */
4
8
  type BitFieldResolvable<S extends string> = S | number | BitField<S> | (S | number | BitField<S>)[];
5
9
  declare class BitField<S extends string> {
@@ -43,6 +47,7 @@ declare class SnowflakeUtil {
43
47
  static snowflakeFromTimestamp(timestamp: number): string;
44
48
  /**
45
49
  * Deconstructs a snowflake into its components.
50
+ * @throws TypeError If snowflake is not a valid numeric string
46
51
  */
47
52
  static deconstruct(snowflake: string): {
48
53
  timestamp: number;
@@ -270,12 +275,20 @@ declare function truncate(str: string, maxLength: number, suffix?: string): stri
270
275
  */
271
276
  declare function resolveColor(color: number | string | [number, number, number]): number;
272
277
  /**
273
- * Parse an emoji string (e.g. :name:123 or unicode) into id and name.
278
+ * Parse an emoji string into id and name.
279
+ * Supports: <a?:name:id> (mention), :name: (colons), name:id (API format), unicode.
274
280
  */
275
281
  declare function parseEmoji(emoji: string): {
276
282
  id: string | null;
277
283
  name: string;
284
+ animated?: boolean;
278
285
  } | null;
286
+ /**
287
+ * Parse a role mention string (e.g. <@&123456>) and extract the role ID.
288
+ * @param arg - String that may contain a role mention
289
+ * @returns The role ID if valid mention, otherwise null
290
+ */
291
+ declare function parseRoleMention(arg: string): string | null;
279
292
  /**
280
293
  * Convert emoji to the format used in API (for reactions etc).
281
294
  * Custom: "name:id", Unicode: "unicode"
@@ -286,4 +299,4 @@ declare function formatEmoji(emoji: {
286
299
  animated?: boolean;
287
300
  }): string;
288
301
 
289
- export { BitField, type BitFieldResolvable, FLUXER_EPOCH, MessageFlagsBitField, MessageFlagsBits, type MessageFlagsResolvable, type MessageFlagsString, PermissionFlags, type PermissionResolvable, type PermissionString, PermissionsBitField, SnowflakeUtil, UserFlagsBitField, UserFlagsBits, type UserFlagsResolvable, type UserFlagsString, escapeMarkdown, formatColor, formatEmoji, formatTimestamp, parseEmoji, resolveColor, truncate };
302
+ export { BitField, type BitFieldResolvable, FLUXER_EPOCH, MessageFlagsBitField, MessageFlagsBits, type MessageFlagsResolvable, type MessageFlagsString, PermissionFlags, type PermissionResolvable, type PermissionString, PermissionsBitField, SnowflakeUtil, UserFlagsBitField, UserFlagsBits, type UserFlagsResolvable, type UserFlagsString, escapeMarkdown, formatColor, formatEmoji, formatTimestamp, parseEmoji, parseRoleMention, resolveColor, truncate };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  /**
2
2
  * Data structure that allows efficient storage of multiple flags (bits) in a single number.
3
+ *
4
+ * Note: JavaScript bitwise operations (|, &, ^, <<, >>) operate on 32-bit signed integers.
5
+ * Flags with values exceeding 2^30 may exhibit unexpected behavior. For permission-style
6
+ * bitfields with more than 30 bits, consider using BigInt-based implementations.
3
7
  */
4
8
  type BitFieldResolvable<S extends string> = S | number | BitField<S> | (S | number | BitField<S>)[];
5
9
  declare class BitField<S extends string> {
@@ -43,6 +47,7 @@ declare class SnowflakeUtil {
43
47
  static snowflakeFromTimestamp(timestamp: number): string;
44
48
  /**
45
49
  * Deconstructs a snowflake into its components.
50
+ * @throws TypeError If snowflake is not a valid numeric string
46
51
  */
47
52
  static deconstruct(snowflake: string): {
48
53
  timestamp: number;
@@ -270,12 +275,20 @@ declare function truncate(str: string, maxLength: number, suffix?: string): stri
270
275
  */
271
276
  declare function resolveColor(color: number | string | [number, number, number]): number;
272
277
  /**
273
- * Parse an emoji string (e.g. :name:123 or unicode) into id and name.
278
+ * Parse an emoji string into id and name.
279
+ * Supports: <a?:name:id> (mention), :name: (colons), name:id (API format), unicode.
274
280
  */
275
281
  declare function parseEmoji(emoji: string): {
276
282
  id: string | null;
277
283
  name: string;
284
+ animated?: boolean;
278
285
  } | null;
286
+ /**
287
+ * Parse a role mention string (e.g. <@&123456>) and extract the role ID.
288
+ * @param arg - String that may contain a role mention
289
+ * @returns The role ID if valid mention, otherwise null
290
+ */
291
+ declare function parseRoleMention(arg: string): string | null;
279
292
  /**
280
293
  * Convert emoji to the format used in API (for reactions etc).
281
294
  * Custom: "name:id", Unicode: "unicode"
@@ -286,4 +299,4 @@ declare function formatEmoji(emoji: {
286
299
  animated?: boolean;
287
300
  }): string;
288
301
 
289
- export { BitField, type BitFieldResolvable, FLUXER_EPOCH, MessageFlagsBitField, MessageFlagsBits, type MessageFlagsResolvable, type MessageFlagsString, PermissionFlags, type PermissionResolvable, type PermissionString, PermissionsBitField, SnowflakeUtil, UserFlagsBitField, UserFlagsBits, type UserFlagsResolvable, type UserFlagsString, escapeMarkdown, formatColor, formatEmoji, formatTimestamp, parseEmoji, resolveColor, truncate };
302
+ export { BitField, type BitFieldResolvable, FLUXER_EPOCH, MessageFlagsBitField, MessageFlagsBits, type MessageFlagsResolvable, type MessageFlagsString, PermissionFlags, type PermissionResolvable, type PermissionString, PermissionsBitField, SnowflakeUtil, UserFlagsBitField, UserFlagsBits, type UserFlagsResolvable, type UserFlagsString, escapeMarkdown, formatColor, formatEmoji, formatTimestamp, parseEmoji, parseRoleMention, resolveColor, truncate };
package/dist/index.js CHANGED
@@ -34,6 +34,7 @@ __export(index_exports, {
34
34
  formatEmoji: () => formatEmoji,
35
35
  formatTimestamp: () => formatTimestamp,
36
36
  parseEmoji: () => parseEmoji,
37
+ parseRoleMention: () => parseRoleMention,
37
38
  resolveColor: () => resolveColor,
38
39
  truncate: () => truncate
39
40
  });
@@ -130,9 +131,15 @@ var SnowflakeUtil = class {
130
131
  }
131
132
  /**
132
133
  * Deconstructs a snowflake into its components.
134
+ * @throws TypeError If snowflake is not a valid numeric string
133
135
  */
134
136
  static deconstruct(snowflake) {
135
- const big = BigInt(snowflake);
137
+ let big;
138
+ try {
139
+ big = BigInt(snowflake);
140
+ } catch {
141
+ throw new TypeError("Invalid snowflake: must be a numeric string");
142
+ }
136
143
  return {
137
144
  timestamp: Number((big >> 22n) + FLUXER_EPOCH),
138
145
  date: new Date(Number((big >> 22n) + FLUXER_EPOCH)),
@@ -284,14 +291,34 @@ function resolveColor(color) {
284
291
  throw new TypeError("Color must be a number, hex string, or RGB array");
285
292
  }
286
293
  function parseEmoji(emoji) {
287
- const custom = /^<a?:\w+:(\d+)>$/;
288
- const match = emoji.match(custom);
289
- if (match) {
290
- const name = emoji.slice(emoji.indexOf(":") + 1, emoji.lastIndexOf(":"));
291
- return { id: match[1], name };
294
+ if (emoji == null || typeof emoji !== "string") return null;
295
+ const trimmed = emoji.trim();
296
+ if (trimmed.length === 0) return null;
297
+ const mention = /^<(a?):(\w+):(\d+)>$/;
298
+ const mentionMatch = trimmed.match(mention);
299
+ if (mentionMatch) {
300
+ return {
301
+ id: mentionMatch[3],
302
+ name: mentionMatch[2],
303
+ animated: mentionMatch[1] === "a"
304
+ };
292
305
  }
293
- if (emoji.length > 0) return { id: null, name: emoji };
294
- return null;
306
+ const nameId = /^(\w+):(\d{17,19})$/;
307
+ const nameIdMatch = trimmed.match(nameId);
308
+ if (nameIdMatch) {
309
+ return { id: nameIdMatch[2], name: nameIdMatch[1] };
310
+ }
311
+ const colons = /^:(\w+):$/;
312
+ const colonsMatch = trimmed.match(colons);
313
+ if (colonsMatch) {
314
+ return { id: null, name: colonsMatch[1] };
315
+ }
316
+ return { id: null, name: trimmed };
317
+ }
318
+ function parseRoleMention(arg) {
319
+ if (arg == null || typeof arg !== "string") return null;
320
+ const match = arg.trim().match(/^<@&(\d{17,19})>$/);
321
+ return match?.[1] ?? null;
295
322
  }
296
323
  function formatEmoji(emoji) {
297
324
  if (emoji.id) {
@@ -316,6 +343,7 @@ function formatEmoji(emoji) {
316
343
  formatEmoji,
317
344
  formatTimestamp,
318
345
  parseEmoji,
346
+ parseRoleMention,
319
347
  resolveColor,
320
348
  truncate
321
349
  });
package/dist/index.mjs CHANGED
@@ -89,9 +89,15 @@ var SnowflakeUtil = class {
89
89
  }
90
90
  /**
91
91
  * Deconstructs a snowflake into its components.
92
+ * @throws TypeError If snowflake is not a valid numeric string
92
93
  */
93
94
  static deconstruct(snowflake) {
94
- const big = BigInt(snowflake);
95
+ let big;
96
+ try {
97
+ big = BigInt(snowflake);
98
+ } catch {
99
+ throw new TypeError("Invalid snowflake: must be a numeric string");
100
+ }
95
101
  return {
96
102
  timestamp: Number((big >> 22n) + FLUXER_EPOCH),
97
103
  date: new Date(Number((big >> 22n) + FLUXER_EPOCH)),
@@ -243,14 +249,34 @@ function resolveColor(color) {
243
249
  throw new TypeError("Color must be a number, hex string, or RGB array");
244
250
  }
245
251
  function parseEmoji(emoji) {
246
- const custom = /^<a?:\w+:(\d+)>$/;
247
- const match = emoji.match(custom);
248
- if (match) {
249
- const name = emoji.slice(emoji.indexOf(":") + 1, emoji.lastIndexOf(":"));
250
- return { id: match[1], name };
252
+ if (emoji == null || typeof emoji !== "string") return null;
253
+ const trimmed = emoji.trim();
254
+ if (trimmed.length === 0) return null;
255
+ const mention = /^<(a?):(\w+):(\d+)>$/;
256
+ const mentionMatch = trimmed.match(mention);
257
+ if (mentionMatch) {
258
+ return {
259
+ id: mentionMatch[3],
260
+ name: mentionMatch[2],
261
+ animated: mentionMatch[1] === "a"
262
+ };
251
263
  }
252
- if (emoji.length > 0) return { id: null, name: emoji };
253
- return null;
264
+ const nameId = /^(\w+):(\d{17,19})$/;
265
+ const nameIdMatch = trimmed.match(nameId);
266
+ if (nameIdMatch) {
267
+ return { id: nameIdMatch[2], name: nameIdMatch[1] };
268
+ }
269
+ const colons = /^:(\w+):$/;
270
+ const colonsMatch = trimmed.match(colons);
271
+ if (colonsMatch) {
272
+ return { id: null, name: colonsMatch[1] };
273
+ }
274
+ return { id: null, name: trimmed };
275
+ }
276
+ function parseRoleMention(arg) {
277
+ if (arg == null || typeof arg !== "string") return null;
278
+ const match = arg.trim().match(/^<@&(\d{17,19})>$/);
279
+ return match?.[1] ?? null;
254
280
  }
255
281
  function formatEmoji(emoji) {
256
282
  if (emoji.id) {
@@ -274,6 +300,7 @@ export {
274
300
  formatEmoji,
275
301
  formatTimestamp,
276
302
  parseEmoji,
303
+ parseRoleMention,
277
304
  resolveColor,
278
305
  truncate
279
306
  };
package/package.json CHANGED
@@ -3,8 +3,22 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.0.6",
6
+ "version": "1.0.8",
7
7
  "description": "Shared utilities for @fluxerjs/core",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/fluxerjs/core.git",
11
+ "directory": "packages/util"
12
+ },
13
+ "bugs": "https://github.com/fluxerjs/core/issues",
14
+ "homepage": "https://fluxerjs.blstmo.com",
15
+ "keywords": [
16
+ "fluxer",
17
+ "util",
18
+ "snowflake",
19
+ "bitfield"
20
+ ],
21
+ "license": "AGPL-3.0",
8
22
  "main": "./dist/index.js",
9
23
  "module": "./dist/index.mjs",
10
24
  "types": "./dist/index.d.ts",
@@ -20,10 +34,15 @@
20
34
  ],
21
35
  "devDependencies": {
22
36
  "tsup": "^8.3.0",
23
- "typescript": "^5.6.0"
37
+ "typescript": "^5.6.0",
38
+ "vitest": "^4.0.18"
24
39
  },
25
40
  "scripts": {
26
41
  "build": "tsup src/index.ts --format cjs,esm --dts",
27
- "clean": "rm -rf dist"
42
+ "clean": "rm -rf dist",
43
+ "lint": "eslint src --max-warnings 0 --config ../../eslint.config.js",
44
+ "lint:fix": "eslint src --fix --config ../../eslint.config.js",
45
+ "test": "vitest run",
46
+ "test:coverage": "vitest run --coverage"
28
47
  }
29
48
  }