A sub-500B TypeScript utility library for safely getting and setting nested object properties with full type safety.
- π Type Safe: Full TypeScript support with strict type checking
- π‘οΈ Safe: Handles null/undefined objects gracefully
- π Immutable: All operations return new objects, never mutate the original
- π¦ Lightweight: Zero dependencies, minimal bundle size
- π§ͺ Well Tested: 100% test coverage with comprehensive test suite
- π Well Documented: Clear JSDoc comments and examples
npm install object-dot-ts
yarn add object-dot-ts
pnpm add object-dot-ts
import { get, set } from 'object-dot-ts';
const user = {
profile: {
name: 'John',
settings: {
theme: 'dark'
}
}
};
// Get simple property
const name = get(user, 'profile.name'); // 'John'
// Get with default value
const age = get(user, 'profile.age', 25); // 25
// Get with array path
const theme = get(user, ['profile', 'settings', 'theme']); // 'dark'
// Safe handling of missing properties
const missing = get(user, 'profile.missing.deep.property'); // undefined
const user = {
profile: {
name: 'John'
}
};
// Set simple property
const updated = set(user, 'profile.name', 'Jane');
// { profile: { name: 'Jane' } }
// Create nested structure
const withSettings = set(user, 'profile.settings.theme', 'light');
// { profile: { name: 'John', settings: { theme: 'light' } } }
// Set with array path
const withArray = set(user, ['profile', 'age'], 30);
// { profile: { name: 'John', age: 30 } }
// Handle complex nested structures
const complexUser = {
data: {
personal: {
contact: {
email: '[email protected]'
}
}
}
};
// Get deeply nested value
const email = get(complexUser, 'data.personal.contact.email'); // '[email protected]'
// Set deeply nested value
const withPhone = set(complexUser, 'data.personal.contact.phone', '+1234567890');
// Creates the full nested structure if it doesn't exist
// Type safety - TypeScript will catch invalid paths
// const invalid = get(user, 'profile.invalid.path'); // TypeScript error
// const invalidSet = set(user, 'profile.invalid.path', 'value'); // TypeScript error
Safely gets a nested property value from an object using a dot-notation path.
Parameters:
obj
- The object to get the value frompath
- The path to the property (supports dot notation like 'user.profile.name' or array notation like ['user', 'profile', 'name'])defaultValue
- Optional default value to return if the property doesn't exist
Returns: The value at the specified path, or the default value if the path doesn't exist
Safely sets a nested property value in an object using a dot-notation path. Returns a new object with the property set, without mutating the original object.
Parameters:
obj
- The object to set the value inpath
- The path to the property (supports dot notation like 'user.profile.name' or array notation like ['user', 'profile', 'name'])value
- The value to set at the specified path
Returns: A new object with the property set at the specified path
# Build the project
npm run build
# Run tests
npm run test
# Run tests in watch mode with UI
npm run test:watch
# Run tests with coverage
npm run test:coverage
# Format code using Biome
npm run format
- Make your changes in the
src/
directory - Run tests to ensure everything works:
npm run test
- Format your code:
npm run format
- Build the project:
npm run build
- Check coverage:
npm run test:coverage
MIT Β© Dhruv Jain
We welcome contributions to object-dot-ts
! Here's how you can help:
-
Fork the repository on GitHub
-
Clone your fork locally:
git clone https://github.com/YOUR_USERNAME/object-dot-ts.git cd object-dot-ts
-
Install dependencies:
npm install
-
Create a new branch for your feature or bugfix:
git checkout -b feature/your-feature-name # or git checkout -b fix/your-bugfix-name
- We use Biome for formatting and linting
- Run
npm run format
before committing - Follow TypeScript best practices
- Write clear, descriptive commit messages
- All new features must include tests
- Maintain 100% test coverage
- Run
npm run test:coverage
to check coverage - Use
npm run test:watch
for development
- All code must be fully typed
- Use proper TypeScript generics
- Avoid
any
types (except in test files where needed) - Ensure type definitions are exported correctly
- π maddhruv.dev
- π @maddhruv on GitHub