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

@merry-solutions/typed-empty-fields-filter

v1.0.0

Published

This package provides functions for filtering arrays of objects based on empty values for given fields, returning arrays of modified types. Go check readme for more information.

Downloads

1

Readme

typed-empty-fields-filter

What is it?

A simple package for filtering arrays of objects, leaving out items based on null or undefined in given fields, while at the same time providing propert typing for the resulted items.

The package includes 3 functions

  • filterNullFields for filtering fields with null values;
  • filterUndefinedFields for filtering fields with undefined values;
  • filterEmptyFields for filtering both null and undefined values at the same time.

For example

Provided you have an interface with a field or fields which can be undefined

interface Point {
    x?: number;
    y?: number;
}

Given an array of such items, you can filter them out and have the proper typing for a returned items in the new array:

const points: Array<Point> = [{x: 2}, { y: 2 }, {x: 1, y: 1}];

// result array type will be Array<{x?: number; y: number}>
const filteredWithYRequired = filterEmptyFields(points, 'y');

// resulting interface has y as 'number', not 'number | undefined' union as before
filteredWithYRequired[0]?.y;

or filter based on a set of fields:

const points: Array<Point> = [{x: 2}, { y: 2 }, {x: 1, y: 1}];

// result array type will be Array<{x: number; y: number}>
const filteredWithYRequired = filterEmptyFields(points, ['x', 'y']);

Installation

npm i --save @merry-solutions/typed-empty-fields-filter

Import

import {filterEmptyFields, filterNullFields, filterUndefinedFields} from '@merry-solutions/typed-empty-fields-filter';

Usage

Pick one of the three function and pass an array and a filed/list of fields for filtering. Intellisense will help you out.


import {filterEmptyFields, filterNullFields, filterUndefinedFields} from '@merry-solutions/typed-empty-fields-filter';

export interface User {
  id: number;
  surname: string;
  name?: string;
  middleName: string | undefined;
  phone?: string | null;
  hobby?: string | null;
}

const USERS: Array<User> = [
  {
    id: 1,
    surname: 'Tann',
    middleName: 'XtraCare Wet Wipes',
    phone: '750-476-2058',
    hobby: 'transform one-to-one vortals',
  },
  {
    id: 2,
    name: 'Emylee',
    surname: 'Langelaan',
    middleName: undefined,
    phone: null,
  },
  {
    id: 3,
    name: 'Silvanus',
    surname: 'Bewsy',
    middleName: 'daytime cold and flu',
    phone: '999-831-0154',
    hobby: null,
  },
  {
    id: 4,
    name: 'Leroi',
    surname: 'Dragge',
    middleName: 'Monistat 7 Combination Pack',
    phone: null,
    hobby: 'evolve interactive vortals',
  },
];

// only users with non-null and non-undefined hobbies, hobby is 'string'
const u1 = filterEmptyFields(USERS, 'hobby');

// only users with non-null and non-undefined name, middleName, phone, hobby
const u2 = filterEmptyFields(USERS, ['name', 'middleName', 'phone', 'hobby']);


// only users with non-null phone, phone is 'undefined | string'
const u3 = filterNullFields(USERS, 'phone');

// same as above, array example
const u4 = filterNullFields(USERS, ['phone']);

// only users with non-undefined phone, phone is 'null | string'
const u5 = filterUndefinedFields(USERS, 'phone');

// same as above, array example
const u6 = filterUndefinedFields(USERS, ['phone']);

That's all.