@sapphire/string-store 1.1.0-next.7c2bed20

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/CHANGELOG.md ADDED
File without changes
package/README.md ADDED
@@ -0,0 +1,284 @@
1
+ <div align="center">
2
+
3
+ ![Sapphire Logo](https://raw.githubusercontent.com/sapphiredev/assets/main/banners/SapphireCommunity.png)
4
+
5
+ # @sapphire/string-store
6
+
7
+ **High-capacity raw data storage in UTF-16 strings.**
8
+
9
+ [![GitHub](https://img.shields.io/github/license/sapphiredev/utilities)](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
10
+ [![codecov](https://codecov.io/gh/sapphiredev/utilities/branch/main/graph/badge.svg?token=OEGIV6RFDO)](https://codecov.io/gh/sapphiredev/utilities)
11
+ [![npm bundle size](https://img.shields.io/bundlephobia/min/@sapphire/string-store?logo=webpack&style=flat-square)](https://bundlephobia.com/result?p=@sapphire/string-store)
12
+ [![npm](https://img.shields.io/npm/v/@sapphire/string-store?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@sapphire/string-store)
13
+
14
+ </div>
15
+
16
+ ## Description
17
+
18
+ A package that can store large chunks of data in a short UTF-16 string, useful for storing data in length-limited
19
+ locations such as Discord's `custom_id` field in message components.
20
+
21
+ ## Features
22
+
23
+ - Written in TypeScript
24
+ - Bundled with esbuild so it can be used in NodeJS and browsers
25
+ - Offers CommonJS, ESM and UMD bundles
26
+ - Fully tested
27
+
28
+ ## Installation
29
+
30
+ You can use the following command to install this package, or replace `npm install` with your package manager of choice.
31
+
32
+ ```sh
33
+ npm install @sapphire/string-store
34
+ ```
35
+
36
+ ## Usage
37
+
38
+ **Note**: While this section uses `require`, the imports match 1:1 with ESM imports. For example, `const { SchemaStore } = require('@sapphire/string-store')` is equivalent to `import { SchemaStore } from '@sapphire/string-store'`.
39
+
40
+ ```ts
41
+ // Require the store classes
42
+ const { SchemaStore, Schema } = require('@sapphire/string-store');
43
+
44
+ const Id = {
45
+ AgeUpdate: 0,
46
+ StrengthUpdate: 1,
47
+ Planet: 2,
48
+ User: 3
49
+ };
50
+
51
+ // Create the store
52
+ const store = new SchemaStore()
53
+ // Add a schema with an age field stored as a int32:
54
+ // Schema<Id.AgeUpdate, { age: number }>
55
+ .add(new Schema(Id.AgeUpdate).int32('age'))
56
+ // Add a schema with a strength field stored as a float32:
57
+ // Schema<Id.StrengthUpdate, { strength: number }>
58
+ .add(new Schema(Id.StrengthUpdate).float32('strength'));
59
+
60
+ // Serialize an `Id.AgeUpdate` object into a string containing:
61
+ // - The schema ID (0)
62
+ // - The age field (20)
63
+ const buffer = store.serialize(Id.AgeUpdate, { age: 20 }).toString();
64
+ ```
65
+
66
+ > [!Tip]
67
+ > The serialized string is encoded in UTF-16, meaning it can store 16 bits per character. Each type stores a different number of bits, for example, a single character can store:
68
+ > - 16 booleans
69
+ > - 8 2-bit integers (0-3)
70
+ > - 4 4-bit integers (0-15)
71
+ > - 2 8-bit integers (0-255)
72
+ > - 1 16-bit integer (0-65535)
73
+ >
74
+ > As a use-case, Discord's `custom_id` field in message components can store up to **100** UTF-16 characters, which means it has a storage of **1600 bits**, below you can see the supported types and their storage in bits. Keep in mind that the schema ID is stored as a [16-bit](#int16) integer, and that the property names are **not** stored.
75
+
76
+ The schema can be defined using the following methods:
77
+
78
+ ### `array`
79
+
80
+ Adds an array with a dynamic length to the schema.
81
+
82
+ ```ts
83
+ // A schema with a single field `names` that is an array of strings:
84
+
85
+ const schema = new Schema(Id.Planets).array('names', StringType);
86
+ // → Schema<Id.Planets, { names: string[] }>
87
+ ```
88
+
89
+ To track the length of the array, it will serialize a [16-bit](#int16) unsigned integer before the array.
90
+
91
+ ### `fixedLengthArray`
92
+
93
+ An alternative to [`array`](#array) that has a fixed length, will require the exact number of elements to be serialized,
94
+ but it will save space by not storing the length of the array.
95
+
96
+ ```ts
97
+ // A schema with a single field `names` that is an array of exactly 3 strings:
98
+
99
+ const schema = new Schema(Id.Planets).fixedLengthArray('names', StringType, 3);
100
+ // → Schema<Id.Planets, { names: [string, string, string] }>
101
+ ```
102
+
103
+ ### `string`
104
+
105
+ Adds a string to the schema.
106
+
107
+ ```ts
108
+ // A schema with a single field `name` that is a string:
109
+
110
+ const schema = new Schema(Id.Planet).string('name');
111
+ // → Schema<Id.Planets, { name: string }>
112
+ ```
113
+
114
+ The string is serialized as UTF-8, and the length is serialized as a [16-bit](#int16) unsigned integer before the string.
115
+
116
+ ### `boolean`
117
+
118
+ Adds a boolean (single bit) to the schema.
119
+
120
+ ```ts
121
+ // A schema with a single field `isHabitable` that is a boolean:
122
+
123
+ const schema = new Schema(Id.Planet).boolean('isHabitable');
124
+ // → Schema<Id.Planets, { isHabitable: boolean }>
125
+ ```
126
+
127
+ ### `bit`
128
+
129
+ Adds a bit (0 or 1) to the schema. This is a numeric version of [`boolean`](#boolean).
130
+
131
+ ```ts
132
+ // A schema with a single field `isHabitable` that is a bit:
133
+
134
+ const schema = new Schema(Id.Planet).bit('isHabitable');
135
+ // → Schema<Id.Planets, { isHabitable: 0 | 1 }>
136
+ ```
137
+
138
+ ### `int2`
139
+
140
+ Adds a 2-bit integer to the schema. It can store values from 0 to 3 (`0b11`), inclusive.
141
+
142
+ ```ts
143
+ // A schema with a single field `type` that is a 2-bit integer:
144
+
145
+ const schema = new Schema(Id.Planet).int2('type');
146
+ // → Schema<Id.Planets, { type: 0 | 1 | 2 | 3 }>
147
+ ```
148
+
149
+ ### `int4`
150
+
151
+ Adds a 4-bit integer to the schema. It can store values from 0 to 15 (`0b1111`), inclusive.
152
+
153
+ ```ts
154
+ // A schema with a single field `type` that is a 4-bit integer:
155
+
156
+ const schema = new Schema(Id.Planet).int4('type');
157
+ // → Schema<Id.Planets, { type: 0..=15 }>
158
+ ```
159
+
160
+ ### `int8`
161
+
162
+ Adds an 8-bit integer to the schema. It can store values from 0 to 255 (`0b1111_1111`), inclusive.
163
+
164
+ ```ts
165
+ // A schema with a single field `type` that is an 8-bit integer:
166
+
167
+ const schema = new Schema(Id.Planet).int8('type');
168
+ // → Schema<Id.Planets, { type: 0..=255 }>
169
+ ```
170
+
171
+ ### `int16`
172
+
173
+ Adds a 16-bit integer to the schema. It can store values from 0 to 65535 (`0xFFFF`), inclusive.
174
+
175
+ ```ts
176
+ // A schema with a single field `type` that is a 16-bit integer:
177
+
178
+ const schema = new Schema(Id.Planet).int16('type');
179
+ // → Schema<Id.Planets, { type: 0..=65535 }>
180
+ ```
181
+
182
+ ### `int32`
183
+
184
+ Adds a 32-bit integer to the schema. It can store values from 0 to 4294967295 (`0xFFFFFFFF`), inclusive.
185
+
186
+ ```ts
187
+ // A schema with a single field `type` that is a 32-bit integer:
188
+
189
+ const schema = new Schema(Id.Planet).int32('type');
190
+ // → Schema<Id.Planets, { type: 0..=4294967295 }>
191
+ ```
192
+
193
+ ### `int64`
194
+
195
+ Adds a 64-bit integer to the schema. It can store values from 0 to 9007199254740991 (`Number.MAX_SAFE_INTEGER`), inclusive.
196
+
197
+ ```ts
198
+ // A schema with a single field `type` that is a 64-bit integer:
199
+
200
+ const schema = new Schema(Id.Planet).int64('type');
201
+ // → Schema<Id.Planets, { type: 0..=9007199254740991 }>
202
+ ```
203
+
204
+ **Note**: values larger than `Number.MAX_SAFE_INTEGER` will be truncated.
205
+
206
+ ### `bigInt32`
207
+
208
+ Alternative to [`int32`](#int32) that uses `BigInt`.
209
+
210
+ ```ts
211
+ // A schema with a single field `type` that is a 32-bit integer:
212
+
213
+ const schema = new Schema(Id.Planet).bigInt32('type');
214
+ // → Schema<Id.Planets, { type: 0n..=4294967295n }>
215
+ ```
216
+
217
+ ### `bigInt64`
218
+
219
+ Alternative to [`int64`](#int64) that uses `BigInt`.
220
+
221
+ ```ts
222
+ // A schema with a single field `type` that is a 64-bit integer:
223
+
224
+ const schema = new Schema(Id.Planet).bigInt64('type');
225
+ // → Schema<Id.Planets, { type: 0n..=9007199254740991n }>
226
+ ```
227
+
228
+ ### `float32`
229
+
230
+ Adds a 32-bit floating-point number to the schema.
231
+
232
+ ```ts
233
+ // A schema with a single field `radius` that is a 32-bit floating-point number:
234
+
235
+ const schema = new Schema(Id.Planet).float32('radius');
236
+ // → Schema<Id.Planets, { radius: number }>
237
+ ```
238
+
239
+ ### `float64`
240
+
241
+ Adds a 64-bit floating-point number to the schema.
242
+
243
+ ```ts
244
+ // A schema with a single field `radius` that is a 64-bit floating-point number:
245
+
246
+ const schema = new Schema(Id.Planet).float64('radius');
247
+ // → Schema<Id.Planets, { radius: number }>
248
+ ```
249
+
250
+ ### `snowflake`
251
+
252
+ Adds a 64-bit snowflake to the schema.
253
+
254
+ ```ts
255
+ const schema = new Schema(Id.User).snowflake('id');
256
+ // → Schema<Id.User, { id: bigint }>
257
+ ```
258
+
259
+ ---
260
+
261
+ ## Buy us some doughnuts
262
+
263
+ Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
264
+
265
+ We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.
266
+
267
+ | Donate With | Address |
268
+ | :-------------: | :-------------------------------------------------: |
269
+ | Open Collective | [Click Here](https://sapphirejs.dev/opencollective) |
270
+ | Ko-fi | [Click Here](https://sapphirejs.dev/kofi) |
271
+ | Patreon | [Click Here](https://sapphirejs.dev/patreon) |
272
+ | PayPal | [Click Here](https://sapphirejs.dev/paypal) |
273
+
274
+ ## Contributors
275
+
276
+ Please make sure to read the [Contributing Guide][contributing] before making a pull request.
277
+
278
+ Thank you to all the people who already contributed to Sapphire!
279
+
280
+ <a href="https://github.com/sapphiredev/utilities/graphs/contributors">
281
+ <img src="https://contrib.rocks/image?repo=sapphiredev/utilities" />
282
+ </a>
283
+
284
+ [contributing]: https://github.com/sapphiredev/.github/blob/main/.github/CONTRIBUTING.md