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.
npm install produce-by-pathProduce 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.``bash`
npm install produce-by-path
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
$3
-
handlers \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: toPrimitiveRead our contributing guide to learn about our development process.
This project has adopted the Contributor Covenant as its Code of Conduct, and we expect project participants to adhere to it. Please read the full text so that you can understand what actions will and will not be tolerated.