npm.io
0.1.0 • Published 6 years ago

itobj

Licence
MIT
Version
0.1.0
Deps
0
Size
12 kB
Vulns
0
Weekly
0
Stars
81

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]

Keywords