@raw-js/iterable

1.0.0 • Public • Published

@raw-js/iterable

Make an object iterable implementing @@iterable protocol

import { iterable } from "@raw-js/iterable";

const obj = {}
iterable(obj) // now obj is iterable

Examples

import { iterable } from "@raw-js/iterable";

const obj = {
  a: 100,
  b: 42,
  c: "hello",
  d: "world",
  e: true,
};

/* for..of */

for (const [key, value] of iterable(obj)) {
  console.log(key + ": " + value);
}
// » a: 100,
// » b: 42,
// » c: "hello",
// » d: "world",
// » e: true,

/* destructuring */

[...iterable(obj)]
  .filter(([_, value]) => typeof value === "string")
  .map(([key]) => key);
// » Array ['c', 'd']

[...iterable(obj)].reduce(
  (acc, [_, value]) => (typeof value === "number" ? acc + value : acc),
  0
);
// » 142

/* keeping same obj */
obj === iterable(obj);
// » true

Package Sidebar

Install

npm i @raw-js/iterable

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

7.16 kB

Total Files

8

Last publish

Collaborators

  • manutero