npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

object-exact

v2.1.5

Published

Filter or partially filter object members and remove the rest, return value with accurate typing at the end, work best with typescript.

Downloads

21

Readme

object-exact

npm GitHub PRs Welcome tylim88

🐤 Filter or partially filter object members and remove the rest, return value with accurate typing at the end, work best with typescript.

🥰 0 dependency.

⛲️ Out of box typescript support.

🦺 Tested.

Installation

npm i object-exact

🦗 Functionality

This library allow you to filter or partially filter object members that you want to keep and discard the rest members, all without modifying the original object.

The logic in this code is simple, you can easily create one yourself, however the library assist you in creating accurate output type and handle input type with minimum interface as possible.

Example you have a object with 3 keys: a,b,c, where:

a : required, number b : optional, number c : discard, -

Based on this information, the library construct the target object type {a:number, b?:number}, and given the target objects {a:1, b:2, c:3} and {a:1, c:3}:

For optional key, it will never return partial type key, the type shape is alway the same as target object.

Hence output type of the first object is {a:number, b:number} and the type of the second object is {a: number}, both object will never return type {a:number, b?:number}, this allows you to process typing in more precise manner.

🎵 Usage

import { objExact } from 'object-exact'

console.log(
	objExact(
		{
			a: 'use any value here except undefined',
			b: 'use any value here except undefined',
		},
		{ a: 'hello', b: false, c: '!' }
	)
) // { a:"hello", b:"world" } and the type is { a:string, b:boolean }

object(dummy,target): require dummy object and target object, return new object with accurate typing, does not modify original objects.

dummy: the keys of this object is the keys that you want to keep, as for the values of the keys, use any value, except undefined which bear special utility(see explanation in target).

target: object that you want to remove excess members, this object must be subtype of dummy object(all keys exist in dummy object must also present in target object); unless the dummy key's value is undefined, then it tries to keep the key only if it is available and will not trigger typescript error if not available.

import { objExact } from 'object-exact'

// this will trigger typescript error: property b is missing.
// `target` must bet subtype of `dummy`
// NOT recommend for typescript developer, javascript developer is unaffected
console.log(objExact({ a: 1, b: 2 }, { a: 'hello' })) // { a:"hello", b: undefined } and the type is { a: unknown, b: unknown } <- wasted

// if you want to make a value optional
// then  let the `dummy` key's value be undefined, it will try to keep the key if it is available in `target`
console.log(objExact({ a: 1, b: undefined }, { a: 'hello', c: '!' })) // { a:"hello" } and the type is {a: string}
console.log(objExact({ a: 1, b: undefined }, { a: 'hello', b: 123, c: '!' })) // { a:"hello", b:123 } and the type is {a: string, b: number}