https://github.com/morulus/mark-props
mark-props
https://github.com/morulus/mark-props
Last synced: about 1 year ago
JSON representation
mark-props
- Host: GitHub
- URL: https://github.com/morulus/mark-props
- Owner: morulus
- License: mit
- Created: 2017-11-26T22:50:51.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-28T14:09:27.000Z (about 8 years ago)
- Last Synced: 2025-06-05T04:36:03.243Z (about 1 year ago)
- Language: JavaScript
- Size: 43.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
mark-props
==
Wrap an object with [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) in such way, when any gotten property or a created property will have the information about the path of its getting.
For example:
```js
import markProps, { getMarking } from 'mark-props';
import PropTypes from 'prop-types';
const MarkedPropTypes = markProps(PropTypes);
const propType = MarkedPropTypes.shape({
enabled: MarkedPropTypes.bool,
}).isRequired;
console.log(
getMarking(propType)
);
// Result will:
// [
// {
// name: 'shape',
// type: 'function',
// args: [{
// enabled: ⨍ function() { ... }
// }]
// },
// {
// name: 'isRequired',
// type: 'function'
// }
// ]
const [ firstArgOfShape ] = getMarking(propType)[0].args;
console.log(
getMarking(firstArgOfShape)
);
// Result will:
// [
// {
// name: 'bool',
// type: 'function'
// }
// ]
```
This tool created especially for [PropTypes](https://www.npmjs.com/package/prop-types) marking, but you can use it with any object.
# Useful with babel
Use [babel-plugin-module-resolver](https://github.com/tleunen/babel-plugin-module-resolver) to wrap, for example, `prop-types` module globally:
```js
// marked-prop-types.js
const PropTypes = require('origin-prop-types');
const markProps = require('mark-props');
module.exports = markProps(PropTypes);
```
```js
// Babel configuration
{
"plugins": [
[
require.resolve('babel-plugin-module-resolver'),
{
"alias": {
"origin-prop-types": require.resolve("prop-types"),
"prop-types": require.resolve("./marked-prop-types.js"),
}
}
]
]
}
```
Thus, all PropTypes in a project will be wrapped with `mark-props`.
```js
import PropTypes from 'prop-types';
import { getMarking } from 'mark-props';
const propTypes = {
children: PropTypes.node.isRequired,
};
getMarking(propTypes.children); // [{ name: 'node' }, { name: 'isRequired' }]
```
Author
----
Vladimir Kalmykov
License
----
MIT, 2017