smartobject/readme.md
2024-04-14 18:05:04 +02:00

131 lines
5.0 KiB
Markdown

# @push.rocks/smartobject
work with objects
## Install
To install `@push.rocks/smartobject`, use the following npm command. It's recommended to install this package in a TypeScript project to make full use of its functionalities and type definitions.
```
npm install @push.rocks/smartobject --save
```
This command will add `@push.rocks/smartobject` to your project's `package.json` file and download the package to the `node_modules` directory.
## Usage
This module provides a convenient way to work with JavaScript objects, offering utilities for deep object manipulation, comparison, and more. Below are detailed examples of how to utilize `@push.rocks/smartobject` in a variety of use cases. Always use ESM syntax in combination with TypeScript for the best developer experience.
### Importing the Module
Before you can use the functionalities provided by `smartobject`, you need to import the required classes or functions into your TypeScript file.
```typescript
import {
SmartObject,
fastDeepEqual,
smartGet,
smartAdd,
compareObjects,
forEachMinimatch
} from '@push.rocks/smartobject';
```
### Working with SmartObject
The `SmartObject` class is a wrapper around a standard JavaScript object that provides additional capabilities for manipulating and querying the object.
#### Creating a SmartObject
```typescript
// Original JavaScript object
const originalObject = {
level1: {
level2: 'value',
},
};
// Creating a SmartObject
const smartObj = new SmartObject(originalObject);
```
#### Adding and Retrieving Values
```typescript
// Adding a new value
smartObj.addValueAtFlatPathString('level1.newKey', 'newValue');
// Retrieving a value
const value = smartObj.getValueAtFlatPathString('level1.level2');
console.log(value); // Output: 'value'
```
#### Converting to a Flat Object
```typescript
const flatObject = smartObj.toFlatObject();
console.log(flatObject); // Output: { 'level1.level2': 'value', 'level1.newKey': 'newValue' }
```
### Deep Object Comparison
`compareObjects` allows you to compare two objects and get detailed information about their differences.
```typescript
const obj1 = {name: 'John', age: 30};
const obj2 = {name: 'John', age: 31};
const comparisonResult = compareObjects(obj1, obj2);
console.log(comparisonResult);
```
### Deep Object Manipulation
`smartAdd` and `smartGet` enable deep manipulation and querying of objects using dot notation paths.
```typescript
// Using smartAdd to add a deep-nested property
const obj = {};
smartAdd(obj, 'user.info.name', 'Jane Doe');
// Using smartGet to retrieve a deep-nested property
const name = smartGet(obj, 'user.info.name');
console.log(name); // Output: Jane Doe
```
### Iterating over Object Properties with Minimatch Patterns
`forEachMinimatch` provides a way to execute a function for each object property that matches a given minimatch pattern.
```typescript
const userObj = {
'user.name': 'John Doe',
'user.age': 30,
'config.theme': 'dark',
};
forEachMinimatch(userObj, 'user.*', (value, key) => {
console.log(key, value);
});
```
This module leverages the power of TypeScript to offer robust typing and intelligent auto-completion, making it easier to work with complex objects and avoid common pitfalls. Whether you are manipulating deeply nested properties, comparing object differences, or iterating over specific object keys, `@push.rocks/smartobject` provides a versatile toolkit to streamline your object handling logic.
For a complete list of features and functionalities, including additional utility functions not covered in these examples, please refer to the module's documentation and type definitions.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.