|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.4 KB
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | exports.type = 'perItem';
|
|---|
| 4 |
|
|---|
| 5 | exports.active = true;
|
|---|
| 6 |
|
|---|
| 7 | exports.description = 'Sorts children of <defs> to improve compression';
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * Sorts children of defs in order to improve compression.
|
|---|
| 11 | * Sorted first by frequency then by element name length then by element name (to ensure grouping).
|
|---|
| 12 | *
|
|---|
| 13 | * @param {Object} item current iteration item
|
|---|
| 14 | * @return {Boolean} if false, item will be filtered out
|
|---|
| 15 | *
|
|---|
| 16 | * @author David Leston
|
|---|
| 17 | */
|
|---|
| 18 | exports.fn = function(item) {
|
|---|
| 19 |
|
|---|
| 20 | if (item.isElem('defs')) {
|
|---|
| 21 |
|
|---|
| 22 | if (item.content) {
|
|---|
| 23 | var frequency = item.content.reduce(function (frequency, child) {
|
|---|
| 24 | if (child.elem in frequency) {
|
|---|
| 25 | frequency[child.elem]++;
|
|---|
| 26 | } else {
|
|---|
| 27 | frequency[child.elem] = 1;
|
|---|
| 28 | }
|
|---|
| 29 | return frequency;
|
|---|
| 30 | }, {});
|
|---|
| 31 | item.content.sort(function (a, b) {
|
|---|
| 32 | var frequencyComparison = frequency[b.elem] - frequency[a.elem];
|
|---|
| 33 | if (frequencyComparison !== 0 ) {
|
|---|
| 34 | return frequencyComparison;
|
|---|
| 35 | }
|
|---|
| 36 | var lengthComparison = b.elem.length - a.elem.length;
|
|---|
| 37 | if (lengthComparison !== 0) {
|
|---|
| 38 | return lengthComparison;
|
|---|
| 39 | }
|
|---|
| 40 | return a.elem != b.elem ? a.elem > b.elem ? -1 : 1 : 0;
|
|---|
| 41 | });
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | return true;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.