swift_ids 1.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 56

swift_ids #

Convert number IDs to human friendly string and back.

integerswift_id
0GX08
1HY9U
2JA2Q
5D9YU
1000SF7B
10000000GM1Y3Q

The generated ID is:

  • fast to convert back and forth (algorithm uses mostly binary operations)
  • seems random (end user can't browse trough entities easily)
  • user can't tell how many entities there are
  • shorter than original int (5 characters can save 8388608 ids)
  • easy to write down / spell over the phone because generated string uses:
    • only uppercase letters,
    • no confusing digits: O=0 and I=L=1
    • no two consecutive characters are the same
  • avoid accidental profanities (and words in general)
  • minimum length is configurable

usage #

var encode = new Id(5);
print("${encode.value} == ${encode.toString()}"); //5 == D9YU
var decode = new Id.fromString('D9YU');
print("${decode.value} == ${decode.toString()}"); //4 == C84X

var encode2 = new Id(5, minLength:10, zero:0x55555555555);
print("${encode2.value} == ${encode2.toString()}"); //5 == HBQ8H7E85B
//same zero/minLength need to be used when decoding
var decode2 = new Id.fromString('HBQ8H7E85B', minLength:10, zero:0x55555555555);
print("${decode2.value} == ${decode2.toString()}"); //4 == C84X

zero is an initial value that is XORed with input that can be used to:

  • make separate entities ids not overlap (so user with id=5 won't have same hash as message with id=5)
  • mangle initial integer to prevent repetitions for small integers with large minLength
  • zero should have maximum (5 * minLength) - (2 * (minLength / 3)) bits.

1.0.0 #

initial release

1.1.0 #

typos handling, docs update

example/swift_ids.dart

#!/usr/bin/env dart

import 'package:swift_ids/swift_ids.dart';

void main (List<String> arguments) {
  var encode = new Id(5);
  print("${encode.value} == ${encode.toString()}"); //5 == D9YU
  var decode = new Id.fromString('D9YU');
  print("${decode.value} == ${decode.toString()}"); //4 == C84X

  var encode2 = new Id(5, minLength:10, zero:0x55555555555);
  print("${encode2.value} == ${encode2.toString()}"); //5 == HBQ8H7E85B
  //same zero/minLength need to be used when decoding
  var decode2 = new Id.fromString('HBQ8H7E85B', minLength:10, zero:0x55555555555);
  print("${decode2.value} == ${decode2.toString()}"); //4 == C84X
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  swift_ids: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:swift_ids/swift_ids.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
14
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
56
Learn more about scoring.

We analyzed this package on Oct 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:swift_ids/swift_ids.dart.

Health suggestions

Fix lib/swift_ids.dart. (-2.48 points)

Analysis of lib/swift_ids.dart reported 5 hints:

line 47 col 1: Prefer using /// for doc comments.

line 56 col 1: Prefer using /// for doc comments.

line 65 col 3: Prefer using /// for doc comments.

line 70 col 3: Prefer using /// for doc comments.

line 159 col 3: Prefer using /// for doc comments.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
hashids2 any
test any