Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #38 from dart-lang/devoncarew_uuid
Browse files Browse the repository at this point in the history
replace the uuid package with our own implementation
  • Loading branch information
devoncarew committed Dec 21, 2014
2 parents 7d20476 + f51cb35 commit 9ef08d8
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lib/src/usage_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ library usage_impl;
import 'dart:async';
import 'dart:math' as math;

import 'package:uuid/uuid.dart';

import 'uuid.dart';
import '../usage.dart';

final int _MAX_EXCEPTION_LENGTH = 100;
Expand Down Expand Up @@ -154,7 +153,7 @@ abstract class AnalyticsImpl implements Analytics {

void _initClientId() {
if (_clientId == null) {
properties['clientId'] = new Uuid().v4();
properties['clientId'] = new Uuid().generateV4();
}
}

Expand Down
48 changes: 48 additions & 0 deletions lib/src/uuid.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/**
* A UUID generator library.
*/
library usage.uuid;

import 'dart:math' show Random;

/**
* A UUID generator. This will generate unique IDs in the format:
*
* f47ac10b-58cc-4372-a567-0e02b2c3d479
*
* The generated uuids are 128 bit numbers encoded in a specific string format.
*
* For more information, see
* http://en.wikipedia.org/wiki/Universally_unique_identifier.
*/
class Uuid {
Random _random = new Random();

/**
* Generate a version 4 (random) uuid. This is a uuid scheme that only uses
* random numbers as the source of the generated uuid.
*/
String generateV4() {
// Generate xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx / 8-4-4-4-12.
int special = 8 + _random.nextInt(4);

return
'${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}-'
'${_bitsDigits(16, 4)}-'
'4${_bitsDigits(12, 3)}-'
'${_printDigits(special, 1)}${_bitsDigits(12, 3)}-'
'${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}';
}

String _bitsDigits(int bitCount, int digitCount) =>
_printDigits(_generateBits(bitCount), digitCount);

int _generateBits(int bitCount) => _random.nextInt(1 << bitCount);

String _printDigits(int value, int count) =>
value.toRadixString(16).padLeft(count, '0');
}
3 changes: 0 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ description: A Google Analytics wrapper for both command-line and web apps.
homepage: https://github.com/dart-lang/usage
author: Dart Team <misc@dartlang.org>

dependencies:
uuid: '>=0.4.0 <0.5.0'

dev_dependencies:
browser: any
grinder: any
Expand Down
2 changes: 2 additions & 0 deletions test/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import 'hit_types_test.dart' as hit_types_test;
import 'usage_test.dart' as usage_test;
import 'usage_impl_test.dart' as usage_impl_test;
import 'usage_impl_io_test.dart' as usage_impl_io_test;
import 'uuid_test.dart' as uuid_test;

void main() {
hit_types_test.defineTests();
usage_test.defineTests();
usage_impl_test.defineTests();
usage_impl_io_test.defineTests();
uuid_test.defineTests();
}
71 changes: 71 additions & 0 deletions test/uuid_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

library usage.uuid_test;

import 'package:unittest/unittest.dart';
import 'package:usage/src/uuid.dart';

void defineTests() {
group('uuid', () {
// xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
test('simple', () {
Uuid uuid = new Uuid();
String result = uuid.generateV4();
expect(result.length, 36);
expect(result[8], '-');
expect(result[13], '-');
expect(result[18], '-');
expect(result[23], '-');
});

test('can parse', () {
Uuid uuid = new Uuid();
String result = uuid.generateV4();
expect(int.parse(result.substring(0, 8), radix: 16), isNotNull);
expect(int.parse(result.substring(9, 13), radix: 16), isNotNull);
expect(int.parse(result.substring(14, 18), radix: 16), isNotNull);
expect(int.parse(result.substring(19, 23), radix: 16), isNotNull);
expect(int.parse(result.substring(24, 36), radix: 16), isNotNull);
});

test('special bits', () {
Uuid uuid = new Uuid();
String result = uuid.generateV4();
expect(result[14], '4');
expect(result[19].toLowerCase(), isIn('89ab'));

result = uuid.generateV4();
expect(result[19].toLowerCase(), isIn('89ab'));

result = uuid.generateV4();
expect(result[19].toLowerCase(), isIn('89ab'));
});

test('is pretty random', () {
Set set = new Set();

Uuid uuid = new Uuid();
for (int i = 0; i < 64; i++) {
String val = uuid.generateV4();
expect(set, isNot(contains(val)));
set.add(val);
}

uuid = new Uuid();
for (int i = 0; i < 64; i++) {
String val = uuid.generateV4();
expect(set, isNot(contains(val)));
set.add(val);
}

uuid = new Uuid();
for (int i = 0; i < 64; i++) {
String val = uuid.generateV4();
expect(set, isNot(contains(val)));
set.add(val);
}
});
});
}
2 changes: 2 additions & 0 deletions test/web_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:unittest/unittest.dart';
import 'hit_types_test.dart' as hit_types_test;
import 'usage_test.dart' as usage_test;
import 'usage_impl_test.dart' as usage_impl_test;
import 'uuid_test.dart' as uuid_test;

// TODO: get the tests running in content_shell

Expand All @@ -24,6 +25,7 @@ void main() {
hit_types_test.defineTests();
usage_test.defineTests();
usage_impl_test.defineTests();
uuid_test.defineTests();

// Define some web specfic tests.
defineWebTests();
Expand Down

0 comments on commit 9ef08d8

Please sign in to comment.