create.mjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { get, isFunction } from "./basic.mjs";
  2. import { camelize } from "./format.mjs";
  3. import locale from "../locale/index.mjs";
  4. function createTranslate(name) {
  5. const prefix = camelize(name) + ".";
  6. return (path, ...args) => {
  7. const messages = locale.messages();
  8. const message = get(messages, prefix + path) || get(messages, path);
  9. return isFunction(message) ? message(...args) : message;
  10. };
  11. }
  12. function genBem(name, mods) {
  13. if (!mods) {
  14. return "";
  15. }
  16. if (typeof mods === "string") {
  17. return ` ${name}--${mods}`;
  18. }
  19. if (Array.isArray(mods)) {
  20. return mods.reduce(
  21. (ret, item) => ret + genBem(name, item),
  22. ""
  23. );
  24. }
  25. return Object.keys(mods).reduce(
  26. (ret, key) => ret + (mods[key] ? genBem(name, key) : ""),
  27. ""
  28. );
  29. }
  30. function createBEM(name) {
  31. return (el, mods) => {
  32. if (el && typeof el !== "string") {
  33. mods = el;
  34. el = "";
  35. }
  36. el = el ? `${name}__${el}` : name;
  37. return `${el}${genBem(el, mods)}`;
  38. };
  39. }
  40. function createNamespace(name) {
  41. const prefixedName = `van-${name}`;
  42. return [
  43. prefixedName,
  44. createBEM(prefixedName),
  45. createTranslate(prefixedName)
  46. ];
  47. }
  48. export {
  49. createBEM,
  50. createNamespace,
  51. createTranslate
  52. };