Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
docs (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
knighttower authored Sep 15, 2023
1 parent 07f4cab commit ddd4cac
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
105 changes: 97 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
# JsObjectProxyHelper
Convert object to proxy to protect the objects
Convert object to proxy to protect the object's properties and the object itself.
Allows to declare _private, _protected and _mutable all arrays with prop names.

``npm install @knighttower/js-object-proxy-helper``

``yarn add @knighttower/js-object-proxy-helper``
## Installation

```import ProxyHelper from '@knighttower/js-object-proxy-helper';```
``` npm install @knighttower/js-object-proxy-helper ```

```css
``` yarn add @knighttower/js-object-proxy-helper ```

```javascript
import ProxyHelper from '@knighttower/js-object-proxy-helper';
```

---

## Usage

### Basic Usage

Here is how you can use the `ProxyHelper` function:

```javascript
const proxy = ProxyHelper({
objectProps: 'your object properties here',
_protected: ['prop1', 'prop2'],
_private: ['prop3'],
_mutable: ['prop4']
});
/**
* @module ProxyHelper
* Convert to proxy to protect objects
* Allows to declare _private, _protected and _mutable all arrays with prop names
* @example ProxyHelper({objectProps..., _protected: array(...)})
* @param {Object} object
* @return {Proxy}
Expand All @@ -20,6 +37,78 @@ Convert object to proxy to protect the objects
* @usage _private: array(...) -> Cannot be accessed
* @usage _mutable: array(...) -> Can be modified
*/
```

### Property Modifiers

- **_protected**: Properties that cannot be modified.
- **_private**: Properties that cannot be accessed.
- **_mutable**: Properties that can be modified.

---

## Examples



### Creating a Proxy with Protected and Private Properties

```javascript
const myObject = {
prop1: 'This is a public property',
prop2: 'This is a protected property',
prop3: 'This is a private property'
};

const proxy = ProxyHelper({
...myObject,
_protected: ['prop2'],
_private: ['prop3']
});

console.log(proxy.prop1); // Output: "This is a public property"
console.log(proxy.prop2); // Output: Error
console.log(proxy.prop3); // Output: Error
```

### Just proxy the object

```javascript
const myObject = {
prop1: 'This is a public property',
prop2: 'This is a protected property',
prop3: 'This is a private property'
};

const proxy = ProxyHelper(myObject);
```

---

## Parameters

#### `object: ProxyHelperConfig`

An object of type `ProxyHelperConfig`. This object can have the following optional fields:

- `_private?: string[]`: An array of property names that should be made private.
- `_protected?: string[]`: An array of property names that should be made protected.
- `_mutable?: string[]`: An array of property names that can be modified.
- `[key: string]: any`: Any additional properties.

---

## Return Value

Returns a Proxy object that wraps the original `object` and applies the privacy, protection, and mutability rules.

---


## License

This project is licensed under the MIT License.


[![release version](https://github.com/knighttower/JsObjectProxyHelper/actions/workflows/pre-release.yml/badge.svg)](https://github.com/knighttower/JsObjectProxyHelper/actions/workflows/pre-release.yml)
[![NPM published](https://github.com/knighttower/JsObjectProxyHelper/actions/workflows/to-npm.yml/badge.svg)](https://github.com/knighttower/JsObjectProxyHelper/actions/workflows/to-npm.yml)
16 changes: 16 additions & 0 deletions dist/ProxyHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* Author Knighttower
MIT License
[2023] [Knighttower] https://github.com/knighttower
*/
/**
* @module ProxyHelper
* Convert to proxy to protect objects
* Allows to declare _private, _protected and _mutable all arrays with prop names
* @example ProxyHelper({objectProps..., _protected: array(...)})
* @param {Object} object
* @return {Proxy}
* @usage const proxy = ProxyHelper({objectProps..., _protected: array(...), _private: array(...), _mutable: array(...)})
* @usage _protected: array(...) -> Cannot be modified
* @usage _private: array(...) -> Cannot be accessed
* @usage _mutable: array(...) -> Can be modified
*/
function ProxyHelper(object) {
'use strict';
const _private = new Set(object._private ? ['_private', ...object._private] : ['_private']);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@knighttower/js-object-proxy-helper",
"version": "1.0.4",
"version": "1.0.5",
"main": "src/ProxyHelper.ts",
"jsdeliver": "dist/ProxyHelper.js",
"unpkg": "dist/ProxyHelper.js",
Expand Down
1 change: 1 addition & 0 deletions src/ProxyHelper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ test('should not allow modification of functions', () => {
obj.func = () => 2;
if (obj.func() !== 1) throw new Error('Failed to protect function modification');
});

0 comments on commit ddd4cac

Please sign in to comment.