1.0.2 • Published 6 years ago

mark-props v1.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

mark-props

Wrap an object with Proxy in such way, when any gotten property or a created property will have the information about the path of its getting.

For example:

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 marking, but you can use it with any object.

Useful with babel

Use babel-plugin-module-resolver to wrap, for example, prop-types module globally:

// marked-prop-types.js
const PropTypes = require('origin-prop-types');
const markProps = require('mark-props');

module.exports = markProps(PropTypes);
// 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.

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 vladimirmorulus@gmail.com

License

MIT, 2017

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-beta

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.0

6 years ago