/
util.js
148 lines (137 loc) · 4.37 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/** @module utility-functions */
define(['underscore'], function(_) {
/**
*
* Sorting function that deals with alpha and numeric elements.
*
* @param {String[]} list A list of strings to sort
*
* @return {String[]} The sorted list of strings
* @function naturalSort
*/
function naturalSort(list) {
var numericPart = [], alphaPart = [], result = [];
// separate the numeric and the alpha elements of the array
for (var index = 0; index < list.length; index++) {
if (isNaN(parseFloat(list[index]))) {
alphaPart.push(list[index]);
}
else {
numericPart.push(list[index]);
}
}
// ignore casing of the strings, taken from:
// http://stackoverflow.com/a/9645447/379593
alphaPart.sort(function(a, b) {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
// sort in ascending order
numericPart.sort(function(a, b) {return parseFloat(a) - parseFloat(b)});
return result.concat(alphaPart, numericPart);
}
/**
*
* Utility function that splits the lineage into taxonomic levels
* and returns the taxonomic level specified
*
* @param {String} lineage The taxonomic string, with levels seperated by
* semicolons.
* @param {Integer} levelIndex The taxonomic level to truncate to.
* 1 = Kingdom, 2 = Phylum, etc.
*
* @return {String} The taxonomic string truncated to desired level.
* @function truncateLevel
*/
function truncateLevel(lineage, levelIndex) {
if (levelIndex === 0) {
return lineage;
}
var levels = lineage.split(';');
var taxaLabel = '';
for (var i = 0; (i < levelIndex && i < levels.length); i++) {
var level = levels[i];
if (level[level.length - 1] == '_') {
taxaLabel += ';' + level;
}else {
taxaLabel = level;
}
}
return taxaLabel;
}
/**
*
* Utility function to convert an XML DOM documents to a string useful for
* unit testing. This code is based on
* [this SO answer]{@link http://stackoverflow.com/a/1750890}
*
* @param {Node} node XML DOM object, usually as created by the document
* object.
*
* @return {String} Representation of the node object.
* @function convertXMLToString
*/
function convertXMLToString(node) {
if (typeof(XMLSerializer) !== 'undefined') {
var serializer = new XMLSerializer();
return serializer.serializeToString(node);
}
else if (node.xml) {
return node.xml;
}
}
/**
*
* Split list of string values into numeric and non-numeric values
*
* @param {String[]} values The values to check
* @return {Object} Object with two keys, `numeric` and `nonNumeric`.
* `numeric` holds an array of all numeric values found. `nonNumeric` holds
* an array of the remaining values.
*/
function splitNumericValues(values) {
var numeric = [];
var nonNumeric = [];
_.each(values, function(element) {
// http://stackoverflow.com/a/9716488
if (!isNaN(parseFloat(element)) && isFinite(element)) {
numeric.push(element);
}
else {
nonNumeric.push(element);
}
});
return {numeric: numeric, nonNumeric: nonNumeric};
}
/**
*
* Escape special characters in a string for use in a regular expression.
* Credits go to [this SO answer]{@link http://stackoverflow.com/a/5306111}
*
* @param {String} regex string to escape for use in a regular expression.
*
* @return {String} String with escaped characters for use in a regular
* expression.
* @function escapeRegularExpression
*/
function escapeRegularExpression(regex) {
return regex.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}
/**
*
* Clean a string in HTML formatted strings that get created with the
* namespace tag in some browsers and not in others. Intended to facilitate
* testing.
*
* @param {String} htmlString string to remove namespace from.
*
* @return {String} String without namespace.
* @function cleanHTML
*/
function cleanHTML(htmlString) {
return htmlString.replace(' xmlns="http://www.w3.org/1999/xhtml"', '');
}
return {'truncateLevel': truncateLevel, 'naturalSort': naturalSort,
'convertXMLToString': convertXMLToString,
'escapeRegularExpression': escapeRegularExpression,
'cleanHTML': cleanHTML, 'splitNumericValues': splitNumericValues};
});