bem.wxs 788 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. var array = require('./array.wxs');
  2. var object = require('./object.wxs');
  3. var PREFIX = 'van-';
  4. function join(name, mods) {
  5. name = PREFIX + name;
  6. mods = mods.map(function(mod) {
  7. return name + '--' + mod;
  8. });
  9. mods.unshift(name);
  10. return mods.join(' ');
  11. }
  12. function traversing(mods, conf) {
  13. if (!conf) {
  14. return;
  15. }
  16. if (typeof conf === 'string' || typeof conf === 'number') {
  17. mods.push(conf);
  18. } else if (array.isArray(conf)) {
  19. conf.forEach(function(item) {
  20. traversing(mods, item);
  21. });
  22. } else if (typeof conf === 'object') {
  23. object.keys(conf).forEach(function(key) {
  24. conf[key] && mods.push(key);
  25. });
  26. }
  27. }
  28. function bem(name, conf) {
  29. var mods = [];
  30. traversing(mods, conf);
  31. return join(name, mods);
  32. }
  33. module.exports.bem = bem;