shared-utils.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { CustomFieldConfig } from './generated-types';
  2. /**
  3. * Predicate with type guard, used to filter out null or undefined values
  4. * in a filter operation.
  5. */
  6. export function notNullOrUndefined<T>(val: T | undefined | null): val is T {
  7. return val !== undefined && val !== null;
  8. }
  9. /**
  10. * Used in exhaustiveness checks to assert a codepath should never be reached.
  11. */
  12. export function assertNever(value: never): never {
  13. throw new Error(`Expected never, got ${typeof value} (${JSON.stringify(value)})`);
  14. }
  15. /**
  16. * Simple object check.
  17. * From https://stackoverflow.com/a/34749873/772859
  18. */
  19. export function isObject(item: any): item is object {
  20. return item && typeof item === 'object' && !Array.isArray(item);
  21. }
  22. export function isClassInstance(item: any): boolean {
  23. // Even if item is an object, it might not have a constructor as in the
  24. // case when it is a null-prototype object, i.e. created using `Object.create(null)`.
  25. return isObject(item) && item.constructor && item.constructor.name !== 'Object';
  26. }
  27. type NumericPropsOf<T> = {
  28. [K in keyof T]: T[K] extends number ? K : never;
  29. }[keyof T];
  30. type OnlyNumerics<T> = {
  31. [K in NumericPropsOf<T>]: T[K];
  32. };
  33. /**
  34. * Adds up all the values of a given numeric property of a list of objects.
  35. */
  36. export function summate<T extends OnlyNumerics<T>>(
  37. items: T[] | undefined | null,
  38. prop: keyof OnlyNumerics<T>,
  39. ): number {
  40. return (items || []).reduce((sum, i) => sum + i[prop], 0);
  41. }
  42. /**
  43. * Given an array of option arrays `[['red, 'blue'], ['small', 'large']]`, this method returns a new array
  44. * containing all the combinations of those options:
  45. *
  46. * @example
  47. * ```
  48. * generateAllCombinations([['red, 'blue'], ['small', 'large']]);
  49. * // =>
  50. * // [
  51. * // ['red', 'small'],
  52. * // ['red', 'large'],
  53. * // ['blue', 'small'],
  54. * // ['blue', 'large'],
  55. * // ]
  56. */
  57. export function generateAllCombinations<T>(
  58. optionGroups: T[][],
  59. combination: T[] = [],
  60. k: number = 0,
  61. output: T[][] = [],
  62. ): T[][] {
  63. if (k === 0) {
  64. optionGroups = optionGroups.filter(g => 0 < g.length);
  65. }
  66. if (k === optionGroups.length) {
  67. output.push(combination);
  68. return [];
  69. } else {
  70. // tslint:disable:prefer-for-of
  71. for (let i = 0; i < optionGroups[k].length; i++) {
  72. generateAllCombinations(optionGroups, combination.concat(optionGroups[k][i]), k + 1, output);
  73. }
  74. // tslint:enable:prefer-for-of
  75. return output;
  76. }
  77. }
  78. /**
  79. * @description
  80. * Returns the input field name of a custom field, taking into account that "relation" type custom
  81. * field inputs are suffixed with "Id" or "Ids".
  82. */
  83. export function getGraphQlInputName(config: { name: string; type: string; list?: boolean }): string {
  84. if (config.type === 'relation') {
  85. return config.list === true ? `${config.name}Ids` : `${config.name}Id`;
  86. } else {
  87. return config.name;
  88. }
  89. }