From 8801e963da8cea679ec77c3045e604e8b303f7c3 Mon Sep 17 00:00:00 2001 From: Tom Yeh Date: Tue, 28 Nov 2023 18:25:53 +0800 Subject: [PATCH] isValidOid() introduced ignoreLength --- CHANGELOG.md | 4 ++++ lib/oid.dart | 11 +++++++---- test/oid_test.dart | 5 ++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 832b94b..955f1c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changes +**3.0.1** + +* `isValidOid()` introduced the `ignoreLength` argument. + **3.0.0** * `Entity.write` and `Entity.read` accept `Iterable` as fields instead of `Set`. diff --git a/lib/oid.dart b/lib/oid.dart index 8173ec4..673e07d 100644 --- a/lib/oid.dart +++ b/lib/oid.dart @@ -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._~]+'; diff --git a/test/oid_test.dart b/test/oid_test.dart index 9d761a1..21dc7a4 100755 --- a/test/oid_test.dart +++ b/test/oid_test.dart @@ -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();