Skip to content

Commit

Permalink
feat: matches the latest api
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed Mar 7, 2021
1 parent 83688e7 commit 0ba2674
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ jobs:
container:
image: google/dart:latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- run: pub get
- run: pub run test
26 changes: 4 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ void main() {
### Custom Alphabet or length

```dart
import 'package:nanoid/generate.dart';
import 'package:nanoid/nanoid.dart';
void main() {
var id = generate('1234567890abcdef', 10);
var id = customAlphabet('1234567890abcdef', 10);
// ...
}
```

### Async API

```dart
import 'package:nanoid/async/nanoid.dart';
import 'package:nanoid/async.dart';
void main() async {
var id = await nanoid();
Expand All @@ -46,19 +46,10 @@ void main() async {
}
```

```dart
import 'package:nanoid/async/generate.dart';
void main() async {
var id = await generate('1234567890abcdef', 10);
// ...
}
```

### Non-secure API

```dart
import 'package:nanoid/non_secure/nanoid.dart';
import 'package:nanoid/non_secure.dart';
void main() {
var non_secure_id = nanoid();
Expand All @@ -67,15 +58,6 @@ void main() {
}
```

```dart
import 'package:nanoid/non_secure/generate.dart';
void main() {
var non_secure_id = generate('1234567890abcdef', 10);
// ...
}
```

## License

MIT
2 changes: 1 addition & 1 deletion example/nanoid_async_example.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:nanoid/async/nanoid.dart';
import 'package:nanoid/async.dart';

void main() async {
var id = await nanoid();
Expand Down
2 changes: 1 addition & 1 deletion example/nanoid_non_secure_example.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:nanoid/non_secure/nanoid.dart';
import 'package:nanoid/non_secure.dart';

void main() {
var non_secure_id = nanoid();
Expand Down
18 changes: 18 additions & 0 deletions lib/async.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:math';
import 'package:nanoid/src/url_alphabet.dart';
export 'package:nanoid/src/url_alphabet.dart';

final _random = Random.secure();

Future<String> nanoid([int size = 21]) async {
return customAlphabet(urlAlphabet, size);
}

Future<String> customAlphabet(String alphabet, int size) async {
final len = alphabet.length;
String id = '';
while (0 < size--) {
id += alphabet[_random.nextInt(len)];
}
return id;
}
13 changes: 0 additions & 13 deletions lib/async/generate.dart

This file was deleted.

7 changes: 0 additions & 7 deletions lib/async/nanoid.dart

This file was deleted.

12 changes: 0 additions & 12 deletions lib/generate.dart

This file was deleted.

18 changes: 15 additions & 3 deletions lib/nanoid.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import 'generate.dart';
import 'url.dart';
import 'dart:math';
import 'package:nanoid/src/url_alphabet.dart';
export 'package:nanoid/src/url_alphabet.dart';

final _random = Random.secure();

String nanoid([int size = 21]) {
return generate(url, size);
return customAlphabet(urlAlphabet, size);
}

String customAlphabet(String alphabet, int size) {
final len = alphabet.length;
String id = '';
while (0 < size--) {
id += alphabet[_random.nextInt(len)];
}
return id;
}
18 changes: 18 additions & 0 deletions lib/non_secure.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:math';
import 'package:nanoid/src/url_alphabet.dart';
export 'package:nanoid/src/url_alphabet.dart';

final _random = Random();

String nanoid([int size = 21]) {
return customAlphabet(urlAlphabet, size);
}

String customAlphabet(String alphabet, int size) {
final len = alphabet.length;
String id = '';
while (0 < size--) {
id += alphabet[_random.nextInt(len)];
}
return id;
}
12 changes: 0 additions & 12 deletions lib/non_secure/generate.dart

This file was deleted.

6 changes: 0 additions & 6 deletions lib/non_secure/nanoid.dart

This file was deleted.

2 changes: 2 additions & 0 deletions lib/src/url_alphabet.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const urlAlphabet =
'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';
1 change: 0 additions & 1 deletion lib/url.dart

This file was deleted.

3 changes: 1 addition & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
name: nanoid
version: 0.2.0
author: Rongjian Zhang <pd4d10@gmail.com>
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-0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dev_dependencies:
test: ^1.16.4
6 changes: 3 additions & 3 deletions test/generate_test.dart → test/custom_alphabet_test.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:test/test.dart';
import 'package:nanoid/generate.dart';
import 'package:nanoid/nanoid.dart';

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

test('has flat distribution', () {
Expand All @@ -13,7 +13,7 @@ void main() {

var chars = {};
for (var i = 0; i < count; i++) {
var id = generate(alphabet, length);
var id = customAlphabet(alphabet, length);
for (var j = 0; j < id.length; j++) {
var char = id[j];
if (chars[char] == null) chars[char] = 0;
Expand Down
3 changes: 1 addition & 2 deletions test/nanoid_test.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import 'package:test/test.dart';
import 'package:nanoid/nanoid.dart';
import 'package:nanoid/url.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(url.contains(id[j]), equals(true));
expect(urlAlphabet.contains(id[j]), equals(true));
}
}
});
Expand Down

0 comments on commit 0ba2674

Please sign in to comment.