Skip to content
Open
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
22 changes: 17 additions & 5 deletions specifyweb/backend/stored_queries/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,30 @@ def make_expr(self,
# Helper function to apply only string-ish transforms with no numeric casts
def apply_stringish(expr):
e = expr

if fieldNodeAttrib.get('trimzeros') == 'true':
numeric_str = cast(cast(e, types.Numeric(65)), types.String())
e = case(
(e.op('REGEXP')(r'^-?[0-9]+(\.[0-9]+)?$'), numeric_str),
else_=cast(e, types.String()),
)
e = cast(e, types.String())
trimmed = e

# remove leading zeros
trimmed = func.regexp_replace(trimmed, r'^(-?)0+([0-9])', r'\1\2')

# remove trailing zeros after decimal
trimmed = func.regexp_replace(trimmed, r'(\.[0-9]*?)0+$', r'\1')

# remove trailing decimal point
trimmed = func.regexp_replace(trimmed, r'\.$', '')

e = case((e.op('REGEXP')(r'^-?[0-9]+(\.[0-9]+)?$'), trimmed), else_=e)

fmt = fieldNodeAttrib.get('format')
if fmt is not None:
e = self.pseudo_sprintf(fmt, e)

sep = fieldNodeAttrib.get('sep')
if sep is not None:
e = concat(sep, e)

return e
Comment on lines 219 to 245
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs a small change in the trimzeros logic for handling different formatting.

Looks like the decimal loss case for numeric values is fixed, but it also removes the old guard that only normalized values when the entire value was numeric. As a result, values that only look numeric at the start or end can now be rewritten unexpectedly.

We can keep this fix, but only apply it when the full value matches the numeric pattern:

def apply_stringish(expr):
    e = expr

    if fieldNodeAttrib.get('trimzeros') == 'true':
        e = cast(e, types.String())
        trimmed = e

        # remove leading zeros
        trimmed = func.regexp_replace(trimmed, r'^(-?)0+([0-9])', r'\1\2')

        # remove trailing zeros after decimal
        trimmed = func.regexp_replace(trimmed, r'(\.[0-9]*?)0+$', r'\1')

        # remove trailing decimal point
        trimmed = func.regexp_replace(trimmed, r'\.$', '')

        e = case((e.op('REGEXP')(r'^-?[0-9]+(\.[0-9]+)?$'), trimmed), else_=e)

    fmt = fieldNodeAttrib.get('format')
    if fmt is not None:
        e = self.pseudo_sprintf(fmt, e)

    sep = fieldNodeAttrib.get('sep')
    if sep is not None:
        e = concat(sep, e)

    return e

Examples cases:

Input Current PR result New result
0001-A 1-A 0001-A
ABC.2300 ABC.23 ABC.2300
INV0007.000 INV0007 INV0007.000

Numeric inputs would continue to work as intended:

Input Current PR result New result
001.230400 1.2304 1.2304
0000 0 0

Let me know if that would be a good change or not?


stringish_expr = apply_stringish(raw_expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ describe('AppResourceEditButton', () => {

const dialog = getByRole('dialog');
expect(dialog.innerHTML).toMatchInlineSnapshot(`
"<div class=\\"
flex items-center gap-2 md:gap-4
-m-4 cursor-move p-4
flex-wrap
\\" id=\\"modal-0-handle\\"><div class=\\"flex items-center gap-2\\"><h2 class=\\"font-semibold text-black dark:text-white text-xl\\" id=\\"modal-0-header\\">TestTitle</h2></div></div><div class=\\"
dark:text-neutral-350 -mx-1 flex-1 overflow-y-auto px-1 py-4
text-gray-700 flex flex-col gap-2
\\" id=\\"modal-0-content\\"></div><div class=\\"flex gap-2 justify-end\\"><span class=\\"-ml-2 flex-1\\"></span><button class=\\"button rounded cursor-pointer active:brightness-80 px-4 py-2
disabled:bg-gray-200 disabled:dark:ring-neutral-500 disabled:ring-gray-400 disabled:text-gray-500
dark:disabled:!bg-neutral-700 gap-2 inline-flex items-center capitalize justify-center shadow-sm button hover:brightness-90 dark:hover:brightness-125 bg-[color:var(--secondary-button-color)] text-gray-800
dark:text-gray-100\\" type=\\"button\\">Close</button></div>"
`);
"<div class=\\"
flex items-center gap-2 md:gap-4
-m-4 cursor-move p-4
flex-wrap
\\" id=\\"modal-0-handle\\"><div class=\\"flex items-center gap-2\\"><h2 class=\\"font-semibold text-black dark:text-white text-xl\\" id=\\"modal-0-header\\">TestTitle</h2></div></div><div class=\\"
dark:text-neutral-350 -mx-1 flex-1 overflow-y-auto px-1 py-4
text-gray-700 flex flex-col gap-2
\\" id=\\"modal-0-content\\"></div><div class=\\"flex gap-2 justify-end\\"><span class=\\"-ml-2 flex-1\\"></span><button class=\\"button rounded cursor-pointer active:brightness-80 px-4 py-2
disabled:bg-gray-200 disabled:dark:ring-neutral-500 disabled:ring-gray-400 disabled:text-gray-500
dark:disabled:!bg-neutral-700 gap-2 inline-flex items-center capitalize justify-center shadow-sm button hover:brightness-90 dark:hover:brightness-125 bg-[color:var(--secondary-button-color)] text-gray-800
dark:text-gray-100\\" type=\\"button\\">Close</button></div>"
`);

expect(handleDeleted).not.toHaveBeenCalled();
});
Expand Down
6 changes: 3 additions & 3 deletions specifyweb/frontend/js_src/lib/components/Molecules/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import React from 'react';

import { useHueDifference } from '../../hooks/useHueDifference';
import { commonText } from '../../localization/common';
import type { RA } from '../../utils/types';
import { useHueDifference } from '../../hooks/useHueDifference';

export const loadingGif = (
<div className="hover:animate-hue-rotate [.reduce-motion_&]:animate-hue-rotate">
Expand All @@ -31,7 +31,7 @@ export const loadingGif = (
* This must be accompanied by a label since loading bar is hidden from screen
* readers
*/
const LoadingBar = () => {
function LoadingBar() {
const hueDifference = useHueDifference();

return (
Expand All @@ -45,7 +45,7 @@ const LoadingBar = () => {
/>
</div>
);
};
}

export const loadingBar = <LoadingBar />;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,8 @@ export function CustomSelectElement({
>
<input
// Associate validation message with the listbox
id={id('validation')}
defaultValue={validation}
id={id('validation')}
aria-label={validation}
// Announce validation message to screen readers
aria-live="polite"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function useHotHooks({
* Add accessibility labels to empty table header cells
* This ensures screen readers can properly announce the structure
*/
afterInit: function () {
afterInit() {
if (workbench.hot === undefined) return;

// Add aria-label to corner header cell (intersection of row/column headers)
Expand Down
8 changes: 1 addition & 7 deletions specifyweb/frontend/js_src/lib/localization/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,13 +1020,7 @@ export const resourcesText = createDictionary({
'uk-ua': 'Налаштувати поле',
},
trimZeros: {
'en-us': 'Trim Leading Zeros',
'de-ch': 'Führende Nullen entfernen',
'es-es': 'Recortar ceros iniciales',
'fr-fr': 'Supprimer les zéros non significatifs',
'pt-br': 'Eliminar zeros à esquerda',
'ru-ru': 'Обрезка ведущих нулей',
'uk-ua': 'Видалити початкові нулі',
'en-us': 'Trim Zeros',
},
trimZerosDescription: {
'en-us': 'Remove leading zeros from numeric values.',
Expand Down
Loading