customFields.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. class CustomField {
  2. constructor(definition) {
  3. this.definition = definition;
  4. }
  5. }
  6. export class CustomFieldStringTemplate extends CustomField {
  7. constructor(definition) {
  8. super(definition);
  9. this.format = definition.settings.stringtemplateFormat;
  10. this.separator = definition.settings.stringtemplateSeparator;
  11. }
  12. getFormattedValue(rawValue) {
  13. const ret = (rawValue ?? [])
  14. .filter(value => !!value.trim())
  15. .map(value => {
  16. let _ret = this.format.replace(/[%$]\{.+?[^0-9]\}/g, function(_match) {
  17. let __ret;
  18. if (_match.match(/%\{value\}/i)) {
  19. __ret = value;
  20. } else {
  21. _match = _match.replace(/^\$/, "");
  22. try {
  23. const _json = JSON.parse(_match);
  24. __ret = value.replace(new RegExp(_json.regex, _json.flags), _json.replace);
  25. } catch (err) {
  26. console.error(err);
  27. }
  28. }
  29. return __ret;
  30. });
  31. return _ret;
  32. })
  33. .join(this.separator ?? '');
  34. return ret;
  35. }
  36. }