Object Utilities
Overview
Section titled “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
Section titled “Object Operations”Plucking Properties
Section titled “Plucking Properties”The pluck
function creates a new object with only the specified properties:
import { pluck } from 'dill-pixel';
const character = { name: 'Hero', health: 100, mana: 50, inventory: ['sword', 'potion'], position: { x: 0, y: 0 },};
// Get only specific propertiesconst stats = pluck(character, ['health', 'mana']);// { health: 100, mana: 50 }
Omitting Properties
Section titled “Omitting Properties”The omitKeys
function creates a new object excluding the specified properties:
import { omitKeys } from 'dill-pixel';
const playerData = { id: '123', name: 'Player1', score: 1000, sessionToken: 'abc123', lastLogin: '2024-01-01',};
// Remove sensitive dataconst publicData = omitKeys(['sessionToken', 'lastLogin'], playerData);// { id: '123', name: 'Player1', score: 1000 }
Common Use Cases
Section titled “Common Use Cases”State Management
Section titled “State Management”import { pluck } from 'dill-pixel';
class GameState { private state = { player: { health: 100, position: { x: 0, y: 0 } }, score: 0, level: 1, settings: { sound: true, music: true }, };
// Get only necessary data for saving getSaveData() { return pluck(this.state, ['player', 'score', 'level']); }}
API Data Transformation
Section titled “API Data Transformation”import { omitKeys } from 'dill-pixel';
interface PlayerData { id: string; name: string; email: string; password: string; score: number;}
class PlayerAPI { // Remove sensitive data before sending to leaderboard getLeaderboardData(player: PlayerData) { return omitKeys(['email', 'password'], player); }}
Configuration Management
Section titled “Configuration Management”import { pluck } from 'dill-pixel';
class GameConfig { private config = { 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 getGraphicsSettings() { return pluck(this.config, ['graphics']); }
// Get only sound settings getSoundSettings() { return pluck(this.config, ['sound']); }}
Type Safety
Section titled “Type Safety”The object utilities are fully typed and provide TypeScript type inference:
import { pluck, omitKeys } from 'dill-pixel';
interface GameEntity { id: string; position: { x: number; y: number }; velocity: { x: number; y: number }; sprite: string; health: number;}
// Types are preservedconst position = pluck(entity, ['position']); // Type: { position: { x: number, y: number } }const noVelocity = omitKeys(['velocity'], entity); // Type: Omit<GameEntity, 'velocity'>
Best Practices
Section titled “Best Practices”-
Use
pluck
for selecting specific properties:// Good: Select only needed propertiesconst playerStats = pluck(player, ['health', 'mana', 'level']);// Less ideal: Manual property selectionconst playerStats = {health: player.health,mana: player.mana,level: player.level,}; -
Use
omitKeys
for removing sensitive or unnecessary data:// Good: Remove sensitive dataconst publicData = omitKeys(['password', 'token'], userData);// Less ideal: Manual property copyingconst publicData = { ...userData };delete publicData.password;delete publicData.token; -
Consider performance with large objects:
// Cache results for frequently accessed subsetsclass EntityManager {private positionCache = new Map<string, ReturnType<typeof pluck>>();getEntityPosition(id: string) {if (!this.positionCache.has(id)) {const entity = this.entities.get(id);if (entity) {this.positionCache.set(id, pluck(entity, ['position']));}}return this.positionCache.get(id);}}
Performance Considerations
Section titled “Performance Considerations”Efficient Usage
Section titled “Efficient Usage”// Less efficient: Creating new object frequentlyfunction update() { const pos = pluck(entity, ['position']); // New object every frame updatePosition(pos);}
// More efficient: Direct property accessfunction update() { updatePosition(entity.position); // Direct reference}
// Good: Cache frequently used subsetsconst cachedStats = pluck(character, ['health', 'mana', 'stamina']);function updateUI() { renderStats(cachedStats); // Use cached object}
Tips and Tricks
Section titled “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);}