Skip to content

Commit

Permalink
isValidOid() introduced ignoreLength
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyeh committed Nov 28, 2023
1 parent 79a7d35 commit 8801e96
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

**3.0.1**

* `isValidOid()` introduced the `ignoreLength` argument.

**3.0.0**

* `Entity.write` and `Entity.read` accept `Iterable<String>` as fields instead of `Set<String>`.
Expand Down
11 changes: 7 additions & 4 deletions lib/oid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ String nextOid() {
String mergeOid(String oid1, String oid2)
=> "${oid1.substring(0, 12)}${oid2.substring(0, 12)}";

///Test if the given value is a valid OID.
/// Test if the given value is a valid OID.
///
///Note: for performance reason, it does only the basic check.
bool isValidOid(String value)
=> value.length == oidLength && _reOid.hasMatch(value);
/// Note: for performance reason, it does only the basic check.
///
/// - [ignoreLength] whether to check `value.length` is the same as
/// [oidLength].
bool isValidOid(String value, {bool ignoreLength = false})
=> (ignoreLength || value.length == oidLength) && _reOid.hasMatch(value);

/// OID's regular expression pattern.
final oidPattern = '[-0-9a-zA-Z._~]+';
Expand Down
5 changes: 4 additions & 1 deletion test/oid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import 'package:entity/oid.dart';

void main() {
test("OID Test", () {
expect(isValidOid('_bcdefghijkl.nop-rstuvwx'), isTrue);
expect(isValidOid('_bcdefghijkl.nop-rs~uvwx'), isTrue);
expect(isValidOid('0bcdefghijklmn/pqrstuvwx'), isFalse);
expect(isValidOid('ABCDEFGHIJKLMNOPQRSTUVW;'), isFalse);
expect(isValidOid('ABCDEFGHIJKLMNOPQRSTUVW '), isFalse);
expect(isValidOid('ABCDEFGHIJKLMNOPQRST+UVW'), isFalse);

expect(isValidOid('_bcdefghijkl.nop-rs~'), isFalse);
expect(isValidOid('_bcdefghijkl.nop-rs~', ignoreLength: true), isTrue);

String? prevOid;
const loops = 100000;
final t0 = DateTime.now();
Expand Down

0 comments on commit 8801e96

Please sign in to comment.