@posthog/core 1.54.3 → 1.54.5
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.
- package/dist/surveys/index.d.ts +1 -0
- package/dist/surveys/index.d.ts.map +1 -1
- package/dist/surveys/index.js +7 -0
- package/dist/surveys/index.mjs +1 -0
- package/dist/surveys/property-matching.js +1 -1
- package/dist/surveys/property-matching.mjs +1 -1
- package/dist/surveys/shuffling.d.ts +7 -0
- package/dist/surveys/shuffling.d.ts.map +1 -0
- package/dist/surveys/shuffling.js +70 -0
- package/dist/surveys/shuffling.mjs +29 -0
- package/package.json +2 -2
- package/src/surveys/index.ts +1 -0
- package/src/surveys/property-matching.spec.ts +7 -0
- package/src/surveys/property-matching.ts +1 -1
- package/src/surveys/shuffling.spec.ts +51 -0
- package/src/surveys/shuffling.ts +42 -0
package/dist/surveys/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export { applySurveyTranslation, detectSurveyLanguage, findBestTranslationMatch,
|
|
|
4
4
|
export { canSurveyActivateRepeatedly, doesSurveyActivateByEvent, isSurveyIterationBased } from './activation';
|
|
5
5
|
export { getSurveyIterationKey, isSurveyKeyForSurvey, type SurveyWithIteration } from './keys';
|
|
6
6
|
export { isMatchingRegex, isValidRegex, matchPropertyFilters, propertyComparisons } from './property-matching';
|
|
7
|
+
export { getDisplayOrderChoices, shuffle } from './shuffling';
|
|
7
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/surveys/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAC1F,OAAO,EACL,6BAA6B,EAC7B,4BAA4B,EAC5B,uBAAuB,EACvB,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACxB,kBAAkB,GACnB,MAAM,UAAU,CAAA;AACjB,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,wBAAwB,EACxB,eAAe,EACf,qCAAqC,EACrC,qBAAqB,GACtB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,2BAA2B,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAC7G,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,KAAK,mBAAmB,EAAE,MAAM,QAAQ,CAAA;AAC9F,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/surveys/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAC1F,OAAO,EACL,6BAA6B,EAC7B,4BAA4B,EAC5B,uBAAuB,EACvB,oBAAoB,EACpB,sBAAsB,EACtB,wBAAwB,EACxB,kBAAkB,GACnB,MAAM,UAAU,CAAA;AACjB,OAAO,EACL,sBAAsB,EACtB,oBAAoB,EACpB,wBAAwB,EACxB,eAAe,EACf,qCAAqC,EACrC,qBAAqB,GACtB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,2BAA2B,EAAE,yBAAyB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAC7G,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,KAAK,mBAAmB,EAAE,MAAM,QAAQ,CAAA;AAC9F,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAA;AAC9G,OAAO,EAAE,sBAAsB,EAAE,OAAO,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/surveys/index.js
CHANGED
|
@@ -36,6 +36,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
36
36
|
doesSurveyActivateByEvent: ()=>external_activation_js_namespaceObject.doesSurveyActivateByEvent,
|
|
37
37
|
findBestTranslationMatch: ()=>external_translations_js_namespaceObject.findBestTranslationMatch,
|
|
38
38
|
getBaseLanguage: ()=>external_translations_js_namespaceObject.getBaseLanguage,
|
|
39
|
+
getDisplayOrderChoices: ()=>external_shuffling_js_namespaceObject.getDisplayOrderChoices,
|
|
39
40
|
getLanguageFromStoredPersonProperties: ()=>external_translations_js_namespaceObject.getLanguageFromStoredPersonProperties,
|
|
40
41
|
getLengthFromRules: ()=>external_validation_js_namespaceObject.getLengthFromRules,
|
|
41
42
|
getRequirementsHint: ()=>external_validation_js_namespaceObject.getRequirementsHint,
|
|
@@ -52,6 +53,7 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
52
53
|
matchPropertyFilters: ()=>external_property_matching_js_namespaceObject.matchPropertyFilters,
|
|
53
54
|
normalizeLanguageCode: ()=>external_translations_js_namespaceObject.normalizeLanguageCode,
|
|
54
55
|
propertyComparisons: ()=>external_property_matching_js_namespaceObject.propertyComparisons,
|
|
56
|
+
shuffle: ()=>external_shuffling_js_namespaceObject.shuffle,
|
|
55
57
|
surveyHasResponses: ()=>external_events_js_namespaceObject.surveyHasResponses
|
|
56
58
|
});
|
|
57
59
|
const external_validation_js_namespaceObject = require("./validation.js");
|
|
@@ -60,6 +62,7 @@ const external_translations_js_namespaceObject = require("./translations.js");
|
|
|
60
62
|
const external_activation_js_namespaceObject = require("./activation.js");
|
|
61
63
|
const external_keys_js_namespaceObject = require("./keys.js");
|
|
62
64
|
const external_property_matching_js_namespaceObject = require("./property-matching.js");
|
|
65
|
+
const external_shuffling_js_namespaceObject = require("./shuffling.js");
|
|
63
66
|
exports.SURVEY_LANGUAGE_PROPERTY = __webpack_exports__.SURVEY_LANGUAGE_PROPERTY;
|
|
64
67
|
exports.applySurveyTranslation = __webpack_exports__.applySurveyTranslation;
|
|
65
68
|
exports.buildSurveyResponseProperties = __webpack_exports__.buildSurveyResponseProperties;
|
|
@@ -68,6 +71,7 @@ exports.detectSurveyLanguage = __webpack_exports__.detectSurveyLanguage;
|
|
|
68
71
|
exports.doesSurveyActivateByEvent = __webpack_exports__.doesSurveyActivateByEvent;
|
|
69
72
|
exports.findBestTranslationMatch = __webpack_exports__.findBestTranslationMatch;
|
|
70
73
|
exports.getBaseLanguage = __webpack_exports__.getBaseLanguage;
|
|
74
|
+
exports.getDisplayOrderChoices = __webpack_exports__.getDisplayOrderChoices;
|
|
71
75
|
exports.getLanguageFromStoredPersonProperties = __webpack_exports__.getLanguageFromStoredPersonProperties;
|
|
72
76
|
exports.getLengthFromRules = __webpack_exports__.getLengthFromRules;
|
|
73
77
|
exports.getRequirementsHint = __webpack_exports__.getRequirementsHint;
|
|
@@ -84,6 +88,7 @@ exports.isValidRegex = __webpack_exports__.isValidRegex;
|
|
|
84
88
|
exports.matchPropertyFilters = __webpack_exports__.matchPropertyFilters;
|
|
85
89
|
exports.normalizeLanguageCode = __webpack_exports__.normalizeLanguageCode;
|
|
86
90
|
exports.propertyComparisons = __webpack_exports__.propertyComparisons;
|
|
91
|
+
exports.shuffle = __webpack_exports__.shuffle;
|
|
87
92
|
exports.surveyHasResponses = __webpack_exports__.surveyHasResponses;
|
|
88
93
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
89
94
|
"SURVEY_LANGUAGE_PROPERTY",
|
|
@@ -94,6 +99,7 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
|
94
99
|
"doesSurveyActivateByEvent",
|
|
95
100
|
"findBestTranslationMatch",
|
|
96
101
|
"getBaseLanguage",
|
|
102
|
+
"getDisplayOrderChoices",
|
|
97
103
|
"getLanguageFromStoredPersonProperties",
|
|
98
104
|
"getLengthFromRules",
|
|
99
105
|
"getRequirementsHint",
|
|
@@ -110,6 +116,7 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
|
110
116
|
"matchPropertyFilters",
|
|
111
117
|
"normalizeLanguageCode",
|
|
112
118
|
"propertyComparisons",
|
|
119
|
+
"shuffle",
|
|
113
120
|
"surveyHasResponses"
|
|
114
121
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
115
122
|
Object.defineProperty(exports, '__esModule', {
|
package/dist/surveys/index.mjs
CHANGED
|
@@ -4,3 +4,4 @@ export { applySurveyTranslation, detectSurveyLanguage, findBestTranslationMatch,
|
|
|
4
4
|
export { canSurveyActivateRepeatedly, doesSurveyActivateByEvent, isSurveyIterationBased } from "./activation.mjs";
|
|
5
5
|
export { getSurveyIterationKey, isSurveyKeyForSurvey } from "./keys.mjs";
|
|
6
6
|
export { isMatchingRegex, isValidRegex, matchPropertyFilters, propertyComparisons } from "./property-matching.mjs";
|
|
7
|
+
export { getDisplayOrderChoices, shuffle } from "./shuffling.mjs";
|
|
@@ -67,7 +67,7 @@ function matchPropertyFilters(propertyFilters, eventProperties) {
|
|
|
67
67
|
if (null == eventPropertyValue) return false;
|
|
68
68
|
const comparisonFunction = propertyComparisons[filter.operator];
|
|
69
69
|
if (!comparisonFunction) return false;
|
|
70
|
-
return comparisonFunction(filter.values, [
|
|
70
|
+
return comparisonFunction(filter.values.map(String), [
|
|
71
71
|
String(eventPropertyValue)
|
|
72
72
|
]);
|
|
73
73
|
});
|
|
@@ -38,7 +38,7 @@ function matchPropertyFilters(propertyFilters, eventProperties) {
|
|
|
38
38
|
if (null == eventPropertyValue) return false;
|
|
39
39
|
const comparisonFunction = propertyComparisons[filter.operator];
|
|
40
40
|
if (!comparisonFunction) return false;
|
|
41
|
-
return comparisonFunction(filter.values, [
|
|
41
|
+
return comparisonFunction(filter.values.map(String), [
|
|
42
42
|
String(eventPropertyValue)
|
|
43
43
|
]);
|
|
44
44
|
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { MultipleSurveyQuestion } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Fisher-Yates shuffle without mutating the input array.
|
|
4
|
+
*/
|
|
5
|
+
export declare const shuffle: <T>(array: readonly T[]) => T[];
|
|
6
|
+
export declare const getDisplayOrderChoices: (question: Pick<MultipleSurveyQuestion, "choices" | "hasOpenChoice" | "shuffleOptions">) => string[];
|
|
7
|
+
//# sourceMappingURL=shuffling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shuffling.d.ts","sourceRoot":"","sources":["../../src/surveys/shuffling.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAA;AAGtD;;GAEG;AACH,eAAO,MAAM,OAAO,GAAI,CAAC,EAAE,OAAO,SAAS,CAAC,EAAE,KAAG,CAAC,EASjD,CAAA;AAUD,eAAO,MAAM,sBAAsB,GACjC,UAAU,IAAI,CAAC,sBAAsB,EAAE,SAAS,GAAG,eAAe,GAAG,gBAAgB,CAAC,KACrF,MAAM,EAcR,CAAA"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
getDisplayOrderChoices: ()=>getDisplayOrderChoices,
|
|
32
|
+
shuffle: ()=>shuffle
|
|
33
|
+
});
|
|
34
|
+
const type_utils_js_namespaceObject = require("../utils/type-utils.js");
|
|
35
|
+
const shuffle = (array)=>{
|
|
36
|
+
const shuffled = [
|
|
37
|
+
...array
|
|
38
|
+
];
|
|
39
|
+
for(let index = shuffled.length - 1; index > 0; index--){
|
|
40
|
+
const swapIndex = Math.floor(Math.random() * (index + 1));
|
|
41
|
+
[shuffled[index], shuffled[swapIndex]] = [
|
|
42
|
+
shuffled[swapIndex],
|
|
43
|
+
shuffled[index]
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
return shuffled;
|
|
47
|
+
};
|
|
48
|
+
const reverseIfUnshuffled = (unshuffled, shuffled)=>{
|
|
49
|
+
if (shuffled.length > 1 && unshuffled.every((value, index)=>value === shuffled[index])) return shuffled.reverse();
|
|
50
|
+
return shuffled;
|
|
51
|
+
};
|
|
52
|
+
const getDisplayOrderChoices = (question)=>{
|
|
53
|
+
if (!question.shuffleOptions) return question.choices;
|
|
54
|
+
const choices = [
|
|
55
|
+
...question.choices
|
|
56
|
+
];
|
|
57
|
+
const openEndedChoice = question.hasOpenChoice ? choices.pop() ?? null : null;
|
|
58
|
+
const shuffledChoices = reverseIfUnshuffled(choices, shuffle(choices));
|
|
59
|
+
if (!(0, type_utils_js_namespaceObject.isNull)(openEndedChoice)) shuffledChoices.push(openEndedChoice);
|
|
60
|
+
return shuffledChoices;
|
|
61
|
+
};
|
|
62
|
+
exports.getDisplayOrderChoices = __webpack_exports__.getDisplayOrderChoices;
|
|
63
|
+
exports.shuffle = __webpack_exports__.shuffle;
|
|
64
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
65
|
+
"getDisplayOrderChoices",
|
|
66
|
+
"shuffle"
|
|
67
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
68
|
+
Object.defineProperty(exports, '__esModule', {
|
|
69
|
+
value: true
|
|
70
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isNull } from "../utils/type-utils.mjs";
|
|
2
|
+
const shuffle = (array)=>{
|
|
3
|
+
const shuffled = [
|
|
4
|
+
...array
|
|
5
|
+
];
|
|
6
|
+
for(let index = shuffled.length - 1; index > 0; index--){
|
|
7
|
+
const swapIndex = Math.floor(Math.random() * (index + 1));
|
|
8
|
+
[shuffled[index], shuffled[swapIndex]] = [
|
|
9
|
+
shuffled[swapIndex],
|
|
10
|
+
shuffled[index]
|
|
11
|
+
];
|
|
12
|
+
}
|
|
13
|
+
return shuffled;
|
|
14
|
+
};
|
|
15
|
+
const reverseIfUnshuffled = (unshuffled, shuffled)=>{
|
|
16
|
+
if (shuffled.length > 1 && unshuffled.every((value, index)=>value === shuffled[index])) return shuffled.reverse();
|
|
17
|
+
return shuffled;
|
|
18
|
+
};
|
|
19
|
+
const getDisplayOrderChoices = (question)=>{
|
|
20
|
+
if (!question.shuffleOptions) return question.choices;
|
|
21
|
+
const choices = [
|
|
22
|
+
...question.choices
|
|
23
|
+
];
|
|
24
|
+
const openEndedChoice = question.hasOpenChoice ? choices.pop() ?? null : null;
|
|
25
|
+
const shuffledChoices = reverseIfUnshuffled(choices, shuffle(choices));
|
|
26
|
+
if (!isNull(openEndedChoice)) shuffledChoices.push(openEndedChoice);
|
|
27
|
+
return shuffledChoices;
|
|
28
|
+
};
|
|
29
|
+
export { getDisplayOrderChoices, shuffle };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@posthog/core",
|
|
3
|
-
"version": "1.54.
|
|
3
|
+
"version": "1.54.5",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
6
|
},
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
}
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@posthog/types": "^1.412.
|
|
73
|
+
"@posthog/types": "^1.412.2"
|
|
74
74
|
},
|
|
75
75
|
"devDependencies": {
|
|
76
76
|
"@rslib/core": "0.23.2",
|
package/src/surveys/index.ts
CHANGED
|
@@ -19,3 +19,4 @@ export {
|
|
|
19
19
|
export { canSurveyActivateRepeatedly, doesSurveyActivateByEvent, isSurveyIterationBased } from './activation'
|
|
20
20
|
export { getSurveyIterationKey, isSurveyKeyForSurvey, type SurveyWithIteration } from './keys'
|
|
21
21
|
export { isMatchingRegex, isValidRegex, matchPropertyFilters, propertyComparisons } from './property-matching'
|
|
22
|
+
export { getDisplayOrderChoices, shuffle } from './shuffling'
|
|
@@ -126,6 +126,13 @@ describe('shared property matching contract', () => {
|
|
|
126
126
|
expect(matchPropertyFilters(filter('exact', ['premium,vip']), { property: ['premium', 'vip'] })).toBe(true)
|
|
127
127
|
})
|
|
128
128
|
|
|
129
|
+
it('coerces filter values to strings before matching', () => {
|
|
130
|
+
// Filter values come off the /surveys JSON response untyped at runtime, so a
|
|
131
|
+
// numeric filter value must not reach `.toLowerCase()` as a number.
|
|
132
|
+
const numericFilter = filter('icontains', [100 as unknown as string])
|
|
133
|
+
expect(matchPropertyFilters(numericFilter, { property: 100 })).toBe(true)
|
|
134
|
+
})
|
|
135
|
+
|
|
129
136
|
it.each<{
|
|
130
137
|
operator: PropertyOperator
|
|
131
138
|
value: string
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { SurveyQuestionType } from '../types'
|
|
3
|
+
import { getDisplayOrderChoices, shuffle } from './shuffling'
|
|
4
|
+
|
|
5
|
+
afterEach(() => vi.restoreAllMocks())
|
|
6
|
+
|
|
7
|
+
describe('survey choice shuffling', () => {
|
|
8
|
+
const question = (choices: string[], shuffleOptions = true, hasOpenChoice = false) => ({
|
|
9
|
+
type: SurveyQuestionType.SingleChoice as const,
|
|
10
|
+
question: 'Pick one',
|
|
11
|
+
choices,
|
|
12
|
+
shuffleOptions,
|
|
13
|
+
hasOpenChoice,
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('uses Fisher-Yates without mutating the input', () => {
|
|
17
|
+
vi.spyOn(Math, 'random').mockReturnValue(0)
|
|
18
|
+
const source = Object.freeze(['a', 'b', 'c'])
|
|
19
|
+
expect(shuffle(source)).toEqual(['b', 'c', 'a'])
|
|
20
|
+
expect(source).toEqual(['a', 'b', 'c'])
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it.each([false, true])('keeps Other last without mutating choices (open=%s)', (hasOpenChoice) => {
|
|
24
|
+
vi.spyOn(Math, 'random').mockReturnValue(0)
|
|
25
|
+
const source = question(['a', 'b', 'c', ...(hasOpenChoice ? ['Other'] : [])], true, hasOpenChoice)
|
|
26
|
+
Object.freeze(source.choices)
|
|
27
|
+
Object.freeze(source)
|
|
28
|
+
expect(getDisplayOrderChoices(source)).toEqual(['b', 'c', 'a', ...(hasOpenChoice ? ['Other'] : [])])
|
|
29
|
+
expect(getDisplayOrderChoices(source)).toEqual(['b', 'c', 'a', ...(hasOpenChoice ? ['Other'] : [])])
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('reverses the regular choices if randomness leaves their order unchanged', () => {
|
|
33
|
+
vi.spyOn(Math, 'random').mockReturnValue(0.999999)
|
|
34
|
+
expect(getDisplayOrderChoices(question(['a', 'b', 'c', 'Other'], true, true))).toEqual(['c', 'b', 'a', 'Other'])
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it.each([false, undefined])('preserves configured order when shuffleOptions=%s', (shuffleOptions) => {
|
|
38
|
+
const source = { ...question(['a', 'b', 'Other'], false, true), shuffleOptions }
|
|
39
|
+
expect(getDisplayOrderChoices(source)).toBe(source.choices)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it.each([[], ['Other'], ['a', 'Other'], ['a', 'a', 'Other'], ['a', 'b', '']])(
|
|
43
|
+
'retains every choice in %j',
|
|
44
|
+
(...choices) => {
|
|
45
|
+
const source = question(choices, true, true)
|
|
46
|
+
const result = getDisplayOrderChoices(source)
|
|
47
|
+
expect([...result].sort()).toEqual([...choices].sort())
|
|
48
|
+
if (choices.length) expect(result[result.length - 1]).toBe(choices[choices.length - 1])
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { MultipleSurveyQuestion } from '../types'
|
|
2
|
+
import { isNull } from '../utils/type-utils'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fisher-Yates shuffle without mutating the input array.
|
|
6
|
+
*/
|
|
7
|
+
export const shuffle = <T>(array: readonly T[]): T[] => {
|
|
8
|
+
const shuffled = [...array]
|
|
9
|
+
|
|
10
|
+
for (let index = shuffled.length - 1; index > 0; index--) {
|
|
11
|
+
const swapIndex = Math.floor(Math.random() * (index + 1))
|
|
12
|
+
;[shuffled[index], shuffled[swapIndex]] = [shuffled[swapIndex], shuffled[index]]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return shuffled
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const reverseIfUnshuffled = <T>(unshuffled: readonly T[], shuffled: T[]): T[] => {
|
|
19
|
+
if (shuffled.length > 1 && unshuffled.every((value, index) => value === shuffled[index])) {
|
|
20
|
+
return shuffled.reverse()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return shuffled
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const getDisplayOrderChoices = (
|
|
27
|
+
question: Pick<MultipleSurveyQuestion, 'choices' | 'hasOpenChoice' | 'shuffleOptions'>
|
|
28
|
+
): string[] => {
|
|
29
|
+
if (!question.shuffleOptions) {
|
|
30
|
+
return question.choices
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const choices = [...question.choices]
|
|
34
|
+
const openEndedChoice = question.hasOpenChoice ? (choices.pop() ?? null) : null
|
|
35
|
+
const shuffledChoices = reverseIfUnshuffled(choices, shuffle(choices))
|
|
36
|
+
|
|
37
|
+
if (!isNull(openEndedChoice)) {
|
|
38
|
+
shuffledChoices.push(openEndedChoice)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return shuffledChoices
|
|
42
|
+
}
|