Skip to content

Instantly share code, notes, and snippets.

@TorbjornHoltmon
Last active February 22, 2023 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TorbjornHoltmon/82737f7d7b7bd73f43e6fe941c8e21f0 to your computer and use it in GitHub Desktop.
Save TorbjornHoltmon/82737f7d7b7bd73f43e6fe941c8e21f0 to your computer and use it in GitHub Desktop.
random
function createIsomorphicDestructible(obj, arr) {
const clone = { ...obj, map: arr.map };
Object.defineProperty(clone, Symbol.iterator, {
enumerable: false,
value() {
let index = 0;
return {
next: () => ({
value: arr[index++],
done: index > arr.length,
}),
};
},
});
for (const [index, value] of arr.entries()) {
clone[index] = value;
}
return clone;
}
function createIsomorphicDestructible<T extends Record<string, unknown>, A extends readonly any[]>(
obj: T,
arr: A
): T & A {
const clone = { ...obj };
Object.defineProperty(clone, Symbol.iterator, {
enumerable: false,
value() {
let index = 0;
return {
next: () => ({
value: arr[index++],
done: index > arr.length,
}),
};
},
});
return clone as T & A;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment