Object Creator is a TypeScript-compatible utility that allows you to create nested JavaScript objects based on string paths. It supports dynamic object structures, including arrays and various data types, making it a versatile tool for managing complex data structures.
You can install object-creator
via npm:
npm install object-creator
Or using Yarn:
yarn add object-creator
Import the DynamicObject
class into your project and use it to build nested objects based on string paths.
Creating a simple nested object without arrays:
import { DynamicObject } from 'object-creator';
const dynObj = new DynamicObject();
dynObj.set('user:object.name:string', 'Alice');
console.log(dynObj.getObject());
// Output: { user: { name: 'Alice' } }
Creating nested structures that include arrays:
import { DynamicObject } from 'object-creator';
const dynObj = new DynamicObject();
dynObj.set('user:object.emails:array', '[email protected]');
console.log(dynObj.getObject());
// Output: { user: { emails: ['alice@example.com'] } }
Adding multiple entries to an array:
import { DynamicObject } from 'object-creator';
const dynObj = new DynamicObject();
// Adding first email
dynObj.set('user:object.emails:array', '[email protected]');
// Adding second email
dynObj.set('user:object.emails:array', '[email protected]');
console.log(dynObj.getObject());
// Output:
// {
// user: {
// emails: ['[email protected]', '[email protected]']
// }
// }
Encapsulates the object being manipulated and provides methods to interact with it.
constructor(initialObject?: Record<string, any>);
- Parameters:
initialObject
(optional): The initial object to update or create. Defaults to an empty object{}
if not provided.
Creates or updates a value at the specified path.
-
Parameters:
path
(string
): The string path indicating where to set the value (e.g.,"user:object.emails:array"
).value
(ValueType
): The value to set at the specified path.
-
Returns:
Record<string, any>
: The updated object.
-
Example:
dynObj.set('user:object.age:number', 30); console.log(dynObj.getObject()); // Output: { user: { age: 30 } }
Retrieves the internal object.
-
Returns:
Record<string, any>
: The current object.
-
Example:
const obj = dynObj.getObject(); console.log(obj);
type ValueType = string | number | boolean | Record<string, any> | any[];
-
Structure:
- Use
:
to denote the type of the current segment. - Specify the data type after the colon (e.g.,
string
,number
,boolean
,object
,array
). - Separate nested keys with a dot
.
.
- Use
-
Examples:
'user:object.name:string'
creates{ user: { name: 'value' } }
.'users:object.list:array.username:string'
creates{ users: { list: [ { username: 'value' } ] } }
.
-
Handling Arrays:
- When a segment is specified as an
array
, subsequent segments within that array item can be defined. - Each call to
set
with the same array path will append a new entry to the array.
- When a segment is specified as an
// Creating a simple nested object
dynObj.set('user:object.profile.name:string', 'Alice');
console.log(dynObj.getObject());
// Output: { user: { profile: { name: 'Alice' } } }
// Creating a nested object with an array
dynObj.set('users:object.list:array.username:string', 'Bob');
console.log(dynObj.getObject());
// Output: { user: { profile: { name: 'Alice' } }, users: { list: [ { username: 'Bob' } ] } }
// Adding another user to the array
dynObj.set('users:object.list:array.username:string', 'Charlie');
console.log(dynObj.getObject());
// Output:
// {
// user: { profile: { name: 'Alice' } },
// users: {
// list: [
// { username: 'Bob' },
// { username: 'Charlie' }
// ]
// }
// }
Contributions are welcome! Please follow these steps to contribute:
-
Fork the Repository
Click the "Fork" button at the top right of the repository page to create a copy of the repository under your GitHub account.
-
Clone the Forked Repository
git clone https://github.com/yourusername/object-creator.git
-
Navigate to the Project Directory
cd object-creator
-
Create a New Branch
git checkout -b feature/your-feature-name
-
Make Your Changes
Implement your feature or bug fix.
-
Commit Your Changes
git commit -m "Add feature: your feature description"
-
Push to the Branch
git push origin feature/your-feature-name
-
Open a Pull Request
Go to the original repository and click on "Compare & pull request" to submit your changes for review.
This project is licensed under the MIT License.