js-accessor
v1.2.0
Published
Provides accessors like get, set, assertGet, clone, and toObject for a JS object
Maintainers
Readme
js-accessor
Overview
Provides accessors like get, set, assertGet, clone, and toObject for a JS object.
Install
For npm
npm install js-accessorFor yarn
yarn add js-accessorUsage
import {construct} from 'js-accessor';
interface DataType {
resourceId: string;
resourceType: string;
}
const data = construct<DataType>();
const resourceId = data
.setResourceId('id')
.setResourceType('data')
.clone()
.getResourceId();API
construct(target, options)
Provides accessors for a target object, see below for accessors provided. Accessors will not be provided if a property with the accessor name already exists, allowing you to specify custom handling for certain properties. E.g. target.getField will not use the accessor flow if getField already exists in target. A known downside is set accessors, where you want to chain setters but can't. If this is a bummer, you can create a PR to address the issue or create an issue and I can look into it.
Parameters
target- Optional, target object for accessor functions.options.getter(target, field, originalField, params)- Optional, Function for what a field's value.target- Target object.field- Field whose value is to be returned. It isdataingetData.originalField- Field with accessor prefix. It isgetDataingetData.params- Array of arguments passed to the accessor function. It is["some param"]ingetData("some param").- returns
unknown- field value.
options.setter(target, field, value)- Optional, Function for customizing what a field name is resolved to.target- Target object.field- Field whose value is to be set. It isdatainsetData.originalField- Field with accessor prefix. It issetDatainsetData.params- Array of arguments passed to the accessor function. It is["value", "some param"]insetData("value", "some param").- returns
void
options.cloneResolver(target)- Optional, Function for customizing what a field name is resolved to.target- Target object.params- Array of arguments passed to the accessor function. It is["some param"]inclone("some param").- returns
object & WithAccessor<object>- Should return target object cloned with accessors.
options.toObjectResolver(target)- Optional, Function for customizing what a field name is resolved to.target- Target object.params- Array of arguments passed to the accessor function. It is["some param"]intoObject("some param").- returns
object- Should return cloned target object.
Returns
An object with accessor function described below.
Accessors
get- Getter function that retrieves the property indexed by the camel-cased name followed byget. For example,getResourceId()will returnresourceId.set- Setter function for storing the property indexed by the camel-cased name followed byset. For example,setResourceId("id")will setresourceIdtoid, andsetResourceId(undefined)will set it toundefined.assertGet- Getter function that retrieves and asserts the property indexed by the camel-cased name followed byassertGet. For example,assertGetResourceId()will returnresourceIdor throw an error if there is noresourceId.clone- Clone function that returns a copy of the data stored in an accessor's target, with accessor functions targeting the new copied target.toObject- Copy function that returns a JS object copy of the accessor's target.
