You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -220,11 +233,8 @@ When using `Shrink.bytes(...)`, the input is evaluated with multiple algorithms:
220
233
-**Identity** (no compression)
221
234
Useful when compression would increase the data size.
222
235
223
-
-**ZLIB** (levels 1–9)
224
-
Fast and widely supported, good for structured data.
225
-
226
-
-**GZIP** (levels 1–9)
227
-
Slightly larger output, but better for HTTP-compatible scenarios.
236
+
-**ZLIB** (optimized level between 4–9)
237
+
Fast, compact, and widely supported — ideal for structured or repetitive data.
228
238
229
239
The smallest result is selected automatically.
230
240
The **first byte** of the compressed output encodes the method used, so `Restore.bytes(...)` can safely reverse the process.
@@ -300,6 +310,8 @@ You can rely on `shrink` in **production environments** such as:
300
310
- Network transmission over low bandwidth
301
311
- Size-optimized APIs or backups
302
312
313
+
Performance and compression algorithms continue to improve with each release — but all `1.x.x` versions of `shrink` maintain **full backward compatibility**. You’ll never need to re-compress or migrate your existing data — it will always restore correctly, regardless of how the internals evolve.
314
+
303
315
# 🔥 Firebase Integration Example
304
316
305
317
Storing large lists (like inventory, user items, or flags) in Firestore can get expensive — especially when using arrays of integers. With `shrink`, you can compress the list into a tiny `Blob` field, saving both **space** and **money**, while preserving full data integrity.
@@ -350,6 +362,180 @@ final restoredItems = compressed.restoreUnique(); // or Restore.unique(compresse
350
362
351
363
> ℹ️ _Tip: You can use this approach for storing compressed JSON, logs, flags, or anything serializable into a list or string._
This guide walks you through installing and using shrink — a lightweight and powerful tool to compress your data and save space with just one line of code.
368
+
369
+
You can shrink text, JSON, byte data, or lists of IDs. It’s great for reducing payloads, speeding up storage, and minimizing Firebase costs.
370
+
371
+
---
372
+
373
+
### ✅ Step 1: Add `shrink` to Your Project
374
+
375
+
#### 🔧 Option A: Use a command (easy & automatic)
376
+
377
+
In your terminal, run one of these:
378
+
379
+
```bash
380
+
flutter pub add shrink # if you're using Flutter
381
+
```
382
+
383
+
or
384
+
385
+
```bash
386
+
dart pub add shrink # if you're using Dart only (no Flutter)
387
+
```
388
+
389
+
#### 📄 Option B: Edit `pubspec.yaml` manually
390
+
391
+
Open your `pubspec.yaml` file and add:
392
+
393
+
```yaml
394
+
dependencies:
395
+
shrink: ^latest
396
+
```
397
+
398
+
Then run:
399
+
400
+
```bash
401
+
flutter pub get # for Flutter
402
+
```
403
+
404
+
or
405
+
406
+
```bash
407
+
dart pub get # for Dart only
408
+
```
409
+
410
+
That’s it! You’ve added `shrink` to your project.
411
+
Now you're ready to compress and restore data with just a few lines of code.
412
+
413
+
---
414
+
415
+
### ✅ Step 2: Import the Package
416
+
417
+
In your Dart/Flutter file:
418
+
419
+
```dart
420
+
import 'package:shrink/shrink.dart';
421
+
```
422
+
423
+
This gives you access to all the `.shrink()` and `.restoreX()` functions.
424
+
425
+
---
426
+
427
+
### ✅ Step 3: Shrink Different Types of Data
428
+
429
+
You can shrink 4 types of data.
430
+
Each type has its own example — use the one that matches what you want to compress.
431
+
432
+
#### 📦 Example 1: Shrink a String (like a message, log, or description)
433
+
434
+
```dart
435
+
final compressed = 'Hello world! This is a long message.'.shrink();
436
+
437
+
// Later, to get it back:
438
+
final restored = compressed.restoreText();
439
+
```
440
+
441
+
#### 🧠 Example 2: Shrink a JSON object (like user data or settings)
442
+
443
+
```dart
444
+
final compressed = {'name': 'Alice', 'age': 30}.shrink();
445
+
446
+
// Later:
447
+
final restored = compressed.restoreJson();
448
+
```
449
+
450
+
#### 🔢 Example 3: Shrink a list of IDs (like item IDs, selected indexes)
451
+
452
+
```dart
453
+
final compressed = [1, 2, 3, 5, 8, 13, 21].shrink();
454
+
455
+
// Later:
456
+
final restored = compressed.restoreUnique();
457
+
```
458
+
459
+
#### 🛠️ Example 4: Shrink custom data (by converting to bytes)
460
+
461
+
> 🔍 Prefer using .shrink() on String, JSON, or ID lists when possible. Use bytes only for data types that shrink doesn't support directly.
462
+
463
+
```dart
464
+
final custom = {'type': 'note', 'text': 'Welcome!'};
465
+
466
+
// Convert to bytes (e.g., JSON + UTF-8)
467
+
final bytes = Uint8List.fromList(utf8.encode(jsonEncode(custom)));
468
+
469
+
// Compress the bytes
470
+
final compressed = bytes.shrink();
471
+
472
+
// Later: restore the bytes
473
+
final restored = compressed.restoreBytes();
474
+
475
+
// Convert bytes back to original data
476
+
final original = jsonDecode(utf8.decode(restored));
477
+
```
478
+
479
+
> 💡 Perfect for compressing files, binary blobs, or custom data structures.
480
+
481
+
---
482
+
483
+
### ✅ Step 4: Store or Send the Compressed Data
484
+
485
+
You can now:
486
+
487
+
- Save it to a database
488
+
- Send it over a network
489
+
- Store it in memory or a file
490
+
491
+
**Example: Store it in Firestore**
492
+
493
+
```dart
494
+
await FirebaseFirestore.instance
495
+
.collection('users')
496
+
.doc('abc123')
497
+
.set({'profile_blob': compressed});
498
+
```
499
+
500
+
## ✅ Bonus: Use the Static API Instead (Optional)
501
+
502
+
If you prefer something more explicit than `.shrink()`, use:
503
+
504
+
```dart
505
+
final compressedText = Shrink.text('Hello');
506
+
final compressedJson = Shrink.json({'a': 1});
507
+
final compressedIDs = Shrink.unique([10, 20, 30]);
508
+
final compressedData = Shrink.bytes(Uint8List.fromList([1, 2, 3]));
509
+
```
510
+
511
+
And to restore:
512
+
513
+
```dart
514
+
final original = Restore.text(compressedText);
515
+
```
516
+
517
+
## 🧪 Complete Example
518
+
519
+
```dart
520
+
import 'package:shrink/shrink.dart';
521
+
522
+
void main() {
523
+
final user = {'id': 1, 'name': 'Bob', 'age': 42};
524
+
525
+
final compressed = user.shrink(); // Compress
526
+
final restored = compressed.restoreJson(); // Restore
- ✅ Shrinking is automatic — it picks the best method for your data.
535
+
- ✅ Shrink is **lossless** — you always get the original data back.
536
+
- ✅ If the data can't be compressed, it just returns it as-is (no size increase).
537
+
- ❗ Make sure you use the right `.restoreX()` based on what you compressed.
538
+
353
539
# 🚀 Roadmap & Future Plans
354
540
355
541
The `shrink` package is actively maintained and will continue to evolve with new features, performance optimizations, and developer-friendly tools. Here's what’s coming next:
0 commit comments