What is set-getter?
The set-getter npm package is a utility for defining properties on an object that can be accessed and modified using dot notation. It simplifies the process of setting and getting nested properties.
What are set-getter's main functionalities?
Setting Nested Properties
This feature allows you to set a nested property on an object using a dot-notated string. The package will create any necessary intermediate objects.
const set = require('set-getter');
let obj = {};
set(obj, 'a.b.c', 'value');
console.log(obj); // { a: { b: { c: 'value' } } }
Getting Nested Properties
Although set-getter is primarily for setting properties, it works well with get-value for retrieving nested properties. This example shows how to get a nested property using a dot-notated string.
const get = require('get-value');
let obj = { a: { b: { c: 'value' } } };
let value = get(obj, 'a.b.c');
console.log(value); // 'value'
Other packages similar to set-getter
lodash.set
Lodash's set function provides similar functionality for setting nested properties. It is part of the larger Lodash utility library, which offers a wide range of functions for manipulating objects, arrays, and other data types. Compared to set-getter, lodash.set is more versatile and is part of a well-maintained and widely-used library.
dot-prop
The dot-prop package allows you to get, set, delete, and check properties on an object using dot notation. It offers a more comprehensive set of features compared to set-getter, including the ability to delete properties and check for their existence.
set-getter
Create nested getter properties and any intermediary dot notation ('a.b.c'
) paths
Please consider following this project's author, Brian Woodward, and consider starring the project to show your :heart: and support.
Install
Install with npm:
$ npm install --save set-getter
Usage
var getter = require('set-getter');
set-getter works like set-value by adding a property to an object or an object hierarchy using dot notation. The main difference is that the property is added using Object.defineProperty
and is expected to be a getter function that returns a value.
Example
var obj = {};
getter(obj, 'foo', function() {
return 'bar';
});
console.log(obj.foo);
getter(obj, 'bar.baz', function() {
return 'qux';
});
console.log(obj.bar.baz);
getter(obj, ['beep', 'boop'], function() {
return 'bop';
});
console.log(obj.beep.boop);
API
Defines a getter function on an object using property path notation.
Params
obj
{Object}: Object to add property to.prop
{String|Array}: Property string or array to add.getter
{Function}: Getter function to add as a property.
Example
var obj = {};
getter(obj, 'foo', function() {
return 'bar';
});
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb
Author
Brian Woodward
License
Copyright © 2021, Brian Woodward.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on June 18, 2021.