@fnet/key-value-transformer v0.1.4
@fnet/key-value-transformer
Introduction
The @fnet/key-value-transformer is a utility that allows users to traverse an object or array recursively to modify keys or values according to a user-defined logic. This tool is designed to efficiently process large datasets where there is a need to apply consistent transformations or cleanups to the structure.
How It Works
This tool accepts an object or an array, along with a callback function, and it traverses through each element recursively. The callback function provides the logic on how each key (or index for arrays) and its corresponding value should be transformed. If the callback function returns null for a key, that key is removed from the structure. If it returns a new key-value pair, the original key-value pair is replaced by the new one.
Key Features
- Recursive Traversal: Automatically explores nested structures, applying transformations deeply within an object or array.
- Flexible Callback: Users define how keys and values should be updated or if they should be removed entirely.
- Supports Objects and Arrays: Whether your data is laid out in objects or arrays, this utility can handle it seamlessly.
Conclusion
The @fnet/key-value-transformer is a straightforward tool for anyone needing to update or clean up JSON-like data structures. Its flexibility and recursive nature make it suitable for various applications, from data transformation to data cleaning, without requiring complex coding solutions.
Developer Guide for @fnet/key-value-transformer
Overview
@fnet/key-value-transformer is a utility library designed for developers who need to traverse and manipulate key-value pairs within complex nested structures, such as objects and arrays. By using a simple callback function, you can rename keys, modify values, or remove properties entirely. The library handles recursive traversal, simplifying the process of deep transformations on JavaScript objects and arrays.
Installation
To install @fnet/key-value-transformer, you can use either npm or yarn. Here’s how:
Using npm:
npm install @fnet/key-value-transformerUsing yarn:
yarn add @fnet/key-value-transformerUsage
@fnet/key-value-transformer exports a main function that takes an object or an array and a callback function as arguments. The callback function is applied to each key-value pair, allowing modifications like changing keys, updating values, or removing entries.
Example Usage
First, import the library in your JavaScript or TypeScript file:
import transform from '@fnet/key-value-transformer';
const data = {
name: 'John Doe',
address: {
city: 'New York',
state: 'NY'
},
projects: ['Project1', 'Project2']
};
const transformedData = transform({
data,
callback: (key, value) => {
// Convert all object keys to uppercase
const newKey = key.toUpperCase();
// Example modification: add "Updated" to all project names
const newValue = key === 'projects' ? value.map(v => v + ' Updated') : value;
return [newKey, newValue];
}
});
console.log(transformedData);Examples
Here are some straightforward examples demonstrating typical transformations:
Example 1: Key Transformation and Value Removal
const inputData = {
firstName: 'Jane',
lastName: 'Doe',
email: 'jane.doe@example.com'
};
const cleanedData = transform({
data: inputData,
callback: (key, value) => {
if (key === 'email') {
// Remove the email key
return null;
}
// Keep the key as-is
return [key, value];
}
});
console.log(cleanedData); // Output: { firstName: 'Jane', lastName: 'Doe' }Example 2: Renaming Keys
const userData = {
user_id: 123,
user_name: 'jdoe'
};
const updatedData = transform({
data: userData,
callback: (key, value) => {
const keyMapping = {
user_id: 'id',
user_name: 'username'
};
const newKey = keyMapping[key] || key;
return [newKey, value];
}
});
console.log(updatedData); // Output: { id: 123, username: 'jdoe' }Acknowledgement
This library was developed and maintained by the team at Your Organization. Special thanks to contributors who assisted in improving the library with valuable feedback and code contributions.
By following this guide, developers can effectively utilize @fnet/key-value-transformer to perform deep transformations on their data structures, making it an essential utility for modern JavaScript applications.
Input Schema
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
data:
oneOf:
- type: object
- type: array
description: The object or array to be traversed recursively.
callback:
type: object
description: A callback function to process each key. Should return an array
[newKey, newValue] or null to remove the key.
required:
- data
- callback