Overview
Object utilities provide type-safe functions for manipulating JavaScript objects. These utilities are particularly useful for managing game state, configuration objects, and data transformations.
Object Operations
Plucking Properties
The pluck
function creates a new object with only the specified properties:
import { pluck } from ' dill-pixel ' ;
inventory: [ ' sword ' , ' potion ' ] ,
position: { x: 0 , y: 0 },
// Get only specific properties
const stats = pluck (character , [ ' health ' , ' mana ' ]);
// { health: 100, mana: 50 }
Omitting Properties
The omitKeys
function creates a new object excluding the specified properties:
import { omitKeys } from ' dill-pixel ' ;
const publicData = omitKeys ([ ' sessionToken ' , ' lastLogin ' ] , playerData);
// { id: '123', name: 'Player1', score: 1000 }
Common Use Cases
State Management
import { pluck } from ' dill-pixel ' ;
player: { health: 100 , position: { x: 0 , y: 0 } },
settings: { sound: true , music: true },
// Get only necessary data for saving
return pluck ( this . state , [ ' player ' , ' score ' , ' level ' ]);
import { omitKeys } from ' dill-pixel ' ;
// Remove sensitive data before sending to leaderboard
getLeaderboardData ( player : PlayerData ) {
return omitKeys ([ ' email ' , ' password ' ], player);
Configuration Management
import { pluck } from ' dill-pixel ' ;
graphics: { quality: ' high ' , resolution: ' 1080p ' },
sound: { volume: 0.8 , effects: true , music: true },
controls: { sensitivity: 0.5 , invertY: false },
network: { server: ' us-east ' , port: 8080 },
// Get only graphics settings
return pluck ( this . config , [ ' graphics ' ]);
// Get only sound settings
return pluck ( this . config , [ ' sound ' ]);
Type Safety
The object utilities are fully typed and provide TypeScript type inference:
import { pluck, omitKeys } from ' dill-pixel ' ;
position : { x : number ; y : number };
velocity : { x : number ; y : number };
const position = pluck (entity , [ ' position ' ]); // Type: { position: { x: number, y: number } }
const noVelocity = omitKeys ([ ' velocity ' ] , entity); // Type: Omit<GameEntity, 'velocity'>
Best Practices
Use pluck
for selecting specific properties:
// Good: Select only needed properties
const playerStats = pluck (player , [ ' health ' , ' mana ' , ' level ' ]);
// Less ideal: Manual property selection
Use omitKeys
for removing sensitive or unnecessary data:
// Good: Remove sensitive data
const publicData = omitKeys ([ ' password ' , ' token ' ] , userData);
// Less ideal: Manual property copying
const publicData = { ... userData } ;
delete publicData . password ;
Consider performance with large objects:
// Cache results for frequently accessed subsets
private positionCache = new Map < string , ReturnType < typeof pluck>>();
getEntityPosition ( id : string ) {
if ( ! this . positionCache . has (id)) {
const entity = this . entities . get (id);
this . positionCache . set (id, pluck (entity, [ ' position ' ]));
return this . positionCache . get (id);
Efficient Usage
// Less efficient: Creating new object frequently
const pos = pluck (entity , [ ' position ' ]); // New object every frame
// More efficient: Direct property access
updatePosition (entity . position ); // Direct reference
// Good: Cache frequently used subsets
const cachedStats = pluck (character , [ ' health ' , ' mana ' , ' stamina ' ]);
renderStats (cachedStats); // Use cached object
Tips and Tricks
Use with destructuring for cleaner code:
const { health , mana } = pluck (character , [ ' health ' , ' mana ' ]);
Chain operations for complex transformations:
const processedData = omitKeys ([ ' sensitive ' ] , pluck (data , [ ' public ' , ' shared ' ]));
Use with type guards:
function isValidEntity ( entity : unknown ) : entity is GameEntity {
const required = [ ' id ' , ' position ' , ' sprite ' ];
const props = pluck (entity as any , required);
return required . every ( ( key ) => key in props);