Skip to content

Commit 8048122

Browse files
hackerwinsclaude
andauthored
Return unwrapped primitive values from Array iterator (#1178)
Change the default `for...of` iterator on JSONArray to return raw primitive values instead of Primitive wrappers, making it consistent with `.values()`, `.map()`, and other read-only array methods. Add `elements()` method as an alternative for accessing wrapped elements with CRDT metadata. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a69dc51 commit 8048122

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/sdk/src/document/json/array.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ export type JSONArray<T> = {
114114
*/
115115
moveLast(id: TimeTicket): void;
116116

117+
/**
118+
* `elements` returns an iterator of wrapped elements including CRDT metadata.
119+
*/
120+
elements(): IterableIterator<WrappedElement<T>>;
121+
117122
/**
118123
* `toTestString` returns a String containing the meta data of the node
119124
* for debugging purpose.
@@ -324,6 +329,8 @@ export class ArrayProxy {
324329
return target.length;
325330
} else if (typeof method === 'symbol' && method === Symbol.iterator) {
326331
return ArrayProxy.iteratorInternal.bind(this, context, target);
332+
} else if (method === 'elements') {
333+
return ArrayProxy.wrappedIteratorInternal.bind(this, context, target);
327334
} else if (method === 'includes') {
328335
return (searchElement: JSONElement, fromIndex?: number): boolean => {
329336
return ArrayProxy.includes(
@@ -398,6 +405,16 @@ export class ArrayProxy {
398405
public static *iteratorInternal(
399406
change: ChangeContext,
400407
target: CRDTArray,
408+
): IterableIterator<JSONElement> {
409+
for (const elem of target) {
410+
yield toJSONElement(change, elem)!;
411+
}
412+
}
413+
414+
// eslint-disable-next-line jsdoc/require-jsdoc
415+
public static *wrappedIteratorInternal(
416+
change: ChangeContext,
417+
target: CRDTArray,
401418
): IterableIterator<WrappedElement> {
402419
for (const elem of target) {
403420
yield toWrappedElement(change, elem)!;

0 commit comments

Comments
 (0)