Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

darty 2.0.0 #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ jobs:
image: google/dart:latest
steps:
- uses: actions/checkout@v2
- run: pub get
- run: pub run test
- run: dart pub get
- run: dart format . --set-exit-if-changed
- run: dart analyze
- run: dart test
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# [2.0.0]

- **BREAKING:** Custom alphabet
- Use `Nanoid(alphabet: a2z).generate(10)` instead of `customAlphabet(a2z, 10)`
- **BREAKING:** Nonsecure
- Use `Nanoid(random: Random()).generate(10)` instead of importing `nanoid/non_secure.dart` and using `nanoid(10)`
- **BREAKING:** Async
- async API has been removed

# [1.0.0](https://github.com/pd4d10/nanoid-dart/compare/v0.1.0...v1.0.0) (2021-03-07)

### Features
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Rongjian Zhang
Copyright (c) 2022 Rongjian Zhang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
24 changes: 7 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'package:nanoid/nanoid.dart';

void main() {
var id = nanoid();
var custom_length_id = nanoid(10);
var customLengthId = nanoid(10);
// ...
}
```
Expand All @@ -29,31 +29,21 @@ void main() {
import 'package:nanoid/nanoid.dart';

void main() {
var id = customAlphabet('1234567890abcdef', 10);
var hexNanoid = Nanoid(alphabet: '1234567890abcdef');
var id = hexNanoid(10);
// ...
}
```

### Async API

```dart
import 'package:nanoid/async.dart';

void main() async {
var id = await nanoid();
var custom_length_id = await nanoid(10);
// ...
}
```

### Non-secure API
### Non-secure

```dart
import 'package:nanoid/non_secure.dart';

void main() {
var non_secure_id = nanoid();
var custom_length_id = nanoid(10);
var nonsecureNanoid = Nanoid(random: Random());
var nonsecureId = nonsecureNanoid();
var customLengthId = nonsecureNanoid(10);
// ...
}
```
Expand Down
1 change: 1 addition & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
9 changes: 0 additions & 9 deletions example/nanoid_async_example.dart

This file was deleted.

4 changes: 2 additions & 2 deletions example/nanoid_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ void main() {
var id = nanoid();
print(id);

var custom_length_id = nanoid(10);
print(custom_length_id);
var customLengthId = nanoid(10);
print(customLengthId);
}
9 changes: 0 additions & 9 deletions example/nanoid_non_secure_example.dart

This file was deleted.

18 changes: 0 additions & 18 deletions lib/async.dart

This file was deleted.

43 changes: 31 additions & 12 deletions lib/nanoid.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
import 'dart:math';
import 'package:nanoid/src/url_alphabet.dart';
export 'package:nanoid/src/url_alphabet.dart';

final _random = Random.secure();
/// A convenience function for generating a secure nanoid
///
/// ```dart
/// final id = nanoid(5);
/// print(id); // a5g4z
/// ```
final nanoid = Nanoid();

String nanoid([int size = 21]) {
return customAlphabet(urlAlphabet, size);
}
class Nanoid {
/// The source alphabet to use for generating a nanoid
final String alphabet;

final Random _random;

/// A nanoid generator that is secure by default
Nanoid({this.alphabet = urlSafeAlphabet, Random? random})
: _random = random ?? Random.secure();

String customAlphabet(String alphabet, int size) {
final len = alphabet.length;
String id = '';
while (0 < size--) {
id += alphabet[_random.nextInt(len)];
/// Convenience caller for `nanoid()` shorthand
String call([int size = 21]) => generate(size);

/// Generate a nanoid with a given size
String generate([int size = 21]) {
final len = alphabet.length;
final buffer = StringBuffer();
while (0 < size--) {
buffer.write(alphabet[_random.nextInt(len)]);
}
return buffer.toString();
}
return id;

/// The default alphabet used for generating a nanoid. Contains [a-zA-Z0-9_-]
static const urlSafeAlphabet =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
}
18 changes: 0 additions & 18 deletions lib/non_secure.dart

This file was deleted.

2 changes: 0 additions & 2 deletions lib/src/url_alphabet.dart

This file was deleted.

3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: nanoid
version: 1.0.0
version: 2.0.0
description: A tiny, secure, URL-friendly, unique string ID generator. Dart implementation of ai/nanoid
homepage: https://github.com/pd4d10/nanoid-dart

environment:
sdk: ">=2.12.0 <3.0.0"

dev_dependencies:
lints: ^2.0.1
test: ^1.16.4
29 changes: 0 additions & 29 deletions test/custom_alphabet_test.dart

This file was deleted.

89 changes: 75 additions & 14 deletions test/nanoid_test.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,85 @@
import 'dart:math' show Random;

import 'package:test/test.dart';
import 'package:nanoid/nanoid.dart';

void main() {
test('generates URL-friendly IDs', () {
for (var i = 0; i < 10; i++) {
var id = nanoid();
expect(id.length, equals(21));
for (var j = 0; j < id.length; j++) {
expect(urlAlphabet.contains(id[j]), equals(true));
group('nanoid', () {
test('generates URL-friendly IDs', () {
for (var i = 0; i < 10; i++) {
var id = nanoid();
expect(id.length, equals(21));
for (var j = 0; j < id.length; j++) {
expect(Nanoid.urlSafeAlphabet.contains(id[j]), equals(true));
}
}
});

test('has no collisions', () {
var count = 100 * 1000;
var used = {};
for (var i = 0; i < count; i++) {
var id = nanoid();
expect(used[id], equals(null));
used[id] = true;
}
});
});

group('custom alphabet', () {
String customAlphabet(String alphabet, int length) =>
Nanoid(alphabet: alphabet).generate(length);

test('has options', () {
expect(customAlphabet('a', 5), equals('aaaaa'));
});

test('has flat distribution', () {
var count = 100 * 1000;
var length = 5;
var alphabet = 'abcdefghijklmnopqrstuvwxyz';

var chars = {};
for (var i = 0; i < count; i++) {
var id = customAlphabet(alphabet, length);
for (var j = 0; j < id.length; j++) {
var char = id[j];
if (chars[char] == null) chars[char] = 0;
chars[char] += 1;
}
}
}

chars.forEach((k, _) {
var distribution = (chars[k] * alphabet.length) / (count * length);
expect(distribution, closeTo(1, 1));
});
});
});

test('urlSafeAlphabet', () {
expect(Nanoid.urlSafeAlphabet, matches(RegExp(r'[a-zA-Z0-9_-]')));
});

test('has no collisions', () {
var count = 100 * 1000;
var used = {};
for (var i = 0; i < count; i++) {
group('readme examples', () {
test('normal', () {
var id = nanoid();
expect(used[id], equals(null));
used[id] = true;
}
expect(id.isNotEmpty, isTrue);

var customLengthId = nanoid(10);
expect(customLengthId.length, equals(10));
});

test('custom alphabet or length', () {
var hexNanoid = Nanoid(alphabet: '1234567890abcdef');
var id = hexNanoid(10);
expect(id, matches(RegExp(r'^[a-z0-9]+$')));
expect(id.length, equals(10));
});

test('non-secure', () {
var nonsecureNanoid = Nanoid(random: Random());
var id = nonsecureNanoid(10);
expect(id.length, equals(10));
});
});
}