util.js 801 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. function iterateProps(obj, iterator) {
  2. Object.keys(obj).forEach(key => iterator({ ...obj[key], name: key }));
  3. }
  4. function mapProps(obj) {
  5. return Object.keys(obj).map(key => ({ ...obj[key], name: key }));
  6. }
  7. function filterProps(obj, filter) {
  8. const ret = {};
  9. Object.keys(obj).forEach(key => {
  10. if (filter(obj[key])) {
  11. ret[key] = obj[key];
  12. }
  13. });
  14. return ret;
  15. }
  16. function typeSignature(meta) {
  17. const type = meta.array ? `Array<${meta.type}>` : meta.type;
  18. if (meta.optional) {
  19. return `${meta.name}?: ${type}`;
  20. } else if (meta.maybe) {
  21. return `${meta.name}: ?${type}`;
  22. } else {
  23. return `${meta.name}: ${type}`;
  24. }
  25. }
  26. const unique = items => Array.from(new Set(items));
  27. module.exports = {
  28. iterateProps,
  29. mapProps,
  30. filterProps,
  31. typeSignature,
  32. unique
  33. };