Skip to content

Commit

Permalink
Merge pull request #448 from gaurav/bug-with-double-single-quotes
Browse files Browse the repository at this point in the history
Bug in escaping single quotes in Newick export
  • Loading branch information
stevenweaver authored Jan 17, 2024
2 parents fd26c8c + 0364210 commit 3a1df56
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/formats/newick.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,15 @@ export function getNewick(annotator, root) {
element_array.push(")");
}

if(n.data.name != 'root') {
const node_label = n.data.name.replace("'", "''");
if(n.data.name !== 'root') {
const node_label = n.data.name.replaceAll("'", "''");

// Escape the entire string if it contains any whitespace.
if (/\w/.test(node_label)) {
// Surround the entire string with single quotes if it contains any
// non-alphanumeric characters.
if (/\W/.test(node_label)) {
element_array.push("'" + node_label + "'");
} else {
element_array.push(n.data.name);
element_array.push(node_label);
}
}
element_array.push(annotator(n));
Expand Down
6 changes: 3 additions & 3 deletions test/formats-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ tape("Handle Newick strings with spaces", function(test) {
* can read and write Newick strings correctly.
*/

let nwk = "('Alpha beta', ('Alpha gamma', 'Delta''s epsilon'))";
let nwk = "('Alpha beta', ('Alpha gamma', ('Delta''s epsilon', 'Epsilon zeta ''alphonso''', 'test''s')))";
let phylo = new phylotree.phylotree(nwk);

let test_leaves = ['Alpha beta', 'Alpha gamma', "Delta's epsilon"];
let test_leaves = ['Alpha beta', 'Alpha gamma', "Delta's epsilon", "Epsilon zeta 'alphonso'", "test's"];
test_leaves.forEach(function(leaf) {
let node = phylo.getNodeByName(leaf);
test.equal(node.data.name, leaf);
Expand All @@ -94,7 +94,7 @@ tape("Handle Newick strings with spaces", function(test) {
// This would be identical to the original Newick string, but phylotree.js coerces
// lengths to 1 (see https:/veg/phylotree.js/issues/440). So we expect
// all lengths to be set to 1 on export.
test.equal(phylo.getNewick(), "('Alpha beta':1,('Alpha gamma':1,'Delta''s epsilon':1):1):1;");
test.equal(phylo.getNewick(), "('Alpha beta':1,('Alpha gamma':1,('Delta''s epsilon':1,'Epsilon zeta ''alphonso''':1,'test''s':1):1):1):1;");

test.end();
});

0 comments on commit 3a1df56

Please sign in to comment.