Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix new bug with sorting #201

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 15.0.9

---

- Bugfix:
- fix a bug with sorting strings that looks like numbers. now trying to parse all stings as numbers and if they are numbers, sort as numbers

## 15.0.8

---
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helsenorge/refero",
"version": "15.0.8",
"version": "15.0.9",
"engines": {
"node": "^18.0.0",
"npm": ">=9.0.0"
Expand Down
32 changes: 25 additions & 7 deletions src/components/formcomponents/table/tables/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ type ItemTypeToDataTypeMap = {
};
export const getPrimitiveValueFromItemType = <
ItemType extends keyof ItemTypeToDataTypeMap,
DisplayType extends ItemTypeToDataTypeMap[ItemType]
DisplayType extends ItemTypeToDataTypeMap[ItemType],
>(
type: IItemType,
res: QuestionnaireResponseItemAnswer,
Expand Down Expand Up @@ -165,7 +165,7 @@ export const getPrimitiveValueFromItemType = <
}
};
export function getQuestionnaireResponseItemAnswer(
type: typeof ItemType[keyof typeof ItemType],
type: (typeof ItemType)[keyof typeof ItemType],
result: never[]
): QuestionnaireResponseItemAnswer | Array<QuestionnaireResponseItemAnswer> {
switch (type) {
Expand Down Expand Up @@ -235,15 +235,15 @@ export const getValueIfDataReceiver = (
});
}

return getQuestionnaireResponseItemAnswer(item.type as Exclude<typeof ItemType[keyof typeof ItemType], 'url'>, result);
return getQuestionnaireResponseItemAnswer(item.type as Exclude<(typeof ItemType)[keyof typeof ItemType], 'url'>, result);
}
return undefined;
};

export const convertValuesToStrings = (values: Array<string | number | Quantity>): string[] => values.map(value => value.toString());

export const extractValuesFromAnswer = (
type: typeof ItemType[keyof typeof ItemType],
type: (typeof ItemType)[keyof typeof ItemType],
questionnaireAnswer?: QuestionnaireResponseItemAnswer | QuestionnaireResponseItemAnswer[]
): Array<string | number | Quantity> => {
if (questionnaireAnswer === undefined) {
Expand All @@ -255,7 +255,7 @@ export const extractValuesFromAnswer = (
};

export const transformAnswersToListOfStrings = (
type: typeof ItemType[keyof typeof ItemType],
type: (typeof ItemType)[keyof typeof ItemType],
answer?: QuestionnaireResponseItemAnswer | QuestionnaireResponseItemAnswer[]
): string[] => {
const value = extractValuesFromAnswer(type, answer);
Expand Down Expand Up @@ -397,10 +397,28 @@ export const sortByItemType = (aValue: string, bValue: string, sortOrder: SortDi
case ItemType.STRING:
case ItemType.TEXT:
case ItemType.OPENCHOICE:
return compareStrings(aValue, bValue, sortOrder);
case ItemType.CHOICE:
return isNumber(aValue, bValue) ? compareNumbers(aValue, bValue, sortOrder) : compareStrings(aValue, bValue, sortOrder);
default:
return compareStrings(aValue, bValue, sortOrder);
return isNumber(aValue, bValue) ? compareNumbers(aValue, bValue, sortOrder) : compareStrings(aValue, bValue, sortOrder);
}
};
const isNumber = (aValue: string, bValues: string): boolean => {
return isNumberString(aValue) && isNumberString(bValues);
};
const isNumberString = (str: string): boolean => {
str = str.trim();

if (str === '') {
return false;
}
const numberRegex = /^-?\d+(\.\d+)?([eE][-+]?\d+)?$/;

if (!numberRegex.test(str)) {
return false;
}
const number = Number(str);
return !isNaN(number);
};

const extractNumber = (value: string | undefined): number => {
Expand Down
2 changes: 1 addition & 1 deletion src/preview/FormFillerPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import LanguageLocales from '@helsenorge/core-utils/constants/languages';
import FormFillerSidebar from './FormFillerSidebar';
import { emptyPropertyReplacer } from './helpers';
import { getResources } from './resources/referoResources';
import skjema from './skjema/Test_Slider_New-nb-NO-v01.json';
import skjema from './skjema/q.json';
import { ReferoContainer } from '../components';
import rootReducer from '../reducers';

Expand Down
1 change: 0 additions & 1 deletion src/preview/skjema/Test_Slider_New-nb-NO-v01.json

This file was deleted.

File renamed without changes.
Loading