https://github.com/ruben-arushanyan/produce-by-path
Produce By Path is a design pattern, which is used to dynamically produce values by using the path to which it is applied. This package helps us easily create and define producer instances.
https://github.com/ruben-arushanyan/produce-by-path
deep-proxy design-pattern es6-proxy javascript nested-proxy produce-by-path produce-by-path-pattern proxy
Last synced: about 1 year ago
JSON representation
Produce By Path is a design pattern, which is used to dynamically produce values by using the path to which it is applied. This package helps us easily create and define producer instances.
- Host: GitHub
- URL: https://github.com/ruben-arushanyan/produce-by-path
- Owner: Ruben-Arushanyan
- License: mit
- Created: 2021-04-02T19:14:44.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T08:04:12.000Z (over 3 years ago)
- Last Synced: 2025-03-16T23:06:24.254Z (over 1 year ago)
- Topics: deep-proxy, design-pattern, es6-proxy, javascript, nested-proxy, produce-by-path, produce-by-path-pattern, proxy
- Language: JavaScript
- Homepage: https://github.com/Ruben-Arushanyan/produce-by-path
- Size: 263 KB
- Stars: 15
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Produce By Path
## Description
**`Produce By Path`** is a *design pattern*, which is used to dynamically produce values by using the path to which it is applied. This package helps us easily create and define producer instances.
## Installation
```bash
npm install produce-by-path
```
## Usage
```javascript
import ProduceByPath from "produce-by-path"
// CommonJS usage
// const ProduceByPath = require("produce-by-path")
// define producer instance to our liking :)
const instance = new ProduceByPath({
call: (path, args) => {
return ({
path,
args,
})
},
toPrimitive: (path, hint) => {
return path.join("--")
}
})
// Now we can apply the [[instance]] object with any properties
// combination and call as a function and receive the desired
// result as we defined in the [[call]] handler.
console.log( instance.I.love.you("arg1", "arg2") )
// {
// path: ["I", "love", "you"],
// args: ["arg1", "arg2"]
// }
// We can also apply the [[instance]] object with any properties
// combination and convert as a primitive value and receive
// the desired result as we defined in [[toPrimitive]] handler.
console.log( String(instance.I.love.you) )
// I--love--you
console.log( instance.I.love.you + '')
// I--love--you
```
## API
### **`new ProduceByPath(handlers)`**
- **`handlers`** \ defines which operations will be intercepted and how to redefine intercepted operations. It can have the following fields:
- **`call`** \ defines `call` handler, takes the following arguments:
- **`path`** \ Array of string, which contains a sequence of those properties on which the call operation was applied.
- **`args`** \ Array of arguments which was passed with the call operation.
- **`toPrimitive`** \ defines `toPrimitive` handler, takes the following arguments:
- **`path`** \ Array of string, which contains a sequence of those properties on which the toPrimitive operation was applied.
- **`hint`** \ can have the following values: `"number"`, `"string"`, `"default"` see details: [toPrimitive](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive)
## [Contributing](https://github.com/ruben-arushanyan/produce-by-path/blob/master/CONTRIBUTING.md)
Read our [contributing guide](https://github.com/ruben-arushanyan/produce-by-path/blob/master/CONTRIBUTING.md) to learn about our development process.
## [Code of Conduct](https://github.com/ruben-arushanyan/produce-by-path/blob/master/CODE_OF_CONDUCT.md)
This project has adopted the [Contributor Covenant](https://www.contributor-covenant.org) as its Code of Conduct, and we expect project participants to adhere to it. Please read the [full text](https://github.com/ruben-arushanyan/produce-by-path/blob/master/CODE_OF_CONDUCT.md) so that you can understand what actions will and will not be tolerated.
## Authors
- [Ruben Arushanyan](https://github.com/ruben-arushanyan)
## License
[MIT License](https://github.com/ruben-arushanyan/produce-by-path/blob/master/LICENSE)