@beabee/beabee-common 1.13.2 → 1.13.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringifyAnswer = exports.convertComponentsToFilters = exports.flattenComponents = void 0;
3
+ exports.stringifyAnswer = exports.isFileUploadAnswer = exports.isAddressAnswer = exports.convertComponentsToFilters = exports.flattenComponents = void 0;
4
4
  function flattenComponents(components) {
5
5
  return components.flatMap((component) => [
6
6
  component,
@@ -58,11 +58,29 @@ function getNiceAnswer(component, value) {
58
58
  return value;
59
59
  }
60
60
  }
61
+ function isAddressAnswer(answer) {
62
+ return !!answer && typeof answer === "object" && "geometry" in answer;
63
+ }
64
+ exports.isAddressAnswer = isAddressAnswer;
65
+ function isFileUploadAnswer(answer) {
66
+ return !!answer && typeof answer === "object" && "url" in answer;
67
+ }
68
+ exports.isFileUploadAnswer = isFileUploadAnswer;
61
69
  function stringifyAnswer(component, answer) {
62
- if (!answer) {
70
+ if (Array.isArray(answer)) {
71
+ return answer.map((a) => stringifyAnswer(component, a)).join(", ");
72
+ }
73
+ else if (!answer) {
63
74
  return "";
64
75
  }
76
+ else if (isAddressAnswer(answer)) {
77
+ return answer.formatted_address;
78
+ }
79
+ else if (isFileUploadAnswer(answer)) {
80
+ return answer.url;
81
+ }
65
82
  else if (typeof answer === "object") {
83
+ // Checkboxes
66
84
  return Object.entries(answer)
67
85
  .filter(([, selected]) => selected)
68
86
  .map(([value]) => getNiceAnswer(component, value))
@@ -53,11 +53,27 @@ function getNiceAnswer(component, value) {
53
53
  return value;
54
54
  }
55
55
  }
56
+ export function isAddressAnswer(answer) {
57
+ return !!answer && typeof answer === "object" && "geometry" in answer;
58
+ }
59
+ export function isFileUploadAnswer(answer) {
60
+ return !!answer && typeof answer === "object" && "url" in answer;
61
+ }
56
62
  export function stringifyAnswer(component, answer) {
57
- if (!answer) {
63
+ if (Array.isArray(answer)) {
64
+ return answer.map((a) => stringifyAnswer(component, a)).join(", ");
65
+ }
66
+ else if (!answer) {
58
67
  return "";
59
68
  }
69
+ else if (isAddressAnswer(answer)) {
70
+ return answer.formatted_address;
71
+ }
72
+ else if (isFileUploadAnswer(answer)) {
73
+ return answer.url;
74
+ }
60
75
  else if (typeof answer === "object") {
76
+ // Checkboxes
61
77
  return Object.entries(answer)
62
78
  .filter(([, selected]) => selected)
63
79
  .map(([value]) => getNiceAnswer(component, value))
@@ -28,5 +28,17 @@ export type CalloutComponentSchema = SelectCalloutComponentSchema | RadioCallout
28
28
  export interface CalloutFormSchema {
29
29
  components: CalloutComponentSchema[];
30
30
  }
31
- export type CalloutResponseAnswer = string | boolean | number | null | undefined | Record<string, boolean>;
32
- export type CalloutResponseAnswers = Record<string, CalloutResponseAnswer>;
31
+ export interface CalloutResponseAnswerAddress {
32
+ formatted_address: string;
33
+ geometry: {
34
+ location: {
35
+ lat: number;
36
+ lng: number;
37
+ };
38
+ };
39
+ }
40
+ export interface CalloutResponseAnswerFileUpload {
41
+ url: string;
42
+ }
43
+ export type CalloutResponseAnswer = string | boolean | number | null | undefined | Record<string, boolean> | CalloutResponseAnswerAddress | CalloutResponseAnswerFileUpload;
44
+ export type CalloutResponseAnswers = Record<string, CalloutResponseAnswer | CalloutResponseAnswer[]>;
@@ -1,7 +1,9 @@
1
- import { CalloutComponentSchema, CalloutResponseAnswer } from "../data/callouts";
1
+ import { CalloutComponentSchema, CalloutResponseAnswer, CalloutResponseAnswerAddress, CalloutResponseAnswerFileUpload } from "../data/callouts";
2
2
  import { FilterArgs } from "../search";
3
3
  export declare function flattenComponents(components: CalloutComponentSchema[]): CalloutComponentSchema[];
4
4
  export declare function convertComponentsToFilters(components: CalloutComponentSchema[]): Record<string, FilterArgs & {
5
5
  label: string;
6
6
  }>;
7
- export declare function stringifyAnswer(component: CalloutComponentSchema, answer: CalloutResponseAnswer): string;
7
+ export declare function isAddressAnswer(answer: CalloutResponseAnswer): answer is CalloutResponseAnswerAddress;
8
+ export declare function isFileUploadAnswer(answer: CalloutResponseAnswer): answer is CalloutResponseAnswerFileUpload;
9
+ export declare function stringifyAnswer(component: CalloutComponentSchema, answer: CalloutResponseAnswer | CalloutResponseAnswer[]): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beabee/beabee-common",
3
- "version": "1.13.2",
3
+ "version": "1.13.4",
4
4
  "description": "",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.js",