Skip to content

Instantly share code, notes, and snippets.

@eccegordo
Last active May 26, 2019 03:12
Show Gist options
  • Save eccegordo/aeab0425f6bc965d4365d8593c2e7b47 to your computer and use it in GitHub Desktop.
Save eccegordo/aeab0425f6bc965d4365d8593c2e7b47 to your computer and use it in GitHub Desktop.
Convert tag json to XML string
import makeTagString from 'my-app/utils/make-tag-string';
import { module, test } from 'qunit';
module('Unit | Utility | make tag string');
test('it works with single tag object', function(assert) {
let tag = { type: 'tag', name: 'root', value: "The Root" };
let result = makeTagString(tag);
let expected = '<root>The Root</root>';
assert.equal(result, expected);
});
test('it works with nested tag objects', function(assert) {
let tag = { type: 'tag', name: 'root', value: [] };
let parent = { type: 'tag', name: 'parent', value: [] };
let child1 = { type: 'tag', name: 'child', value: "first child" };
let child2 = { type: 'tag', name: 'child', value: "second child" };
parent.value.push(child1);
parent.value.push(child2);
tag.value.push(parent);
let result = makeTagString(tag);
let expected = `<root>
<parent>
<child>first child</child>
<child>second child</child>
</parent>
</root>`;
assert.equal(result, expected);
});
test('it returns nested tag objects when value is not defined', function(assert) {
let tag = { type: 'tag', name: 'root', value: [] };
let parent = { type: 'tag', name: 'parent', value: null };
tag.value.push(parent);
let result = makeTagString(tag);
let expected = `<root>
<parent />
</root>`;
assert.equal(result, expected);
});
test('it returns empty string when tag objects malformed', function(assert) {
let tag = { type: 'tag', name: 'root', value: [] };
let parent = { type: 'tag', name: 'parent', value: [] };
let child1 = { type: 'tag', name: null, value: "first child" };
let child2 = { type: 'tag', name: 'child', value: "second child" };
let child3 = { name: 'child', value: "third child" };
parent.value.push(child1);
parent.value.push(child2);
parent.value.push(child3);
tag.value.push(parent);
let result = makeTagString(tag);
let expected = `<root>
<parent>
<child>second child</child>
<child>third child</child>
</parent>
</root>`;
assert.equal(result, expected);
});
import Ember from 'ember';
const { isEmpty, isPresent } = Ember;
// Multiple types of tagObjects
// A tag with bare value
// { type: tag, name: 'root', value: "The Root" },
// A tag with nest tags, where value = array
// { type: 'tag', name: 'parent', value: [
// { type: 'tag', name: 'child', value: 'first child' },
// { type: 'tag', name: 'child', value: 'second child' }
// ]
// }
// possible to have empty tags too
// { type: tag, name: 'empty_thing', value: null },
// if type is undefined it will still produce a tag
// { name: 'anonymous', value: 'strong types are for wimps' },
export default function makeTagString(obj) {
if(isEmpty(obj)){
return null;
}
if(isEmpty(obj.name)){
return null;
}
let name = obj.name;
let value = obj.value;
if(isEmpty(value)){
value = [];
}
let hasChildren = Array.isArray(value);
if (hasChildren){
let contents = [];
value.forEach(subTag => {
contents.push(makeTagString(subTag));
});
if(isEmpty(contents)){
let str = `<${name} />`;
return str;
} else {
let actualTags = contents.filter(t => isPresent(t) === true);
let str = `<${name}>\n${actualTags.join('\n')}\n</${name}>`;
return str;
}
} else {
let str = `<${name}>${value}</${name}>`;
return str;
}
}
let tagJson = {
"filename": "myschool.xml",
"generate": "xml",
"content": [
{
"type": "tag",
"name": "root",
"value": [
{ "type": "tag", "name": "teacher", "value": "Susan" },
{
"type": "tag",
"name": "parent",
"value": [
{ "type": "tag", "name": "child", "value": "Frank" },
{ "type": "tag", "name": "child", "value": "Jane" },
{ "type": "tag", "name": "toys", "value": [
{ "type": "tag", "name": "toy", "value": "ball" },
{ "type": "tag", "name": "toy", "value": "doll" }
]
}
]
}
]
}
]
};
let outputXML = `
<root>
<teacher>Susan</teacher>
<parent>
<child>Frank</child>
<child>Jane</child>
<toys>
<toy>ball</toy>
<toy>doll</toy>
</toys>
</parent>
</root>
`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment