0.0.0 • Published 10 months ago

@gulzer/pickle v0.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

šŸ„’ pickle: filter by interface

Recursively filters objects by TypeScript interfaces at runtime — removes extra properties with zero dependencies.

✨ Features

  • āœ… Type-safe object filtering
  • āœ… Automatically removes extra keys
  • āœ… Supports deeply nested objects
  • āœ… No need to define schemas or decorators
  • āœ… Zero dependencies, tiny footprint

šŸš€ Installation

npm intall @gulzer/pickle

or

yarn add @gulzer/pickle

šŸ”§ Usage

import { pickle } from "@gulzer/pickle";

interface Address {
  city: string;
  zip: string;
}

interface User {
  Name: string;
  Age: number;
  Address: Address;
}

const usersFromDB = {
  Name: "Alice",
  Age: 25,
  Address: {
    city: "New York",
    zip: "10001",
    country: "USA", // extra property
  },
  Phone: "123456789", // extra property
};

const filteredUser = pickle<User>(usersFromDB);

console.log(filteredUser);
// Output:
// {
//   Name: "Alice",
//   Age: 25,
//   Address: {
//     city: "New York",
//     zip: "10001"
//   }
// }

🧠 How it works

TypeScript types are not available at runtime — so this utility infers the structure from a generic type T and filters the input object based on that shape using a dummy instance ({} as T).

  • The function walks through each key in the inferred type
  • It copies only those keys from the input object
  • If a value is an object, it applies the same logic recursively

šŸ’” When to Use

  • Sanitizing user or DB input before using it
  • Enforcing a strict structure from loosely typed sources (e.g. APIs)
  • Creating lightweight runtime validation without a schema definition
0.0.0

10 months ago

1.0.0

10 months ago