Comparing version 5.0.6 to 5.1.0
@@ -0,1 +1,5 @@ | ||
### 5.1.0 | ||
- Add support for nested `<group>` tags | ||
### 5.0.6 | ||
@@ -2,0 +6,0 @@ |
@@ -40,21 +40,7 @@ "use strict"; | ||
Object.keys(obj.resources).forEach(function (nsName) { | ||
var fileChildren = createUnitTags(obj.resources[nsName]); | ||
var f = (0, _objectToXml.makeElement)('file', { | ||
id: nsName | ||
}, true); | ||
}, fileChildren); | ||
root.elements.push(f); | ||
Object.keys(obj.resources[nsName]).forEach(function (k) { | ||
var segment = (0, _objectToXml.makeElement)('segment', null, true); | ||
segment.elements.push((0, _objectToXml.makeElement)('source', null, (0, _objectToXml.makeValue)(obj.resources[nsName][k].source, _ElementTypes.default))); | ||
segment.elements.push((0, _objectToXml.makeElement)('target', null, (0, _objectToXml.makeValue)(obj.resources[nsName][k].target, _ElementTypes.default))); | ||
if ('note' in obj.resources[nsName][k]) { | ||
segment.elements.push((0, _objectToXml.makeElement)('note', null, [(0, _objectToXml.makeText)(obj.resources[nsName][k].note)])); | ||
} | ||
var additionalAttributes = obj.resources[nsName][k].additionalAttributes != null ? obj.resources[nsName][k].additionalAttributes : {}; | ||
var u = (0, _objectToXml.makeElement)('unit', Object.assign({ | ||
id: (0, _escape.default)(k) | ||
}, additionalAttributes), [segment]); | ||
f.elements.push(u); | ||
}); | ||
}); | ||
@@ -71,2 +57,35 @@ var xmlJs = { | ||
function createUnitTags(unitElements) { | ||
return Object.keys(unitElements).map(function (key) { | ||
if (unitElements[key].groupUnits) { | ||
return createGroupUnitTag(key, unitElements[key]); | ||
} else { | ||
return createUnitTag(key, unitElements[key]); | ||
} | ||
}); | ||
} | ||
function createGroupUnitTag(id, group) { | ||
var additionalAttributes = group.additionalAttributes != null ? group.additionalAttributes : {}; | ||
var groupUnits = createUnitTags(group.groupUnits); | ||
return (0, _objectToXml.makeElement)('group', Object.assign({ | ||
id: (0, _escape.default)(id) | ||
}, additionalAttributes), groupUnits); | ||
} | ||
function createUnitTag(id, unit) { | ||
var segment = (0, _objectToXml.makeElement)('segment', null, true); | ||
segment.elements.push((0, _objectToXml.makeElement)('source', null, (0, _objectToXml.makeValue)(unit.source, _ElementTypes.default))); | ||
segment.elements.push((0, _objectToXml.makeElement)('target', null, (0, _objectToXml.makeValue)(unit.target, _ElementTypes.default))); | ||
if ('note' in unit) { | ||
segment.elements.push((0, _objectToXml.makeElement)('note', null, [(0, _objectToXml.makeText)(unit.note)])); | ||
} | ||
var additionalAttributes = unit.additionalAttributes != null ? unit.additionalAttributes : {}; | ||
return (0, _objectToXml.makeElement)('unit', Object.assign({ | ||
id: (0, _escape.default)(id) | ||
}, additionalAttributes), [segment]); | ||
} | ||
var js2xliff = function js2xliff(obj, opt, cb) { | ||
@@ -73,0 +92,0 @@ if (!cb && opt === undefined) { |
@@ -41,3 +41,4 @@ "use strict"; | ||
Object.keys(obj.resources).forEach(function (nsName) { | ||
var b = (0, _objectToXml.makeElement)('body', null, true); | ||
var bodyChildren = createUnitTags(obj.resources[nsName], obj, options); | ||
var b = (0, _objectToXml.makeElement)('body', null, bodyChildren); | ||
var fileAttributes = { | ||
@@ -55,9 +56,2 @@ original: nsName, | ||
root.elements.push(f); | ||
Object.keys(obj.resources[nsName]).forEach(function (key) { | ||
if (obj.resources[nsName][key].groupUnits) { | ||
b.elements.push(createGroupUnitTag(key, obj.resources[nsName][key], obj, options)); | ||
} else { | ||
b.elements.push(createTransUnitTag(key, obj.resources[nsName][key], obj, options)); | ||
} | ||
}); | ||
}); | ||
@@ -74,11 +68,18 @@ var xmlJs = { | ||
function createUnitTags(unitElements, obj, options) { | ||
return Object.keys(unitElements).map(function (key) { | ||
if (unitElements[key].groupUnits) { | ||
return createGroupUnitTag(key, unitElements[key], obj, options); | ||
} else { | ||
return createTransUnitTag(key, unitElements[key], obj, options); | ||
} | ||
}); | ||
} | ||
function createGroupUnitTag(key, resource, obj, options) { | ||
var additionalAttributes = resource.additionalAttributes != null ? resource.additionalAttributes : {}; | ||
var u = (0, _objectToXml.makeElement)('group', Object.assign({ | ||
var groupUnits = createUnitTags(resource.groupUnits, obj, options); | ||
return (0, _objectToXml.makeElement)('group', Object.assign({ | ||
id: (0, _escape.default)(key) | ||
}, additionalAttributes), true); | ||
Object.keys(resource.groupUnits).forEach(function (transUnitKey) { | ||
u.elements.push(createTransUnitTag(transUnitKey, resource.groupUnits[transUnitKey], obj, options)); | ||
}); | ||
return u; | ||
}, additionalAttributes), groupUnits); | ||
} | ||
@@ -85,0 +86,0 @@ |
@@ -54,19 +54,6 @@ "use strict"; | ||
body.elements = body.elements || []; | ||
var transUnits = body.elements.filter(function (transunit) { | ||
return transunit.type !== 'comment'; | ||
var bodyChildren = body.elements.filter(function (child) { | ||
return child.type !== 'comment'; | ||
}); | ||
resources[namespace] = transUnits.reduce(function (file, transUnit) { | ||
var key = transUnit.attributes.id; | ||
var childs = transUnit.elements.filter(function (e) { | ||
return e.name === 'trans-unit'; | ||
}); | ||
if (childs.length) { | ||
file[key] = createGroupTag(transUnit, childs); | ||
} else { | ||
file[key] = createTransUnitTag(transUnit); | ||
} | ||
return file; | ||
}, {}); | ||
resources[namespace] = createUnits(bodyChildren); | ||
return resources; | ||
@@ -78,2 +65,19 @@ }, {}); | ||
function createUnits(childElements) { | ||
return childElements.reduce(function (parent, child) { | ||
var key = child.attributes.id; | ||
var children = child.elements.filter(function (e) { | ||
return e.name === 'trans-unit' || e.name === 'group'; | ||
}); | ||
if (children.length) { | ||
parent[key] = createGroupTag(child, children); | ||
} else { | ||
parent[key] = createTransUnitTag(child); | ||
} | ||
return parent; | ||
}, {}); | ||
} | ||
function createTransUnitTag(transUnit) { | ||
@@ -96,9 +100,5 @@ var jsUnit = transUnit.elements.reduce(function (unit, element) { | ||
function createGroupTag(groupUnit, childs) { | ||
function createGroupTag(groupUnit, children) { | ||
var jsGroupUnit = { | ||
groupUnits: childs.reduce(function (groupFile, groupTransUnit) { | ||
var key = groupTransUnit.attributes.id; | ||
groupFile[key] = createTransUnitTag(groupTransUnit); | ||
return groupFile; | ||
}, {}) | ||
groupUnits: createUnits(children) | ||
}; | ||
@@ -105,0 +105,0 @@ return addAdditionalAttributes(jsGroupUnit, groupUnit.attributes); |
@@ -58,20 +58,34 @@ "use strict"; | ||
file.elements = file.elements || []; | ||
resources[namespace] = file.elements.reduce(function (file, unit) { | ||
if (unit.name !== 'unit') return file; | ||
var key = unit.attributes.id; | ||
file[key] = unit.elements.reduce(function (unit, segment) { | ||
segment.elements.forEach(function (element) { | ||
switch (element.name) { | ||
case 'source': | ||
case 'target': | ||
case 'note': | ||
unit[element.name] = (0, _xmlToObject.extractValue)(element.elements, _ElementTypes.default); | ||
break; | ||
} | ||
resources[namespace] = createUnits(file, initValues); | ||
return resources; | ||
}, {}); | ||
} | ||
if (cb) return cb(null, result); | ||
return result; | ||
}; | ||
function createUnits(parent, initValues) { | ||
return parent.elements.reduce(function (file, unit) { | ||
var key = unit.attributes.id; | ||
var additionalAttributes = unit.attributes; | ||
delete additionalAttributes.id; | ||
switch (unit.name) { | ||
case 'unit': | ||
file[key] = createUnit(unit, initValues); | ||
if (Object.keys(additionalAttributes).length) { | ||
Object.assign(file[key], { | ||
additionalAttributes: additionalAttributes | ||
}); | ||
return unit; | ||
}, JSON.parse(JSON.stringify(initValues))); | ||
var additionalAttributes = unit.attributes; | ||
delete additionalAttributes.id; | ||
} | ||
return file; | ||
case 'group': | ||
file[key] = { | ||
groupUnits: createUnits(unit, initValues) | ||
}; | ||
if (Object.keys(additionalAttributes).length) { | ||
@@ -84,11 +98,24 @@ Object.assign(file[key], { | ||
return file; | ||
}, {}); | ||
return resources; | ||
}, {}); | ||
} | ||
if (cb) return cb(null, result); | ||
return result; | ||
}; | ||
default: | ||
return file; | ||
} | ||
}, {}); | ||
} | ||
function createUnit(unit, initValues) { | ||
return unit.elements.reduce(function (unit, segment) { | ||
segment.elements.forEach(function (element) { | ||
switch (element.name) { | ||
case 'source': | ||
case 'target': | ||
case 'note': | ||
unit[element.name] = (0, _xmlToObject.extractValue)(element.elements, _ElementTypes.default); | ||
break; | ||
} | ||
}); | ||
return unit; | ||
}, JSON.parse(JSON.stringify(initValues))); | ||
} | ||
function xliffToJs(str, options, cb) { | ||
@@ -95,0 +122,0 @@ if (!cb && options === undefined) { |
@@ -28,21 +28,7 @@ import convert from 'xml-js'; | ||
Object.keys(obj.resources).forEach(function (nsName) { | ||
var fileChildren = createUnitTags(obj.resources[nsName]); | ||
var f = makeElement('file', { | ||
id: nsName | ||
}, true); | ||
}, fileChildren); | ||
root.elements.push(f); | ||
Object.keys(obj.resources[nsName]).forEach(function (k) { | ||
var segment = makeElement('segment', null, true); | ||
segment.elements.push(makeElement('source', null, makeValue(obj.resources[nsName][k].source, ElementTypes2))); | ||
segment.elements.push(makeElement('target', null, makeValue(obj.resources[nsName][k].target, ElementTypes2))); | ||
if ('note' in obj.resources[nsName][k]) { | ||
segment.elements.push(makeElement('note', null, [makeText(obj.resources[nsName][k].note)])); | ||
} | ||
var additionalAttributes = obj.resources[nsName][k].additionalAttributes != null ? obj.resources[nsName][k].additionalAttributes : {}; | ||
var u = makeElement('unit', Object.assign({ | ||
id: escape(k) | ||
}, additionalAttributes), [segment]); | ||
f.elements.push(u); | ||
}); | ||
}); | ||
@@ -57,2 +43,35 @@ var xmlJs = { | ||
function createUnitTags(unitElements) { | ||
return Object.keys(unitElements).map(function (key) { | ||
if (unitElements[key].groupUnits) { | ||
return createGroupUnitTag(key, unitElements[key]); | ||
} else { | ||
return createUnitTag(key, unitElements[key]); | ||
} | ||
}); | ||
} | ||
function createGroupUnitTag(id, group) { | ||
var additionalAttributes = group.additionalAttributes != null ? group.additionalAttributes : {}; | ||
var groupUnits = createUnitTags(group.groupUnits); | ||
return makeElement('group', Object.assign({ | ||
id: escape(id) | ||
}, additionalAttributes), groupUnits); | ||
} | ||
function createUnitTag(id, unit) { | ||
var segment = makeElement('segment', null, true); | ||
segment.elements.push(makeElement('source', null, makeValue(unit.source, ElementTypes2))); | ||
segment.elements.push(makeElement('target', null, makeValue(unit.target, ElementTypes2))); | ||
if ('note' in unit) { | ||
segment.elements.push(makeElement('note', null, [makeText(unit.note)])); | ||
} | ||
var additionalAttributes = unit.additionalAttributes != null ? unit.additionalAttributes : {}; | ||
return makeElement('unit', Object.assign({ | ||
id: escape(id) | ||
}, additionalAttributes), [segment]); | ||
} | ||
var js2xliff = function js2xliff(obj, opt, cb) { | ||
@@ -59,0 +78,0 @@ if (!cb && opt === undefined) { |
@@ -29,3 +29,4 @@ import convert from 'xml-js'; | ||
Object.keys(obj.resources).forEach(function (nsName) { | ||
var b = makeElement('body', null, true); | ||
var bodyChildren = createUnitTags(obj.resources[nsName], obj, options); | ||
var b = makeElement('body', null, bodyChildren); | ||
var fileAttributes = { | ||
@@ -43,9 +44,2 @@ original: nsName, | ||
root.elements.push(f); | ||
Object.keys(obj.resources[nsName]).forEach(function (key) { | ||
if (obj.resources[nsName][key].groupUnits) { | ||
b.elements.push(createGroupUnitTag(key, obj.resources[nsName][key], obj, options)); | ||
} else { | ||
b.elements.push(createTransUnitTag(key, obj.resources[nsName][key], obj, options)); | ||
} | ||
}); | ||
}); | ||
@@ -60,11 +54,18 @@ var xmlJs = { | ||
function createUnitTags(unitElements, obj, options) { | ||
return Object.keys(unitElements).map(function (key) { | ||
if (unitElements[key].groupUnits) { | ||
return createGroupUnitTag(key, unitElements[key], obj, options); | ||
} else { | ||
return createTransUnitTag(key, unitElements[key], obj, options); | ||
} | ||
}); | ||
} | ||
function createGroupUnitTag(key, resource, obj, options) { | ||
var additionalAttributes = resource.additionalAttributes != null ? resource.additionalAttributes : {}; | ||
var u = makeElement('group', Object.assign({ | ||
var groupUnits = createUnitTags(resource.groupUnits, obj, options); | ||
return makeElement('group', Object.assign({ | ||
id: escape(key) | ||
}, additionalAttributes), true); | ||
Object.keys(resource.groupUnits).forEach(function (transUnitKey) { | ||
u.elements.push(createTransUnitTag(transUnitKey, resource.groupUnits[transUnitKey], obj, options)); | ||
}); | ||
return u; | ||
}, additionalAttributes), groupUnits); | ||
} | ||
@@ -71,0 +72,0 @@ |
@@ -43,19 +43,6 @@ import convert from 'xml-js'; | ||
body.elements = body.elements || []; | ||
var transUnits = body.elements.filter(function (transunit) { | ||
return transunit.type !== 'comment'; | ||
var bodyChildren = body.elements.filter(function (child) { | ||
return child.type !== 'comment'; | ||
}); | ||
resources[namespace] = transUnits.reduce(function (file, transUnit) { | ||
var key = transUnit.attributes.id; | ||
var childs = transUnit.elements.filter(function (e) { | ||
return e.name === 'trans-unit'; | ||
}); | ||
if (childs.length) { | ||
file[key] = createGroupTag(transUnit, childs); | ||
} else { | ||
file[key] = createTransUnitTag(transUnit); | ||
} | ||
return file; | ||
}, {}); | ||
resources[namespace] = createUnits(bodyChildren); | ||
return resources; | ||
@@ -67,2 +54,19 @@ }, {}); | ||
function createUnits(childElements) { | ||
return childElements.reduce(function (parent, child) { | ||
var key = child.attributes.id; | ||
var children = child.elements.filter(function (e) { | ||
return e.name === 'trans-unit' || e.name === 'group'; | ||
}); | ||
if (children.length) { | ||
parent[key] = createGroupTag(child, children); | ||
} else { | ||
parent[key] = createTransUnitTag(child); | ||
} | ||
return parent; | ||
}, {}); | ||
} | ||
function createTransUnitTag(transUnit) { | ||
@@ -85,9 +89,5 @@ var jsUnit = transUnit.elements.reduce(function (unit, element) { | ||
function createGroupTag(groupUnit, childs) { | ||
function createGroupTag(groupUnit, children) { | ||
var jsGroupUnit = { | ||
groupUnits: childs.reduce(function (groupFile, groupTransUnit) { | ||
var key = groupTransUnit.attributes.id; | ||
groupFile[key] = createTransUnitTag(groupTransUnit); | ||
return groupFile; | ||
}, {}) | ||
groupUnits: createUnits(children) | ||
}; | ||
@@ -94,0 +94,0 @@ return addAdditionalAttributes(jsGroupUnit, groupUnit.attributes); |
@@ -47,20 +47,34 @@ import convert from 'xml-js'; | ||
file.elements = file.elements || []; | ||
resources[namespace] = file.elements.reduce(function (file, unit) { | ||
if (unit.name !== 'unit') return file; | ||
var key = unit.attributes.id; | ||
file[key] = unit.elements.reduce(function (unit, segment) { | ||
segment.elements.forEach(function (element) { | ||
switch (element.name) { | ||
case 'source': | ||
case 'target': | ||
case 'note': | ||
unit[element.name] = extractValue(element.elements, ElementTypes2); | ||
break; | ||
} | ||
resources[namespace] = createUnits(file, initValues); | ||
return resources; | ||
}, {}); | ||
} | ||
if (cb) return cb(null, result); | ||
return result; | ||
}; | ||
function createUnits(parent, initValues) { | ||
return parent.elements.reduce(function (file, unit) { | ||
var key = unit.attributes.id; | ||
var additionalAttributes = unit.attributes; | ||
delete additionalAttributes.id; | ||
switch (unit.name) { | ||
case 'unit': | ||
file[key] = createUnit(unit, initValues); | ||
if (Object.keys(additionalAttributes).length) { | ||
Object.assign(file[key], { | ||
additionalAttributes: additionalAttributes | ||
}); | ||
return unit; | ||
}, JSON.parse(JSON.stringify(initValues))); | ||
var additionalAttributes = unit.attributes; | ||
delete additionalAttributes.id; | ||
} | ||
return file; | ||
case 'group': | ||
file[key] = { | ||
groupUnits: createUnits(unit, initValues) | ||
}; | ||
if (Object.keys(additionalAttributes).length) { | ||
@@ -73,11 +87,24 @@ Object.assign(file[key], { | ||
return file; | ||
}, {}); | ||
return resources; | ||
}, {}); | ||
} | ||
if (cb) return cb(null, result); | ||
return result; | ||
}; | ||
default: | ||
return file; | ||
} | ||
}, {}); | ||
} | ||
function createUnit(unit, initValues) { | ||
return unit.elements.reduce(function (unit, segment) { | ||
segment.elements.forEach(function (element) { | ||
switch (element.name) { | ||
case 'source': | ||
case 'target': | ||
case 'note': | ||
unit[element.name] = extractValue(element.elements, ElementTypes2); | ||
break; | ||
} | ||
}); | ||
return unit; | ||
}, JSON.parse(JSON.stringify(initValues))); | ||
} | ||
export default function xliffToJs(str, options, cb) { | ||
@@ -84,0 +111,0 @@ if (!cb && options === undefined) { |
@@ -27,16 +27,5 @@ import convert from 'xml-js' | ||
Object.keys(obj.resources).forEach((nsName) => { | ||
const f = makeElement('file', { id: nsName }, true) | ||
const fileChildren = createUnitTags(obj.resources[nsName]) | ||
const f = makeElement('file', { id: nsName }, fileChildren) | ||
root.elements.push(f) | ||
Object.keys(obj.resources[nsName]).forEach((k) => { | ||
const segment = makeElement('segment', null, true) | ||
segment.elements.push(makeElement('source', null, makeValue(obj.resources[nsName][k].source, ElementTypes2))) | ||
segment.elements.push(makeElement('target', null, makeValue(obj.resources[nsName][k].target, ElementTypes2))) | ||
if ('note' in obj.resources[nsName][k]) { | ||
segment.elements.push(makeElement('note', null, [makeText(obj.resources[nsName][k].note)])) | ||
} | ||
const additionalAttributes = obj.resources[nsName][k].additionalAttributes != null ? obj.resources[nsName][k].additionalAttributes : {} | ||
const u = makeElement('unit', Object.assign({ id: escape(k) }, additionalAttributes), [segment]) | ||
f.elements.push(u) | ||
}) | ||
}) | ||
@@ -53,2 +42,29 @@ | ||
function createUnitTags (unitElements) { | ||
return Object.keys(unitElements).map((key) => { | ||
if (unitElements[key].groupUnits) { | ||
return createGroupUnitTag(key, unitElements[key]) | ||
} else { | ||
return createUnitTag(key, unitElements[key]) | ||
} | ||
}) | ||
} | ||
function createGroupUnitTag (id, group) { | ||
const additionalAttributes = group.additionalAttributes != null ? group.additionalAttributes : {} | ||
const groupUnits = createUnitTags(group.groupUnits) | ||
return makeElement('group', Object.assign({ id: escape(id) }, additionalAttributes), groupUnits) | ||
} | ||
function createUnitTag (id, unit) { | ||
const segment = makeElement('segment', null, true) | ||
segment.elements.push(makeElement('source', null, makeValue(unit.source, ElementTypes2))) | ||
segment.elements.push(makeElement('target', null, makeValue(unit.target, ElementTypes2))) | ||
if ('note' in unit) { | ||
segment.elements.push(makeElement('note', null, [makeText(unit.note)])) | ||
} | ||
const additionalAttributes = unit.additionalAttributes != null ? unit.additionalAttributes : {} | ||
return makeElement('unit', Object.assign({ id: escape(id) }, additionalAttributes), [segment]) | ||
} | ||
const js2xliff = (obj, opt, cb) => { | ||
@@ -55,0 +71,0 @@ if (!cb && opt === undefined) { |
@@ -28,3 +28,5 @@ import convert from 'xml-js' | ||
Object.keys(obj.resources).forEach((nsName) => { | ||
const b = makeElement('body', null, true) | ||
const bodyChildren = createUnitTags(obj.resources[nsName], obj, options) | ||
const b = makeElement('body', null, bodyChildren) | ||
const fileAttributes = { | ||
@@ -40,10 +42,2 @@ original: nsName, | ||
root.elements.push(f) | ||
Object.keys(obj.resources[nsName]).forEach((key) => { | ||
if (obj.resources[nsName][key].groupUnits) { | ||
b.elements.push(createGroupUnitTag(key, obj.resources[nsName][key], obj, options)) | ||
} else { | ||
b.elements.push(createTransUnitTag(key, obj.resources[nsName][key], obj, options)) | ||
} | ||
}) | ||
}) | ||
@@ -60,9 +54,16 @@ | ||
function createUnitTags (unitElements, obj, options) { | ||
return Object.keys(unitElements).map((key) => { | ||
if (unitElements[key].groupUnits) { | ||
return createGroupUnitTag(key, unitElements[key], obj, options) | ||
} else { | ||
return createTransUnitTag(key, unitElements[key], obj, options) | ||
} | ||
}) | ||
} | ||
function createGroupUnitTag (key, resource, obj, options) { | ||
const additionalAttributes = resource.additionalAttributes != null ? resource.additionalAttributes : {} | ||
const u = makeElement('group', Object.assign({ id: escape(key) }, additionalAttributes), true) | ||
Object.keys(resource.groupUnits).forEach((transUnitKey) => { | ||
u.elements.push(createTransUnitTag(transUnitKey, resource.groupUnits[transUnitKey], obj, options)) | ||
}) | ||
return u | ||
const groupUnits = createUnitTags(resource.groupUnits, obj, options) | ||
return makeElement('group', Object.assign({ id: escape(key) }, additionalAttributes), groupUnits) | ||
} | ||
@@ -69,0 +70,0 @@ |
@@ -41,19 +41,9 @@ import convert from 'xml-js' | ||
body.elements = body.elements || [] | ||
const transUnits = body.elements.filter( | ||
(transunit) => transunit.type !== 'comment' | ||
const bodyChildren = body.elements.filter( | ||
(child) => child.type !== 'comment' | ||
) | ||
// namespace | ||
resources[namespace] = transUnits.reduce((file, transUnit) => { | ||
const key = transUnit.attributes.id | ||
const childs = transUnit.elements.filter((e) => e.name === 'trans-unit') | ||
if (childs.length) { | ||
file[key] = createGroupTag(transUnit, childs) | ||
} else { | ||
file[key] = createTransUnitTag(transUnit) | ||
} | ||
resources[namespace] = createUnits(bodyChildren) | ||
return file | ||
}, {}) | ||
return resources | ||
@@ -66,2 +56,16 @@ }, {}) | ||
function createUnits (childElements) { | ||
return childElements.reduce((parent, child) => { | ||
const key = child.attributes.id | ||
const children = child.elements.filter((e) => e.name === 'trans-unit' || e.name === 'group') | ||
if (children.length) { | ||
parent[key] = createGroupTag(child, children) | ||
} else { | ||
parent[key] = createTransUnitTag(child) | ||
} | ||
return parent | ||
}, {}) | ||
} | ||
function createTransUnitTag (transUnit) { | ||
@@ -83,9 +87,5 @@ const jsUnit = transUnit.elements.reduce((unit, element) => { | ||
function createGroupTag (groupUnit, childs) { | ||
function createGroupTag (groupUnit, children) { | ||
const jsGroupUnit = { | ||
groupUnits: childs.reduce((groupFile, groupTransUnit) => { | ||
const key = groupTransUnit.attributes.id | ||
groupFile[key] = createTransUnitTag(groupTransUnit) | ||
return groupFile | ||
}, {}) | ||
groupUnits: createUnits(children) | ||
} | ||
@@ -92,0 +92,0 @@ |
@@ -45,36 +45,54 @@ import convert from 'xml-js' | ||
file.elements = file.elements || [] | ||
resources[namespace] = file.elements.reduce((file, unit) => { | ||
if (unit.name !== 'unit') return file | ||
resources[namespace] = createUnits(file, initValues) | ||
const key = unit.attributes.id | ||
return resources | ||
}, {}) | ||
} | ||
// source, target, note | ||
file[key] = unit.elements.reduce((unit, segment) => { | ||
segment.elements.forEach((element) => { | ||
switch (element.name) { | ||
case 'source': | ||
case 'target': | ||
case 'note': | ||
unit[element.name] = extractValue(element.elements, ElementTypes2) | ||
break | ||
} | ||
}) | ||
if (cb) return cb(null, result) | ||
return result | ||
} | ||
return unit | ||
}, JSON.parse(JSON.stringify(initValues))) | ||
const additionalAttributes = unit.attributes | ||
delete additionalAttributes.id | ||
function createUnits (parent, initValues) { | ||
return parent.elements.reduce((file, unit) => { | ||
const key = unit.attributes.id | ||
const additionalAttributes = unit.attributes | ||
delete additionalAttributes.id | ||
switch (unit.name) { | ||
case 'unit': | ||
file[key] = createUnit(unit, initValues) | ||
if (Object.keys(additionalAttributes).length) { | ||
Object.assign(file[key], { additionalAttributes }) | ||
} | ||
return file | ||
case 'group': | ||
file[key] = { groupUnits: createUnits(unit, initValues) } | ||
if (Object.keys(additionalAttributes).length) { | ||
Object.assign(file[key], { additionalAttributes }) | ||
} | ||
return file | ||
}, {}) | ||
return resources | ||
}, {}) | ||
} | ||
default: | ||
return file | ||
} | ||
}, {}) | ||
} | ||
if (cb) return cb(null, result) | ||
return result | ||
function createUnit (unit, initValues) { | ||
// source, target, note | ||
return unit.elements.reduce((unit, segment) => { | ||
segment.elements.forEach((element) => { | ||
switch (element.name) { | ||
case 'source': | ||
case 'target': | ||
case 'note': | ||
unit[element.name] = extractValue(element.elements, ElementTypes2) | ||
break | ||
} | ||
}) | ||
return unit | ||
}, JSON.parse(JSON.stringify(initValues))) | ||
} | ||
@@ -81,0 +99,0 @@ |
{ | ||
"name": "xliff", | ||
"version": "5.0.6", | ||
"version": "5.1.0", | ||
"description": "xliff2js and js2xliff converter xliff utils", | ||
@@ -125,8 +125,8 @@ "type": "module", | ||
"devDependencies": { | ||
"@babel/cli": "7.10.4", | ||
"@babel/core": "7.10.4", | ||
"@babel/cli": "7.10.5", | ||
"@babel/core": "7.10.5", | ||
"@babel/preset-env": "7.10.4", | ||
"babel-plugin-add-module-exports": "1.0.2", | ||
"browserify": "16.5.1", | ||
"eslint": "7.3.1", | ||
"eslint": "7.5.0", | ||
"eslint-config-standard": "14.1.1", | ||
@@ -133,0 +133,0 @@ "eslint-plugin-import": "2.22.0", |
@@ -57,2 +57,10 @@ [![travis](https://img.shields.io/travis/locize/xliff.svg)](https://travis-ci.org/locize/xliff) [![npm](https://img.shields.io/npm/v/xliff.svg)](https://npmjs.org/package/xliff) | ||
</unit> | ||
<group id="group"> | ||
<unit id="groupUnit"> | ||
<segment> | ||
<source>Group</source> | ||
<target>Gruppe</target> | ||
</segment> | ||
</unit> | ||
</group> | ||
</file> | ||
@@ -75,2 +83,10 @@ </xliff>` | ||
"target": "XLIFF Daten Manager" | ||
}, | ||
"group": { | ||
"groupUnits":{ | ||
"groupUnit": { | ||
"source": "Group", | ||
"target": "Gruppe" | ||
} | ||
} | ||
} | ||
@@ -77,0 +93,0 @@ } |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
507850
10496
550