helper.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Is the provided string a URL?
  3. *
  4. * @param urlToCheck the url to inspect
  5. */
  6. export function isUrl(urlToCheck: string): boolean {
  7. if (urlToCheck.indexOf('http') === 0 || urlToCheck.indexOf('/') === 0 || urlToCheck.indexOf('./') === 0 || urlToCheck.indexOf('../') === 0) {
  8. return true;
  9. }
  10. return false;
  11. }
  12. /**
  13. * Convert a string from kebab-case to camelCase
  14. * @param s string to convert
  15. */
  16. export function kebabToCamel(s) {
  17. return s.replace(/(\-\w)/g, function (m) { return m[1].toUpperCase(); });
  18. }
  19. //https://gist.github.com/youssman/745578062609e8acac9f
  20. /**
  21. * Convert a string from camelCase to kebab-case
  22. * @param str string to convert
  23. */
  24. export function camelToKebab(str) {
  25. return !str ? null : str.replace(/([A-Z])/g, function (g) { return '-' + g[0].toLowerCase() });
  26. }
  27. /**
  28. * This will extend an object with configuration values.
  29. * What it practically does it take the keys from the configuration and set them on the object.
  30. * I the configuration is a tree, it will traverse into the tree.
  31. * @param object the object to extend
  32. * @param config the configuration object that will extend the object
  33. */
  34. export function extendClassWithConfig(object: any, config: any) {
  35. if (!config) return;
  36. Object.keys(config).forEach(key => {
  37. if (key in object && typeof object[key] !== 'function') {
  38. // if (typeof object[key] === 'function') return;
  39. // if it is an object, iterate internally until reaching basic types
  40. if (typeof object[key] === 'object') {
  41. extendClassWithConfig(object[key], config[key]);
  42. } else {
  43. if (config[key] !== undefined) {
  44. object[key] = config[key];
  45. }
  46. }
  47. }
  48. });
  49. }