• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 11124789710

01 Oct 2024 11:57AM UTC coverage: 29.567% (-31.4%) from 60.926%
11124789710

push

github

web-flow
Merge pull request #2105 from Omikhleia/refactor-collated-sort

0 of 10 new or added lines in 1 file covered. (0.0%)

5252 existing lines in 53 files now uncovered.

5048 of 17073 relevant lines covered (29.57%)

1856.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

19.05
/core/utilities/sorting.lua
1
--- Table sorting with language-dependent collation.
2
-- @module SU.sorting
3
--
4

5
local icu = require("justenoughicu")
136✔
6

7
local collatedSort = {
136✔
8
   -- No ICU for language "und", fallback to 'natural' table.sort
9
   und = function (t, _, comparator)
NEW
10
      if comparator then
×
NEW
11
         table.sort(t, function (e1, e2)
×
NEW
12
            return comparator(e1, e2, function (s1, s2)
×
NEW
13
               return s1 < s2 and -1 or s1 > s2 and 1 or 0
×
14
            end)
15
         end)
16
      else
NEW
17
         table.sort(t)
×
18
      end
19
   end,
20
}
21

22
setmetatable(collatedSort, {
272✔
23
   __call = function (self, t, options, comparator)
24
      local lang = SILE.settings:get("document.language")
×
25
      if self[lang] and type(self[lang]) == "function" then
×
26
         -- Allow overriding ICU for some languages, typically "und"
NEW
27
         return self[lang](t, options, comparator)
×
28
      end
29

30
      if self[lang] and type(self[lang]) == "table" then
×
31
         -- Allow customizing the default collation options for some languages
32
         options = options or self[lang]
×
33
      end
34
      -- Be efficient: create the collator once before sorting.
35
      -- I don't think we need to cache it, still.
36
      local collator = icu.collation_create(lang, options or {})
×
37

38
      local stringCompareClosure = function (s1, s2)
UNCOV
39
         return icu.compare(collator, s1, s2)
×
40
      end
NEW
41
      table.sort(t, function (e1, e2)
×
42
         -- Allow custom comparison function, notably for complex objects
43
         -- Pass the stringCompare function so that it can be used.
NEW
44
         if comparator then
×
NEW
45
            return comparator(e1, e2, stringCompareClosure)
×
46
         end
NEW
47
         return stringCompareClosure(e1, e2) < 0
×
48
      end)
49
      icu.collation_destroy(collator)
×
50
   end,
51
})
52

53
return collatedSort
136✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc