UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

41 lines (40 loc) 1.65 kB
import { getObjectTypedKeys } from '@augment-vir/core'; /** * Gets an object's entries. This is the same as * [`Object.entries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) * except that it has better TypeScript types. * * @category Object * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function getObjectTypedEntries(input) { return getObjectTypedKeys(input).map((key) => [ key, input[key], ]); } /** * Create an object from an array of entries. This is the same as * [`Object.fromEntries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries) * except that it has better TypeScript types. * * @category Object * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function typedObjectFromEntries(entries) { return Object.fromEntries(entries); } /** * Gets an object's entries and sorts them by their key values. This is the same as * [`Object.entries`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) * except that it has better TypeScript types and sorts the entries. * * @category Object * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export function getEntriesSortedByKey(input) { return getObjectTypedEntries(input).sort((tupleA, tupleB) => String(tupleA[0]).localeCompare(String(tupleB[0]))); }