0.1.1 • Published 8 months ago

@nixat/nexus v0.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

@nixat/nexus

npm version License: MIT

A lightweight, type-safe object manipulation library for modern JavaScript and TypeScript projects.

Features

  • 🚀 Lightweight - Zero dependencies, small bundle size
  • 🔒 Type-safe - Written in TypeScript with comprehensive type definitions
  • 📚 Well documented - Clear examples and API documentation
  • Well tested - Comprehensive test suite with high coverage
  • 🔧 Tree-shakable - Import only what you need
  • 🌐 Universal - Works in Node.js and browsers

Installation

# Using npm
npm install @nixat/nexus

# Using yarn
yarn add @nixat/nexus

# Using pnpm
pnpm add @nixat/nexus

Usage

import { get, set, has, clone, merge } from '@nixat/nexus';

// Get a nested property safely
const user = { profile: { name: 'John', address: { city: 'New York' } } };
const city = get(user, 'profile.address.city'); // 'New York'
const country = get(user, 'profile.address.country', 'USA'); // 'USA' (default value)

// Set a nested property safely
set(user, 'profile.address.zipCode', '10001');
// user is now { profile: { name: 'John', address: { city: 'New York', zipCode: '10001' } } }

// Check if a property exists
const hasCity = has(user, 'profile.address.city'); // true
const hasCountry = has(user, 'profile.address.country'); // false

// Clone an object
const userCopy = clone(user);

// Merge objects
const defaults = { theme: 'light', notifications: true };
const userSettings = { theme: 'dark' };
const settings = merge(defaults, userSettings);
// settings is { theme: 'dark', notifications: true }

API

Object Access

  • get(obj, path, defaultValue) - Gets the value at path of object
  • set(obj, path, value) - Sets the value at path of object
  • has(obj, path) - Checks if path is a direct property of object

Object Manipulation

  • clone(value) - Creates a deep clone of value
  • merge(...objects) - Recursively merges objects
  • pick(obj, paths) - Creates an object with picked object properties
  • omit(obj, paths) - Creates an object without specified properties

Object Transformation

  • mapKeys(obj, mapper) - Creates an object with the same values and mapped keys
  • mapValues(obj, mapper) - Creates an object with the same keys and mapped values

Namespace

All functions are also available through the nexus namespace:

import { nexus } from '@nixat/nexus';

const value = nexus.get(obj, 'path.to.prop');
nexus.set(obj, 'path.to.prop', 'value');

License

MIT