|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:js_interop'; |
| 6 | +import 'dart:js_interop_unsafe'; |
| 7 | + |
| 8 | +import 'unsafe/object.dart'; |
| 9 | + |
| 10 | +/// A JavaScript "record type", or in other words an object that's used as a |
| 11 | +/// lightweight map. |
| 12 | +/// |
| 13 | +/// This provides a map-like API and utilities for interacting with records, as |
| 14 | +/// well as a [toDart] method for converting it into a true map. It considers |
| 15 | +/// the object's keys to be its enumerable, own, string properties (following |
| 16 | +/// `Object.keys()`). |
| 17 | +/// |
| 18 | +/// In most cases, JS records only accept string keys, and this type is |
| 19 | +/// optimized to make this case easy to work with by automatically wrapping and |
| 20 | +/// unwrapping [JSString]s. However, there are cases where [JSSymbol]s are used |
| 21 | +/// as keys, in which case [JSSymbolicRecord] may be used instead. |
| 22 | +/// |
| 23 | +/// Because this is a JavaScript object it follows JavaScript ordering |
| 24 | +/// semantics. Specifically: all number-like keys come first in numeric order, |
| 25 | +/// then all string keys in insertion order. |
| 26 | +/// |
| 27 | +/// **Note:** Like Dart collections, it's not guaranteed to be safe to modify |
| 28 | +/// this while iterating over it. Unlike Dart collections, it doesn't have any |
| 29 | +/// fail-safes to throw errors if this happens. So be extra careful! |
| 30 | +extension type JSRecord<V extends JSAny?>._(JSObject _) implements JSObject { |
| 31 | + /// Returns an iterable over tuples of the `key`/`value` pairs in this record. |
| 32 | + Iterable<(String, V)> get pairs => JSObjectUnsafeExtension(this).entries.cast<(String, V)>(); |
| 33 | + |
| 34 | + /// See [Map.entries]. |
| 35 | + Iterable<MapEntry<String, V>> get entries sync* { |
| 36 | + for (var (key, value) in pairs) { |
| 37 | + yield MapEntry(key, value); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// See [Map.isEmpty]. |
| 42 | + bool get isEmpty => length == 0; |
| 43 | + |
| 44 | + /// See [Map.isNotEmpty]. |
| 45 | + bool get isNotEmpty => length != 0; |
| 46 | + |
| 47 | + /// See [Map.keys]. |
| 48 | + Iterable<String> get keys sync* { |
| 49 | + for (var key in JSObjectUnsafeExtension(this).keys) { |
| 50 | + yield key.toDart; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// See [Map.length]. |
| 55 | + int get length => keys.length; |
| 56 | + |
| 57 | + /// See [Map.values]. |
| 58 | + Iterable<V> get values => JSObjectUnsafeExtension(this).values.cast<V>(); |
| 59 | + |
| 60 | + /// Creates a new Dart map with the same contents as this record. |
| 61 | + Map<String, V> get toDart => {for (var (key, value) in pairs) key: value}; |
| 62 | + |
| 63 | + /// Creates a new, empty record. |
| 64 | + factory JSRecord() => JSRecord._(JSObject()); |
| 65 | + |
| 66 | + /// Creates a [JSRecord] with the same keys and values as [other]. |
| 67 | + static JSRecord<V> ofRecord<V extends JSAny?>(JSRecord<V> other) => |
| 68 | + JSRecord<V>()..addAllRecord(other); |
| 69 | + |
| 70 | + /// Like [Map.of], but creates a record. |
| 71 | + static JSRecord<V> ofMap<V extends JSAny?>(Map<String, V> other) => |
| 72 | + JSRecord.fromEntries<V>(other.entries); |
| 73 | + |
| 74 | + /// Like [Map.fromEntries], but creates a record. |
| 75 | + static JSRecord<V> fromEntries<V extends JSAny?>( |
| 76 | + Iterable<MapEntry<String, V>> entries, |
| 77 | + ) => JSRecord<V>()..addEntries(entries); |
| 78 | + |
| 79 | + /// Creates a new record and adds all the [pairs]. |
| 80 | + /// |
| 81 | + /// If multiple pairs have the same key, later occurrences overwrite the value |
| 82 | + /// of the earlier ones. |
| 83 | + static JSRecord<V> fromPairs<V extends JSAny?>(Iterable<(String, V)> pairs) => |
| 84 | + JSRecord<V>()..addPairs(pairs); |
| 85 | + |
| 86 | + /// See [Map.addAll]. |
| 87 | + void addAll(Map<String, V> other) => addEntries(other.entries); |
| 88 | + |
| 89 | + /// Adds all enumerable, own, string key/value pairs of [other] to this |
| 90 | + /// record. |
| 91 | + /// |
| 92 | + /// If a key of [other] is already in this record, its value is overwritten. |
| 93 | + /// |
| 94 | + /// The operation is equivalent to doing `this[key] = value` for each key and |
| 95 | + /// associated value in [other]. It iterates over [other], which must therefore |
| 96 | + /// not change during the iteration. |
| 97 | + void addAllRecord(JSRecord<V> other) => addPairs(other.pairs); |
| 98 | + |
| 99 | + /// See [Map.addEntries]. |
| 100 | + void addEntries(Iterable<MapEntry<String, V>> entries) { |
| 101 | + for (var MapEntry(key: key, value: value) in entries) { |
| 102 | + this[key] = value; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// Adds all key/value pairs of [newPairs] to this record. |
| 107 | + /// |
| 108 | + /// If a key of [newPairs] is already in this record, the corresponding value |
| 109 | + /// is overwritten. |
| 110 | + /// |
| 111 | + /// The operation is equivalent to doing `this[entry.key] = entry.value` for |
| 112 | + /// each pair of the iterable. |
| 113 | + void addPairs(Iterable<(String, V)> newPairs) { |
| 114 | + for (var (key, value) in newPairs) { |
| 115 | + this[key] = value; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /// See [Map.clear]. |
| 120 | + void clear() { |
| 121 | + for (var key in keys) { |
| 122 | + delete(key.toJS); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /// See [Map.containsKey]. |
| 127 | + bool containsKey(Object? key) => |
| 128 | + key is String && propertyIsEnumerable(key.toJS); |
| 129 | + |
| 130 | + /// See [Map.containsValue]. |
| 131 | + bool containsValue(Object? value) => values.any((actual) => actual == value); |
| 132 | + |
| 133 | + /// See [Map.forEach]. |
| 134 | + void forEach(void action(String key, V value)) { |
| 135 | + for (var (key, value) in pairs) { |
| 136 | + action(key, value); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /// See [Map.map]. |
| 141 | + Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> convert(String key, V value)) => |
| 142 | + Map.fromEntries(pairs.map((pair) => convert(pair.$1, pair.$2))); |
| 143 | + |
| 144 | + /// See [Map.putIfAbsent]. |
| 145 | + V putIfAbsent(String key, V ifAbsent()) { |
| 146 | + if (containsKey(key)) return this[key]!; |
| 147 | + var result = ifAbsent(); |
| 148 | + this[key] = result; |
| 149 | + return result; |
| 150 | + } |
| 151 | + |
| 152 | + /// See [Map.remove]. |
| 153 | + V? remove(Object? key) { |
| 154 | + if (!containsKey(key)) return null; |
| 155 | + var value = this[key]; |
| 156 | + delete((key as String).toJS); |
| 157 | + return value; |
| 158 | + } |
| 159 | + |
| 160 | + /// See [Map.removeWhere]. |
| 161 | + void removeWhere(bool test(String key, V value)) { |
| 162 | + for (var (key, value) in pairs) { |
| 163 | + if (test(key, value)) delete(key.toJS); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /// See [Map.update]. |
| 168 | + V update(String key, V update(V value), {V ifAbsent()?}) { |
| 169 | + if (containsKey(key)) { |
| 170 | + return this[key] = update(this[key]!); |
| 171 | + } else if (ifAbsent == null) { |
| 172 | + throw new ArgumentError("ifAbsent must be passed if the key is absent."); |
| 173 | + } else { |
| 174 | + return this[key] = ifAbsent(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /// See [Map.updateAll]. |
| 179 | + void updateAll(V update(String key, V value)) { |
| 180 | + for (var (key, value) in pairs) { |
| 181 | + this[key] = update(key, value); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /// See [Map.operator[]]. |
| 186 | + V? operator [](Object? key) => |
| 187 | + key is String ? getProperty(key.toJS) as V? : null; |
| 188 | + |
| 189 | + /// See [Map.operator[]=]. |
| 190 | + void operator []=(String key, V value) => setProperty(key.toJS, value); |
| 191 | +} |
0 commit comments