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

diacritics-neutralise - "s.replace is not a function" error when string column contains pure integer value #455

Open
themightystephen opened this issue Oct 24, 2019 · 5 comments

Comments

@themightystephen
Copy link

I have a DataTable with (ajax) data loaded in one column which normally contains values of type string but sometimes contains integer values. E.g.:

{
  id: 1
  name: "Company A"
  has_access: "1"
},
{
  id: 2
  name: "Company B"
  has_access: "1"
},
{
  id: 1
  name: 727
  has_access: 1
}

In the above, the third company is called 727, which unfortunately is returned as an integer type rather than string in the JSON. diacritics-neutralise hits a snag when it reaches processing this entry and gives an error: s.replace is not a function

I managed to change the back-end which generates the JSON so that it ensures 727 is always a string ("727") - that has been my solution here. However, it may be worth making the plugin more resilient to this situation by updating the removeDiacritics function to cast s to a string to ensure it's never caught off-guard:

function removeDiacritics ( s ) {
  for (var i=0, l=diacriticsMap.length; i<l; i++) {
    s = String(s).replace(diacriticsMap[i].letters, diacriticsMap[i].base);
  }
  return s; 
}

The above worked for me before using the server-side solution mentioned above.

@holtkamp
Copy link

holtkamp commented Nov 24, 2023

The same occurred when using any-number sorting plugin version 1.13.6 and a NULL or integer is handed over by the back-end after inline editing a row:

any-number.js:32 Uncaught TypeError: Cannot read properties of null (reading 'replace')
at _anyNumberSort (any-number.js:32:11)
at DataTable.ext.type.order.any-number-asc (any-number.js:39:12)
at jquery.dataTables.min.js?version=1.13.6:4:42937
at Array.sort (<anonymous>)
at ie (jquery.dataTables.min.js?version=1.13.6:4:42765)
at u (jquery.dataTables.min.js?version=1.13.6:4:23709)
at B.<anonymous> (jquery.dataTables.min.js?version=1.13.6:4:55254)
at B.iterator (jquery.dataTables.min.js?version=1.13.6:4:52083)
at B.<anonymous> (jquery.dataTables.min.js?version=1.13.6:4:55209)
at B.draw (jquery.dataTables.min.js?version=1.13.6:4:53566)

The error is caused by this line:

a = a.replace(',', '.').match(reg);

Or when an integer is involved:

Uncaught TypeError: a.replace is not a function

Like @themightystephen suggested, this can be circumvented by ensuring every value returned by the backend is a string using a PHP function like this:

    public function serializeTableRowDataArrayToJson(array $tableRowData) : string
    {
        $arrayWithStringValues = array_map(
            static fn (mixed $value) : string => strval($value),
            $tableRowData
        );

        return json_encode($arrayWithStringValues, JSON_HEX_TAG | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
    }

I agree that casting to a string in the plugin itself might be a better place to do...

Note that this behaviour did not occur in for example version 1.13.3...

@AllanJard
Copy link
Contributor

The plugin should really just allow for non-string types. If it isn't a string it can't contain a diacritic...! I'll look into that.

@AllanJard
Copy link
Contributor

The plug-in does actually cast to a string already: https://github.com/DataTables/Plugins/blob/master/sorting/src/diacritics-sort.ts#L320 . Could some one link me to a test case showing the issue so I can take a further look into it please?

@holtkamp
Copy link

@AllanJard ah, maybe I hijacked an old issue then.

My example was about any-number.js sorting plugin:

return _anyNumberSort(a, b, Number.POSITIVE_INFINITY);

@AllanJard
Copy link
Contributor

Thanks for the clarification. Fix committed here: 257a4c7 . Assuming you are happy with the fix, I'll close this issue off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants