diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp index be3d860e750..42745cec306 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp @@ -1448,4 +1448,14 @@ WebIDL::ExceptionOr> convert_a_value_to_a_key_range(JS::Rea return IDBKeyRange::create(realm, key, key, false, false); } +// https://w3c.github.io/IndexedDB/#count-the-records-in-a-range +JS::Value count_the_records_in_a_range(GC::Ref source, GC::Ref range) +{ + // 1. Let count be the number of records, if any, in source’s list of records with key in range. + auto count = source->count_records_in_range(range); + + // 2. Return count. + return JS::Value(count); +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h index 2bbb124fe65..12be59ff772 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h +++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.h @@ -42,5 +42,6 @@ void inject_a_key_into_a_value_using_a_key_path(JS::Realm&, JS::Value, GC::Ref, GC::Ref); WebIDL::ExceptionOr> store_a_record_into_an_object_store(JS::Realm&, GC::Ref, JS::Value, GC::Ptr, bool); WebIDL::ExceptionOr> convert_a_value_to_a_key_range(JS::Realm&, Optional, bool = false); +JS::Value count_the_records_in_a_range(GC::Ref, GC::Ref); } diff --git a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp index 79825d59f28..20d54b67d38 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp +++ b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.cpp @@ -67,4 +67,14 @@ void ObjectStore::store_a_record(Record const& record) }); } +u64 ObjectStore::count_records_in_range(GC::Ref range) +{ + u64 count = 0; + for (auto const& record : m_records) { + if (range->is_in_range(record.key)) + ++count; + } + return count; +} + } diff --git a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h index b8277a239eb..aab58c3d4f5 100644 --- a/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h +++ b/Libraries/LibWeb/IndexedDB/Internal/ObjectStore.h @@ -52,6 +52,7 @@ public: void remove_records_in_range(GC::Ref range); bool has_record_with_key(GC::Ref key); void store_a_record(Record const& record); + u64 count_records_in_range(GC::Ref range); protected: virtual void visit_edges(Visitor&) override;