substitute.ts 376 B

1234567891011121314151617
  1. export interface ObjectType<T> {
  2. [key: string]: T;
  3. }
  4. function substitute <T>(str: string, o: ObjectType<T>) {
  5. if (!str || !o) {
  6. return str;
  7. }
  8. return str.replace(/\\?\{([^{}]+)\}/g, (match, name): any => {
  9. if (match.charAt(0) === '\\') {
  10. return match.slice(1);
  11. }
  12. return (o[name] === undefined) ? '' : o[name];
  13. });
  14. }
  15. export default substitute;