@sapphire/string-store 1.1.1-next.3d59d8ea → 1.2.0-next.80c79c3f

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.
@@ -66,14 +66,14 @@ declare class UnalignedUint16Array {
66
66
  static from(value: string | UnalignedUint16Array): UnalignedUint16Array;
67
67
  }
68
68
 
69
- interface IType<ValueType, BitSize extends number | null> {
69
+ interface IType<ValueType, BitSize extends number | null, InputValue = ValueType> {
70
70
  /**
71
71
  * Serialize a value to a buffer.
72
72
  *
73
73
  * @param buffer The buffer to write to
74
74
  * @param value The value to write
75
75
  */
76
- serialize(buffer: UnalignedUint16Array, value: Readonly<ValueType>): void;
76
+ serialize(buffer: UnalignedUint16Array, value: InputValue): void;
77
77
  /**
78
78
  * Deserialize a value from a buffer.
79
79
  *
@@ -97,10 +97,12 @@ declare const BigUint32Type: IType<bigint, 32>;
97
97
 
98
98
  declare const BigUint64Type: IType<bigint, 64>;
99
99
 
100
- declare const BitType: IType<number, 1>;
100
+ declare const BitType: IType<0 | 1, 1, number>;
101
101
 
102
102
  declare const BooleanType: IType<boolean, 1>;
103
103
 
104
+ declare function ConstantType<const ValueType>(constantValue: ValueType): IType<ValueType, 0, never>;
105
+
104
106
  declare function FixedLengthArrayType<ValueType, ValueBitSize extends number | null>(type: IType<ValueType, ValueBitSize>, length: number): IType<ValueType[], ValueBitSize extends null ? null : number>;
105
107
 
106
108
  declare const Float32Type: IType<number, 32>;
@@ -119,11 +121,9 @@ declare const Int64Type: IType<number, 64>;
119
121
 
120
122
  declare const Int8Type: IType<number, 8>;
121
123
 
122
- declare const SnowflakeType: {
123
- readonly serialize: (buffer: UnalignedUint16Array, value: bigint | string) => void;
124
- readonly deserialize: (buffer: UnalignedUint16Array, offset: Pointer) => bigint;
125
- readonly BIT_SIZE: 64;
126
- };
124
+ declare function NullableType<ValueType, ValueBitSize extends number | null>(type: IType<ValueType, ValueBitSize>): IType<ValueType | null, null, ValueType | null | undefined>;
125
+
126
+ declare const SnowflakeType: IType<bigint, 64, bigint | string>;
127
127
 
128
128
  declare const StringType: IType<string, null>;
129
129
 
@@ -141,33 +141,31 @@ declare const Uint8Type: IType<number, 8>;
141
141
 
142
142
  declare const t: {
143
143
  array: typeof ArrayType;
144
- bigInt32: IType<bigint, 32>;
145
- bigInt64: IType<bigint, 64>;
146
- bigUint32: IType<bigint, 32>;
147
- bigUint64: IType<bigint, 64>;
148
- bit: IType<number, 1>;
149
- boolean: IType<boolean, 1>;
144
+ bigInt32: IType<bigint, 32, bigint>;
145
+ bigInt64: IType<bigint, 64, bigint>;
146
+ bigUint32: IType<bigint, 32, bigint>;
147
+ bigUint64: IType<bigint, 64, bigint>;
148
+ bit: IType<0 | 1, 1, number>;
149
+ boolean: IType<boolean, 1, boolean>;
150
+ constant: typeof ConstantType;
150
151
  fixedLengthArray: typeof FixedLengthArrayType;
151
- float32: IType<number, 32>;
152
- float64: IType<number, 64>;
153
- int16: IType<number, 16>;
154
- int2: IType<number, 2>;
155
- int32: IType<number, 32>;
156
- int4: IType<number, 4>;
157
- int64: IType<number, 64>;
158
- int8: IType<number, 8>;
159
- snowflake: {
160
- readonly serialize: (buffer: UnalignedUint16Array, value: string | bigint) => void;
161
- readonly deserialize: (buffer: UnalignedUint16Array, offset: Pointer) => bigint;
162
- readonly BIT_SIZE: 64;
163
- };
164
- string: IType<string, null>;
165
- uint16: IType<number, 16>;
166
- uint2: IType<number, 2>;
167
- uint32: IType<number, 32>;
168
- uint4: IType<number, 4>;
169
- uint64: IType<number, 64>;
170
- uint8: IType<number, 8>;
152
+ float32: IType<number, 32, number>;
153
+ float64: IType<number, 64, number>;
154
+ int16: IType<number, 16, number>;
155
+ int2: IType<number, 2, number>;
156
+ int32: IType<number, 32, number>;
157
+ int4: IType<number, 4, number>;
158
+ int64: IType<number, 64, number>;
159
+ int8: IType<number, 8, number>;
160
+ nullable: typeof NullableType;
161
+ snowflake: IType<bigint, 64, string | bigint>;
162
+ string: IType<string, null, string>;
163
+ uint16: IType<number, 16, number>;
164
+ uint2: IType<number, 2, number>;
165
+ uint32: IType<number, 32, number>;
166
+ uint4: IType<number, 4, number>;
167
+ uint64: IType<number, 64, number>;
168
+ uint8: IType<number, 8, number>;
171
169
  };
172
170
 
173
171
  declare class Schema<Id extends number = number, Entries extends object = object> {
@@ -183,7 +181,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
183
181
  */
184
182
  get id(): Id;
185
183
  /**
186
- * The total bit size of the schema.
184
+ * The bit size of the entries in the schema.
187
185
  *
188
186
  * @remarks
189
187
  *
@@ -191,6 +189,15 @@ declare class Schema<Id extends number = number, Entries extends object = object
191
189
  * schema will also be `null`.
192
190
  */
193
191
  get bitSize(): number | null;
192
+ /**
193
+ * The total bit size of the entries in the schema and the ID.
194
+ *
195
+ * @remarks
196
+ *
197
+ * If any of the entries have a bit size of `null`, the total bit size of
198
+ * the schema will also be `null`.
199
+ */
200
+ get totalBitSize(): number | null;
194
201
  /**
195
202
  * Get a property from the schema.
196
203
  *
@@ -213,7 +220,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
213
220
  * The schema's ID is written to the buffer first, followed by each property
214
221
  * in the schema.
215
222
  */
216
- serialize(buffer: UnalignedUint16Array, value: Readonly<UnwrapSchemaEntries<Entries>>): void;
223
+ serialize(buffer: UnalignedUint16Array, value: Readonly<SerializeValueEntries<Entries>>): void;
217
224
  /**
218
225
  * Deserialize a value from a buffer.
219
226
  *
@@ -236,7 +243,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
236
243
  * @param type The type of the entry in the array
237
244
  * @returns The modified schema
238
245
  */
239
- array<const Name extends string, const ValueType, const ValueBitSize extends number | null>(name: Name, type: IType<ValueType, ValueBitSize>): Merge$1<Id, Entries, Name, IType<ValueType[], null>>;
246
+ array<const Name extends string, const ValueType, const ValueBitSize extends number | null>(name: Name, type: IType<ValueType, ValueBitSize>): Merge$1<Id, Entries, Name, IType<ValueType[], null, ValueType[]>>;
240
247
  /**
241
248
  * Adds a fixed length array property to the schema.
242
249
  *
@@ -247,28 +254,28 @@ declare class Schema<Id extends number = number, Entries extends object = object
247
254
  * @param length The length of the array
248
255
  * @returns The modified schema
249
256
  */
250
- fixedLengthArray<const Name extends string, const ValueType, const ValueBitSize extends number | null>(name: Name, type: IType<ValueType, ValueBitSize>, length: number): Merge$1<Id, Entries, Name, IType<ValueType[], ValueBitSize extends null ? null : number>>;
257
+ fixedLengthArray<const Name extends string, const ValueType, const ValueBitSize extends number | null>(name: Name, type: IType<ValueType, ValueBitSize>, length: number): Merge$1<Id, Entries, Name, IType<ValueType[], ValueBitSize extends null ? null : number, ValueType[]>>;
251
258
  /**
252
259
  * Adds a string property to the schema.
253
260
  *
254
261
  * @param name The name of the property
255
262
  * @returns The modified schema
256
263
  */
257
- string<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<string, null>>;
264
+ string<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<string, null, string>>;
258
265
  /**
259
266
  * Adds a boolean property to the schema.
260
267
  *
261
268
  * @param name The name of the property
262
269
  * @returns The modified schema
263
270
  */
264
- boolean<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<boolean, 1>>;
271
+ boolean<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<boolean, 1, boolean>>;
265
272
  /**
266
273
  * Adds a bit property to the schema.
267
274
  *
268
275
  * @param name The name of the property
269
276
  * @returns The modified schema
270
277
  */
271
- bit<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 1>>;
278
+ bit<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<0 | 1, 1, number>>;
272
279
  /**
273
280
  * Adds a 2-bit integer property to the schema.
274
281
  *
@@ -279,7 +286,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
279
286
  * @param name The name of the property
280
287
  * @returns The modified schema
281
288
  */
282
- int2<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 2>>;
289
+ int2<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 2, number>>;
283
290
  /**
284
291
  * Adds a 2-bit unsigned integer property to the schema.
285
292
  *
@@ -290,7 +297,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
290
297
  * @param name The name of the property
291
298
  * @returns The modified schema
292
299
  */
293
- uint2<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 2>>;
300
+ uint2<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 2, number>>;
294
301
  /**
295
302
  * Adds a 4-bit integer property to the schema.
296
303
  *
@@ -301,7 +308,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
301
308
  * @param name The name of the property
302
309
  * @returns The modified schema
303
310
  */
304
- int4<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 4>>;
311
+ int4<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 4, number>>;
305
312
  /**
306
313
  * Adds a 4-bit unsigned integer property to the schema.
307
314
  *
@@ -312,7 +319,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
312
319
  * @param name The name of the property
313
320
  * @returns The modified schema
314
321
  */
315
- uint4<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 4>>;
322
+ uint4<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 4, number>>;
316
323
  /**
317
324
  * Adds a 8-bit integer property to the schema.
318
325
  *
@@ -323,7 +330,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
323
330
  * @param name The name of the property
324
331
  * @returns The modified schema
325
332
  */
326
- int8<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 8>>;
333
+ int8<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 8, number>>;
327
334
  /**
328
335
  * Adds a 8-bit unsigned integer property to the schema.
329
336
  *
@@ -334,7 +341,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
334
341
  * @param name The name of the property
335
342
  * @returns The modified schema
336
343
  */
337
- uint8<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 8>>;
344
+ uint8<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 8, number>>;
338
345
  /**
339
346
  * Adds a 16-bit integer property to the schema.
340
347
  *
@@ -345,7 +352,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
345
352
  * @param name The name of the property
346
353
  * @returns The modified schema
347
354
  */
348
- int16<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 16>>;
355
+ int16<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 16, number>>;
349
356
  /**
350
357
  * Adds a 16-bit unsigned integer property to the schema.
351
358
  *
@@ -356,7 +363,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
356
363
  * @param name The name of the property
357
364
  * @returns The modified schema
358
365
  */
359
- uint16<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 16>>;
366
+ uint16<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 16, number>>;
360
367
  /**
361
368
  * Adds a 32-bit integer property to the schema.
362
369
  *
@@ -367,7 +374,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
367
374
  * @param name The name of the property
368
375
  * @returns The modified schema
369
376
  */
370
- int32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 32>>;
377
+ int32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 32, number>>;
371
378
  /**
372
379
  * Adds a 32-bit unsigned integer property to the schema.
373
380
  *
@@ -378,7 +385,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
378
385
  * @param name The name of the property
379
386
  * @returns The modified schema
380
387
  */
381
- uint32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 32>>;
388
+ uint32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 32, number>>;
382
389
  /**
383
390
  * Adds a 64-bit integer property to the schema.
384
391
  *
@@ -391,7 +398,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
391
398
  * @param name The name of the property
392
399
  * @returns The modified schema
393
400
  */
394
- int64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 64>>;
401
+ int64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 64, number>>;
395
402
  /**
396
403
  * Adds a 64-bit unsigned integer property to the schema.
397
404
  *
@@ -404,7 +411,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
404
411
  * @param name The name of the property
405
412
  * @returns The modified schema
406
413
  */
407
- uint64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 64>>;
414
+ uint64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 64, number>>;
408
415
  /**
409
416
  * Adds a 32-bit big integer property to the schema.
410
417
  *
@@ -415,7 +422,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
415
422
  * @param name The name of the property
416
423
  * @returns The modified schema
417
424
  */
418
- bigInt32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 32>>;
425
+ bigInt32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 32, bigint>>;
419
426
  /**
420
427
  * Adds a 32-bit big integer property to the schema.
421
428
  *
@@ -426,7 +433,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
426
433
  * @param name The name of the property
427
434
  * @returns The modified schema
428
435
  */
429
- bigUint32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 32>>;
436
+ bigUint32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 32, bigint>>;
430
437
  /**
431
438
  * Adds a 64-bit big integer property to the schema.
432
439
  *
@@ -437,7 +444,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
437
444
  * @param name The name of the property
438
445
  * @returns The modified schema
439
446
  */
440
- bigInt64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 64>>;
447
+ bigInt64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 64, bigint>>;
441
448
  /**
442
449
  * Adds a 64-bit big integer property to the schema.
443
450
  *
@@ -448,7 +455,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
448
455
  * @param name The name of the property
449
456
  * @returns The modified schema
450
457
  */
451
- bigUint64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 64>>;
458
+ bigUint64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 64, bigint>>;
452
459
  /**
453
460
  * Adds a 32-bit floating point number property to the schema.
454
461
  *
@@ -459,7 +466,7 @@ declare class Schema<Id extends number = number, Entries extends object = object
459
466
  * @param name The name of the property
460
467
  * @returns The modified schema
461
468
  */
462
- float32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 32>>;
469
+ float32<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 32, number>>;
463
470
  /**
464
471
  * Adds a 64-bit floating point number property to the schema.
465
472
  *
@@ -470,7 +477,15 @@ declare class Schema<Id extends number = number, Entries extends object = object
470
477
  * @param name The name of the property
471
478
  * @returns The modified schema
472
479
  */
473
- float64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 64>>;
480
+ float64<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<number, 64, number>>;
481
+ /**
482
+ * Adds a nullable property to the schema.
483
+ *
484
+ * @param name The name of the property
485
+ * @param type The type of the underlying value
486
+ * @returns The modified schema
487
+ */
488
+ nullable<const Name extends string, const ValueType, const ValueBitSize extends number | null>(name: Name, type: IType<ValueType, ValueBitSize>): Merge$1<Id, Entries, Name, IType<ValueType | null, null, ValueType | null | undefined>>;
474
489
  /**
475
490
  * Adds a 64-bit big integer property to the schema, similar to {@link Schema.bigUint64}.
476
491
  *
@@ -481,7 +496,16 @@ declare class Schema<Id extends number = number, Entries extends object = object
481
496
  * @param name The name of the property
482
497
  * @returns The modified schema
483
498
  */
484
- snowflake<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 64>>;
499
+ snowflake<const Name extends string>(name: Name): Merge$1<Id, Entries, Name, IType<bigint, 64, string | bigint>>;
500
+ /**
501
+ * Adds a constant value in the schema, this will **not** be serialized and
502
+ * can be used to add extra data without making the payload bigger.
503
+ *
504
+ * @param name The name of the property
505
+ * @param constantValue The value to add to the schema
506
+ * @returns The modified schema
507
+ */
508
+ constant<const Name extends string, const ValueType>(name: Name, constantValue: ValueType): Merge$1<Id, Entries, Name, IType<ValueType, 0, never>>;
485
509
  /**
486
510
  * Iterates over the schema's property names.
487
511
  *
@@ -517,11 +541,19 @@ type ValueOfSchema<SchemaValue extends object> = SchemaValue extends Schema<infe
517
541
  type EntryOfSchema<SchemaValue extends object> = SchemaValue extends Schema<infer _, infer Type> ? {
518
542
  [K in keyof Type]: readonly [K, Type[K]];
519
543
  }[keyof Type] : never;
520
- type UnwrapSchemaType<Type extends object> = Type extends IType<infer T, infer _> ? T : never;
544
+ type UnwrapSchemaType<Type extends object> = Type extends IType<infer ValueType, infer _BitSize, infer _InputType> ? ValueType : never;
521
545
  type UnwrapSchemaEntries<Entries extends object> = {
522
546
  [K in keyof Entries]: UnwrapSchemaType<Entries[K] & object>;
523
547
  } & object;
524
- type UnwrapSchema<SchemaValue extends object> = SchemaValue extends Schema<infer _, infer Type> ? UnwrapSchemaEntries<Type> : never;
548
+ type UnwrapSchema<SchemaValue extends object> = SchemaValue extends Schema<infer _Id, infer Type> ? UnwrapSchemaEntries<Type> : never;
549
+ type OmitNever<T> = {
550
+ [K in keyof T as T[K] extends never ? never : K]: T[K];
551
+ };
552
+ type SerializeValueType<Type extends object> = Type extends IType<infer _ValueType, infer _BitSize, infer InputType> ? InputType : never;
553
+ type SerializeValueEntries<Entries extends object> = OmitNever<{
554
+ [K in keyof Entries]: SerializeValueType<Entries[K] & object>;
555
+ }>;
556
+ type SerializeValue<SchemaValue extends object> = SchemaValue extends Schema<infer _Id, infer Type> ? SerializeValueEntries<Type> : never;
525
557
 
526
558
  declare class SchemaStore<Entries extends object = object> {
527
559
  #private;
@@ -564,7 +596,7 @@ declare class SchemaStore<Entries extends object = object> {
564
596
  * @param value The value to serialize
565
597
  * @returns The serialized buffer
566
598
  */
567
- serialize<const Id extends KeyOfStore<this>>(id: Id, value: Readonly<UnwrapSchema<Entries[Id] & object>>): UnalignedUint16Array;
599
+ serialize<const Id extends KeyOfStore<this>>(id: Id, value: SerializeValue<Entries[Id] & object>): UnalignedUint16Array;
568
600
  /**
569
601
  * Deserializes a buffer
570
602
  *
@@ -639,4 +671,4 @@ declare function toUTF16(buffer: TypedArray): string;
639
671
  */
640
672
  declare function fromUTF16(buffer: string): Uint16Array;
641
673
 
642
- export { ArrayType, BigInt32Type, BigInt64Type, BigUint32Type, BigUint64Type, BitType, BooleanType, type DeserializationResult, type EntryOfSchema, type EntryOfStore, FixedLengthArrayType, Float32Type, Float64Type, type IType, Int16Type, Int2Type, Int32Type, Int4Type, Int64Type, Int8Type, type KeyOfSchema, type KeyOfStore, Pointer, type PointerLike, Schema, SchemaStore, SnowflakeType, StringType, type TypedArray, Uint16Type, Uint2Type, Uint32Type, Uint4Type, Uint64Type, Uint8Type, UnalignedUint16Array, type UnwrapSchema, type UnwrapSchemaEntries, type UnwrapSchemaType, type ValueOfSchema, type ValueOfStore, fromUTF16, t, toUTF16 };
674
+ export { ArrayType, BigInt32Type, BigInt64Type, BigUint32Type, BigUint64Type, BitType, BooleanType, ConstantType, type DeserializationResult, type EntryOfSchema, type EntryOfStore, FixedLengthArrayType, Float32Type, Float64Type, type IType, Int16Type, Int2Type, Int32Type, Int4Type, Int64Type, Int8Type, type KeyOfSchema, type KeyOfStore, NullableType, Pointer, type PointerLike, Schema, SchemaStore, type SerializeValue, type SerializeValueEntries, type SerializeValueType, SnowflakeType, StringType, type TypedArray, Uint16Type, Uint2Type, Uint32Type, Uint4Type, Uint64Type, Uint8Type, UnalignedUint16Array, type UnwrapSchema, type UnwrapSchemaEntries, type UnwrapSchemaType, type ValueOfSchema, type ValueOfStore, fromUTF16, t, toUTF16 };