Skip to content

Instantly share code, notes, and snippets.

@cizordj
Created December 5, 2023 19:53
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 cizordj/897f5b53f485ef75dfb7bc7136945861 to your computer and use it in GitHub Desktop.
Save cizordj/897f5b53f485ef75dfb7bc7136945861 to your computer and use it in GitHub Desktop.
A <non-generic & strictly-typed> collection of numbers in pure Javascript
class NumberCollection {
#numbers;
constructor(numbers) {
if (
!Array.isArray(numbers) ||
!strings.every((str) => typeof str === "number")
) {
throw new Error(
"NumberCollection should be initialized with an array of numbers"
);
}
this.#numbers = numbers;
}
[Symbol.iterator]() {
let index = 0;
let numbers = this.#numbers;
return {
next: function () {
if (index < strings.length) {
return { value: numbers[index++], done: false };
} else {
return { done: true };
}
},
};
}
toJSON() {
return this.#numbers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment