0.1.0 • Published 4 years ago

itobj v0.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

itobj npm

Iterate Object.

⚠️ Ignores any keys but strings in types and skips Symbols and stringifies the rest of the keys in runtime just like Object methods do.

Install

$ yarn add itobj

Usage

type TStringKey<T> = keyof T & string

const iterateObjectKeys: <T extends {}>(obj: T) => Iterable<TStringKey<T>>
const iterateObjectValues: <T extends {}>(obj: T) => Iterable<T[TStringKey<T>]>
const iterateObjectEntries: <T extends {}>(obj: T) => Iterable<[TStringKey<T>, T[TStringKey<T>]]>
import { iterateObjectKeys, iterateObjectValues, iterateObjectEntries } from 'itobj'

const obj = { a: 1, b: 2, c: 3 }

for (const key of iterateObjectKeys(obj)) {
  console.log(key)
}
// 'a'
// 'b'
// 'c'

for (const value of iterateObjectValues(obj)) {
  console.log(value)
}
// 1
// 2
// 3

for (const entry of iterateObjectEntries(obj)) {
  console.log(entry)
}
// ['a', 1]
// ['b', 2]
// ['c', 3]